SendMessageTimeoutW requires casting string to uint?

BoQsc vaidas.boqsc at gmail.com
Tue Jul 9 15:38:12 UTC 2019


On Tuesday, 9 July 2019 at 13:10:57 UTC, a11e99z wrote:
> On Tuesday, 9 July 2019 at 12:14:38 UTC, BoQsc wrote:
>> On Tuesday, 9 July 2019 at 11:11:53 UTC, Dejan Lekic wrote:
>>     auto result = SendMessageTimeoutW(
>>         HWND_BROADCAST,
>>         WM_SETTINGCHANGE,
>>         0,
>>         envi.toUTF16z,
>>         SMTO_ABORTIFHUNG,
>>         timeout,
>>         null
>>     );
>>> C:\Users\User\Desktop>rdmd ref.d
>>> ref.d(19): Error: function 
>>> `core.sys.windows.winuser.SendMessageTimeoutW(void*, uint, 
>>> uint, int, uint, uint, uint*)` is not callable using argument 
>>> types `(void*, int, int, const(wchar)*, int, uint, 
>>> typeof(null))`
>>> ref.d(19):        cannot pass argument 
>>> `toUTF16z(cast(const(char)[])envi)` of type `const(wchar)*` 
>>> to parameter `int`
>>> Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", "-o-", 
>>> "ref.d", "-I."]
>
> // add cast to LPARAM for SendMessageTimeoutW
>           cast( LPARAM )envi.toUTF16z,
>
> // import lib too somewhere out of main() or in command line
> version(Windows) pragma(lib, "user32.lib");

You are absolutely right.

Here is a working example code on how to broadcast environment 
variables changes
from D language to Windows XP, 7, 8, 10 systems, if anyone 
searches for it.

To launch the code:
> rdmd broadcastSignal.d


broadcastSignal.d

import std.utf;
version(Windows) pragma(lib, "user32.lib");

import core.sys.windows.windows : SendMessageTimeoutW;
import core.sys.windows.windows : GetLastError;
import core.sys.windows.windows : ERROR_SUCCESS;
import core.sys.windows.windows : SMTO_ABORTIFHUNG;
import core.sys.windows.windows : LPARAM;
import core.sys.windows.windows : HWND_BROADCAST;
import core.sys.windows.windows : WM_SETTINGCHANGE;



  void main(){
      broadcastSettingChange();

  }

  /**
     Broadcasts a signal to update All Environment variables On 
Windows XP, 7, 8, 10 systems.
     Mostly used update changes to Path environment variable.
     After broadcast of this signal, reopen applications, for 
changes to be visible.

     Date: 07/09/2019
     License: use freely for any purpose

     Params:
         addressBroadcast = "Policy"      When the system sends 
this message as a result of a change in policy settings, this 
parameter points to this string.
                            "intl"        When the system sends 
this message as a result of a change in locale settings, this 
parameter points to this string.
                            "Environment" To effect a change in 
the environment variables for the system or the user, broadcast 
this message with lParam set to this string

     Returns: does not return
     See_Also:
         
https://docs.microsoft.com/lt-lt/windows/win32/api/winuser/nf-winuser-sendmessagetimeouta
         
https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange
  */
  void broadcastSettingChange (string 
addressBroadcast="Environment", uint timeout=1)
  {
     void* hWnd = HWND_BROADCAST;
     uint  Msg = WM_SETTINGCHANGE;
     uint  wParam = 0;
     int   lParam = cast( LPARAM )addressBroadcast.toUTF16z;
     uint  fuFlags = SMTO_ABORTIFHUNG;
     uint  uTimeout = timeout;
     uint* lpdwResult = null;

     int result = SendMessageTimeoutW(
         hWnd,
         Msg,
         wParam,
         lParam,
         SMTO_ABORTIFHUNG,
         uTimeout,
         lpdwResult
     );

     if(result == 0)
     {
         auto errCode = GetLastError();
     }
   }


More information about the Digitalmars-d-learn mailing list