1

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);
  • 3
    Does calling [`GetLastError`](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror) produce any useful insight? – IInspectable Apr 11 '23 at 15:10
  • Most likely [`RegisterClass( CTSHiddenWindowName )`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerclassa) was not called, which is needed for [`CreateWindowEx()`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexa) as the manual explains. – AmigoJack Apr 11 '23 at 16:12
  • @AmigoJack the code in question is calling `RegisterClass()`. Another possibility is that the registered `WndProc` for the `CTSHiddenWindowName` class has a bug in it that is causing the window creation to be aborted. Please [edit] your question to include a [mcve] demonstrating the failure in action. – Remy Lebeau Apr 11 '23 at 17:19

1 Answers1

1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Replacing as advised ...TSClassWndProc(Window: HWND; .... resolved the problem. Earlier Result := CreateWindowEx( ... was returning 0 without throwing any exception, now it returns valid result. Thank You. – AllanFernandes Apr 12 '23 at 04:06