Search code examples
delphidllwindows-messages

Getting error id: 0 from Windows method: FindWindow


im trying to send a Windows message to my Delphi application, but im having problems with the FindWindow method: im getting error id of 0 from the GetLastError method. Im running Vista and from what ive read this error is common in XP or earlier versions, but should work fine in Vista or Win 7 (maybe i misunderstood ?).

This is the code im using and its in a Delphi DLL file, written in Delphi 5 :

procedure SendData(const copyDataStruct: TCopyDataStruct) ;
var
   receiverHandle : THandle;
   res : integer;
begin
   receiverHandle := FindWindow(PChar('TMainForm'),PChar('MainForm')) ;
   if receiverHandle = 0 then
   begin
   ShowMessage(format('Error %x finding MainForm',
    [GetLastError()]));
     Exit;
   end;

   res := SendMessage(receiverHandle, WM_COPYDATA, Integer(receiverHandle), Integer(@copyDataStruct)) ;
end;

Solution

  • According to the system error codes list, error 0 means "ERROR_SUCCESS".

    Could it be that your Window is of class TMainWindow, but has an empty Caption?

    See the remarks for GetWindowText that is internally used by FindWindow when the lpWindowName parameter is non-null (which is the case: you pass MainWindow there).

    --jeroen