Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendMessageTimeout in Delphi XE4 64-bit produces Access violation

Tags:

delphi

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.

Any suggestions?


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;
like image 285
cvz Avatar asked Nov 06 '25 13:11

cvz


2 Answers

You need to null terminate the string. Just switch the declaration to use a PChar.

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.

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;
like image 188
David Heffernan Avatar answered Nov 08 '25 12:11

David Heffernan


Thanks. This helps, but what finally did the trick was initializing aResult to nil. Actually the next code works:

procedure BroadcastChange;
    var
        aResult          : PDWORD_PTR;            
    begin
        aResult  := nil;  {<---}
        SendMessageTimeout(HWND_BROADCAST ,
                           WM_SETTINGCHANGE ,
                           0, 
                           LPARAM(PChar('Environment')), 
                           SMTO_NORMAL  ,
                           4000,
                           aResult
                           );
    end;
like image 40
cvz Avatar answered Nov 08 '25 11:11

cvz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!