delphi7开发mstsc的远程程序,解决不能使用“仅允许网络级别的身份验证的远程桌面的计算机连接”的问题

项目需要,做个delphi7下的远程桌面的程序去维护已有的服务器,因为考虑到安全性问题,把远程服务器的用户和密码加密保存在数据库里面,调用微软的mstsc,传递用户名和密码打开远程桌面。网上有很多关于mstsc的delphi开发的例子,基础都是用到MSTSCLib_TLB.pas进行开发。这个是通过微软提供的mstsc的activex导入的接口文件。引入这个文件后,程序调用很简单:

var
  RDPClient: TMsRdpClient4;
begin
  RDPClient := TMsRdpClient4.Create(self);

  RDPClient.Parent := pnl1;
  RDPClient.Align := alClient;

  //RDPClient.Domain :=  domain;
  RDPClient.Server := 'xxx.xxx.xxx.xxx';
  RDPClient.AdvancedSettings5.RDPPort := 3389;
  RDPClient.UserName := 'admin';
  RDPClient.AdvancedSettings5.ClearTextPassword := 'xxxxxx';
  RDPClient.ConnectingText := 'IP';

  RDPClient.Connect;

  RDPClient.Free;

编译后运行,发现很多服务器都不能正常打开,不能打开的服务器都有如下的设置,如果关闭了就能正常打开

考虑了关闭这个会降低安全性,而且自己机器的mstsc可以正常使用,所以问题应该出在接口上:
查看接口的参数定义,发现了这一条:
RDPClient.AdvancedSettings5.AuthenticationLevel 连接等级 :2就是启用网络级别身份验证
但是加上后仍然打不开,参考了java写的程序,发现上面还有一句:
RDPClient.AdvancedSettings.EnableCredSspSupport := false
感觉这个是和提交的加密模式有关的,应该是这个,可是下面引用的pas没有这个参数

应用的MSTSCLib_TLB.pas

unit MSTSCLib_TLB;

// ************************************************************************ //
// WARNING                                                                    
// -------                                                                    
// The types declared in this file were generated from data read from a       
// Type Library. If this type library is explicitly or indirectly (via        
// another type library referring to this type library) re-imported, or the   
// 'Refresh' command of the Type Library Editor activated while editing the   
// Type Library, the contents of this file will be regenerated and all        
// manual modifications will be lost.                                         
// ************************************************************************ //

// PASTLWTR : 1.2
// File generated on 2014-5-8 2:54:57 from Type Library described below.

// ************************************************************************  //
// Type Lib: C:\WINDOWS\system32\mstscax.dll (1)
// LIBID: {8C11EFA1-92C3-11D1-BC1E-00C04FA31489}
// LCID: 0
// Helpfile: 
// HelpString: Microsoft Terminal Services Active Client 1.0 Type Library
// DepndLst: 
//   (1) v2.0 stdole, (C:\WINDOWS\system32\stdole2.tlb)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface

查看下这个pas感觉这个版本的接口比较老了,应该只支持到rdp4,现在要用rdp6(win7下的版本是6.1),网上没有找到新的pas,基本都是这个版本的,看来只能自己搞个,按照网上的一些方法,导入mstsc到delphi,发现使用高版本的都会报错。没办法,换成delphi最新的10.3试下,找到对应的MSTSCLib_TLB.pas,看了下头是64位的:

unit MSTSCLib_TLB;

// ************************************************************************ //
// WARNING                                                                    
// -------                                                                    
// The types declared in this file were generated from data read from a       
// Type Library. If this type library is explicitly or indirectly (via        
// another type library referring to this type library) re-imported, or the   
// 'Refresh' command of the Type Library Editor activated while editing the   
// Type Library, the contents of this file will be regenerated and all        
// manual modifications will be lost.                                         
// ************************************************************************ //

// $Rev: 52393 $
// File generated on 2020-05-25 12:33:47 from Type Library described below.

// ************************************************************************  //
// Type Lib: C:\Windows\SysWOW64\mstscax.dll (1)
// LIBID: {8C11EFA1-92C3-11D1-BC1E-00C04FA31489}
// LCID: 0
// Helpfile: 
// HelpString: Microsoft Terminal Services Control Type Library
// DepndLst: 
//   (1) v2.0 stdole, (C:\Windows\SysWOW64\stdole2.tlb)
// SYS_KIND: SYS_WIN32
// Errors:

然后在delphi下引用,会有一些差别,修改下,能用了,将原来的代码改成rdp6的,这样 :RDPClient.AdvancedSettings7.EnableCredSspSupport := true终于可以用了,代码如下:

var
  RDPClient: TMsRdpClient6;
begin
  RDPClient := TMsRdpClient6.Create(self);

  RDPClient.Parent := pnl1;
  RDPClient.Align := alClient;

  //RDPClient.Domain :=  domain;
  RDPClient.Server := 'xxx.xxx.xxx.xxx';
  RDPClient.AdvancedSettings5.RDPPort := 3389;
  RDPClient.UserName := 'admin';
  RDPClient.AdvancedSettings5.ClearTextPassword := 'xxxxxx';
  RDPClient.ConnectingText := 'IP';
  RDPClient.AdvancedSettings7.EnableCredSspSupport := true;
  RDPClient.AdvancedSettings5.AuthenticationLevel := 2;

  RDPClient.Connect;

  RDPClient.Free;

编译后远程可以显示了,但是还是登不上服务器,郁闷中…
无意发现java和c#的例子中有人用的事MsRdpClient6NotSafeForScripting这个类,改了下,大工告成…

var
  RDPClient: TMsRdpClient6NotSafeForScripting;
begin
  RDPClient := TMsRdpClient6NotSafeForScripting.Create(self);

  RDPClient.Parent := pnl1;
  RDPClient.Align := alClient;

  //RDPClient.Domain :=  domain;
  RDPClient.Server := 'xxx.xxx.xxx.xxx';
  RDPClient.AdvancedSettings5.RDPPort := 3389;
  RDPClient.UserName := 'admin';
  RDPClient.AdvancedSettings5.ClearTextPassword := 'xxxxxx';
  RDPClient.ConnectingText := 'IP';

  RDPClient.AdvancedSettings7.EnableCredSspSupport := true;
  RDPClient.AdvancedSettings5.AuthenticationLevel := 2;

  RDPClient.Connect;

  //RDPClient.Free;

end;

MSTSCLib_TLB.pas下载:
MSDN下载链接
有需要的也可以联系我:QQ:649124453

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值