Delphi XE4 64位中的SendMessageTimeout产生访问冲突 [英] SendMessageTimeout in Delphi XE4 64-bit produces Access violation
问题描述
我想在程序中广播一些环境变量的更改。因此,其他一些实用程序也可以使用新值。
当我在Windows 7平台上的32位Delphy XE4中编译下一个例程时,一切似乎都可以正常工作。
当我将Delphy切换到64位平台时,调试器会产生访问冲突。
I want to Broadcast the change of a few environment variables in my program. So a few other utilities can make use of the new values. When I compile the next routine in Delphy XE4 32-bit on a Windows 7 platform, everything seems to work fine. When I switch in Delphy to the 64-bit platform, the debugger produces an Access Violation.
有什么建议吗?
procedure BroadcastChange;
var
lParam, wParam : Integer;
Buf : Array[0..10] of Char;
aResult : PDWORD_PTR;
begin
Buf := 'Environment';
wParam := 0;
lParam := Integer(@Buf[0]);
SendMessageTimeout(HWND_BROADCAST,
WM_SETTINGCHANGE,
wParam,
lParam,
SMTO_NORMAL,
4000,
aResult );
end;
推荐答案
您需要将字符串终止为null。只需将声明切换为使用 PChar
。
You need to null terminate the string. Just switch the declaration to use a PChar
.
您还必须停止将指针转换为32位 Integer
会将64位指针截断为32位指针,这很容易导致痛苦。
You must also stop casting pointers to 32 bit Integer
which will truncate a 64 bit pointer to a 32 bit pointer, and that can easily lead to pain.
t使用aResult,传递nil。您的未初始化指针显然是一个问题。
Since you don't use aResult, pass nil. Your uninitialized pointer is obviously a problem.
procedure BroadcastChange;
begin
SendMessageTimeout(
HWND_BROADCAST,
WM_SETTINGCHANGE,
0,
LPARAM(PChar('Environment')),
SMTO_NORMAL,
4000,
nil
);
end;
这篇关于Delphi XE4 64位中的SendMessageTimeout产生访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!