Search code examples
delphiwinapidelphi-11-alexandria

CreateWindowEx() Returns 0


I know that there are many posts regarding my issue, I also read this post Delphi 11.2: CreateWindowEx fails thread on x64 but I am not able to understand what exactly I should do to overcome this problem. I am working on Cromis.DirectoryMonitoring Cromis.Utils.pas https://github.com/CPsoftBE/BackupOfCromis, below code CreateWindowEx() Returns 0 only for Delphi 11.2 / 64Bit.

Result := CreateWindowEx(WS_EX_TOOLWINDOW, CTSHiddenWindowName, '', WS_POPUP,0, 0, 0, 0, 0, 0, HInstance, nil);

Solution

  • One issue I see in Cromis.Utils.pas is that the declaration of TSClassWndProc() is wrong for a 64bit build. It is declaring as this:

    function TSClassWndProc(Window: HWND; Message, WParam, LParam: longint): longint; stdcall;
    

    Longint is the wrong type to use. The function should be declared as this instead:

    function TSClassWndProc(Window: HWND; Message: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
    

    The size of WPARAM, LPARAM, and LRESULT vary between 32bit and 64bit builds. Using the wrong types can cause undefined behavior.


    On a side note, there is no need to use an {$IFDEF} to conditionally call (Get|Set)WindowLongPtr() vs (Get|Set)WindowLong() based on build target. (Get|Set)WindowLongPtr() work in both 32bit and 64bit builds, so use them unconditionally.