2

I would like to send / receive a string from 2 CONSOLE Applications (2 different PIDs with no Forms!). I saw that I need the declare this in a class. Is it possible to do this without having a class at all in a console application? If so, how I can do that?

Thanks for your help.

Ben
  • 3,380
  • 2
  • 44
  • 98
  • 1
    Just have a look at WM from WM_COPYDATA, it's build from WindowMessage. So it is in fact a Message sent to a Window. But not only a TForm is a Window. Nearly everything you can see on your display is based on a window. Thats why MS called their product Windows ;o) – Sir Rufo Nov 15 '12 at 08:19

1 Answers1

11

You can't use WM_COPYDATA without a window to send it to. If you don't use classes, you will have to use the Win32 API RegisterClass() and CreateWindow/Ex() functions directly to allocate a window and provide your own stand-alone function for its message handler procedure.

But why not use classes? Then you can leverage the RTL's built-in message handler systems. At the very least, you can use AllocateHWnd() with a static class method so you don't have to instantiate a class object at runtime, eg:

type
  TWindowMessages = class
  public
    class procedure WndProc(var Message: TMessage);
  end;

class procedure TWindowMessages.WndProc(var Message: TMessage);
begin
  //...
end;

var
  Wnd: HWND;

Wnd := AllocateHWnd(TWindowMessages.WndProc);
// pump the message queue for new messages as needed...
DeallocateHWnd(Wnd);

If this does not suit your needs, then you should consider a different IPC mechanism that does not rely on windows, such as named pipes, mailslots, sockets, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Minor nit-pick. That's not a static class method. A static class method has no implicit Self parameter. You make a static class method by adding static to the end of the declaration. That's a class method. – David Heffernan Nov 15 '12 at 06:26