【发布时间】:2011-01-01 23:26:24
【问题描述】:
我正在对始终运行良好的现有代码(它是来自Jedi Windows Security Library 的Terminal Server unit)的问题进行故障排除。 经过一番调查,问题部分已归结为致电WTSOpenServer:
while true do
begin
hServer := WTSOpenServer(PChar('server'));
WTSCloseServer(hServer);
hServer := 0;
end;
在随机(但很小)的数字或运行后,我们得到一个应用程序完全崩溃,这使得调试变得困难。 以下是我已经尝试过的事情:
- WTSOpenServer 不写入 pServername 参数(如 CreateProcessW)(实际上我检查了反汇编并复制了一份)
- 将 nil 作为参数传递时,代码运行良好(因此可以与本地机器一起使用)。
- 当使用远程服务器、localhost 甚至虚拟服务器作为 pServerName 时,结果总是崩溃(在 Vista 和更高版本中,即使是无效的服务器名也会根据文档返回有效句柄)。
- 用 Delphi 2009 和 2010 测试
- 相同的代码在 Visual Studio (c++) 中运行良好。
-
检查了 Visual Studio 中的反汇编,并从 Delphi 中调用了 asm 中的 WTSOpenServer(并将 Handle 类型更改为 C 中的指针):
hModule := LoadLibrary('wtsapi32.dll'); if hModule = 0 then Exit; WTSOpenServer := GetProcAddress(hModule, 'WTSOpenServerW'); if WTSOpenServer = nil then Exit; while true do begin asm push dword ptr pServerName; call dword ptr WTSOpenServer; mov [hServer], eax; end; hServer := nil; end; 省略对 WTSCloseServer 的调用
- 在 x64 和 x86 版本的 Windows 7 上测试代码
- 使用外部调试器而不是 Delphi 调试器(在这种情况下似乎运行良好,所以我猜这是某种计时/线程/死锁问题)
- 添加了AddVectoredExceptionHandler,然后我看到一个 EXCEPTION_ACCESS_VIOLATION 但堆栈似乎已损坏,EIP 为 1,因此无法确定它发生在哪里。
目前我不知道如何进一步解决此问题或找到解释。
【问题讨论】:
-
我在 Delphi 7 和 2007(调用 WTSOpenServerW 版本)上测试了完全相同的代码,并且两者都运行良好。结合它在 c++ 中没有问题的事实,我怀疑 Delphi 在这里......
-
这可能是 UnicodeString / AnsiString 问题吗?
-
否:显示的代码是项目中唯一的代码。
标签: delphi winapi windows-7 remote-desktop terminal-services