Anyone good with Delphi & Win32?

Started by
3 comments, last by Vortez 10 years, 11 months ago

I have some code in delphi that clicks a button in a window.The way it does that: it gets the hwnd of the window,then it enums all the childs,if any of them has text with "ok"(the button i want),it sends some messages.Full code:


function EnumChildProc_Register(AHWND: HWND; AParam: LParam): Boolean; stdcall;
const
  BUFFER_SIZE = 4096;
var
  text  : array[0..BUFFER_SIZE - 1] of Char;
  pparam: pointer;
  res   : DWORD;
begin
  pparam := pointer(AParam);
  SendMessageTimeout(AHWND, WM_GETTEXT, BUFFER_SIZE, NativeUint(@text[0]), SMTO_ABORTIFHUNG, 50, @res);
  if String(text) = 'OK' then
  begin
    PostMessage(AHWND, WM_KEYDOWN, VK_RETURN, 0);
    Sleep(10);
    PostMessage(AHWND, WM_KEYUP, VK_RETURN, 0);
    Boolean(pparam^) := FALSE;
  end;

  result := Boolean(pparam^);
end;

The big problem is,those 2 post messages do nothing in win32.I tested the code,and the button is not clicked.How could it work in delphi,and in c++ not?

Advertisement

Cutting pasting from the last time you asked this question:

In the case of keyboard events more happens when you type than just a WM_* msg popping up in some window's message queue and it is impossible for these events to arise in a non-foreground window under normal use, so merely fabricating windows messages usually doesn't do what you want it to do. The way to do this that actually works for keyboard events is to make the target window foreground and use SendInput(...) as someone said above; although, I don't think it is possible to foreground an arbitrary window across processes the way that it was years ago ... so not sure.

Still...I'm amaazed at the fact that some code can do a thing in delphi,and the same code,do nothing in c++...

What is your C++ version of this code? The same code should produce the same results, so it must be different somewhere.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

From past experience i can tell that SendMessage is more reliable than PostMessage in delphi, although i have no idea why... give it a try.

This topic is closed to new replies.

Advertisement