Solved

Component messaging

Posted on 1998-04-29
14
164 Views
Last Modified: 2010-04-06
I defined my own windows message.  I have a handler procedure.  It works in the form, but not in the component.

The code is as follows:

const
  WM_VVTInterfaceMessage = 1125;

  protected
    procedure WMVVTInterfaceMessage(var Msg: TMessage); message WM_VVTInterfaceMessage;

Additionally, how can I pass a string in the message?
I tried using a PChar in the lParam, but I get GPFs and Delphi exits immediately.

Bryan
0
Comment
Question by:bcrotaz
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339402
Hi Bryan

What component are you using as your base class?  (I.e. what are you inheriting from?)  I don't think TComponent has an HWND or a WndProc, so that may explain why your msg handler isn't working.

Also, what method are you currently using to pass a PChar?  How are you sending the message in lParam (PostMessage, SendMessage, Perform)?

JB
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339403
Yo,
like JimBob said, you need a window handle and a window proc. Only descendants of TWinControl have these property/method.
If your message is received in the same process, i.e. the same application, you could typecast a PChar variable in the lParam using LongInt(PString) on the sender and PChar(lParam) on the receiver side.
If your message goes out of process boundaries you cannot transfer pointers, only the direct values of wParam and lParam. To get a string through in this scenario, it is getting tricky.

Slash/d003303
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339404
Hi doo3303

You are right about TWinControl having these, but you can use a work around.  Look at TThread in Classes.pas.  In particular, look at "AllocateWindow" which is a nested function inside the procedure "AddThread".

Cheers,
JB
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1339405
OK - I used TControl, so that explains that part.

How could I send a string between processes using a message?
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1339406
I'm currently using PostMessage, cos I don't want the sender to wait for an answer.

I sent the PChar in the lParam by typecasting it to an longint.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339407
If you use PostMessage, you could have a problem.  With SendMessage you could solve your problem, because the calling procedure will then allocate AND free the string.

JB
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339408
Yo,
maybe you yould use the GetWindowText/SetWindowText API functions to work around these things. It also works across process boundaries. Note that this only works if the window text is not used for its standard purpose, like captioning.

Slash/d003303
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1339409
To get messages to your component you need to make an invisible window :

 TCDEvents = class(TComponent)
  private
    { Private declarations }
     FWindowHandle: HWND;
     procedure WndProc(var Msg: TMessage);
  protected
    { Protected declarations }
  public
    { Public declarations }
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
  published
    { Published declarations }
  end;

constructor TCDEvents.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWindowHandle := AllocateHWnd(WndProc);
end;

procedure TCDEvents.WndProc(var Msg: TMessage);
begin
 {handle the messages}
 if message then begin
 end
    else
      Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam,         Msg.lParam);
end;

destructor TCDEvents.Destroy;
begin
  DeallocateHWnd(FWindowHandle);
  inherited Destroy;
end;

Regards, ZiF.


0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1339410
Hmmm... This isn't as simple as it could be.
Is there no way of allocating memory with global permissions, so every process can access it?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339411
Hi

You can create a global shared memory area using CreateFileMapping.  For an example of this, look for MY comments under the following (unanswered) question:
http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10050013

Cheers,
JB
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1339412
Not easy?  Not much code ....

It's the way delphi does it...

Regards, Zif.
0
 
LVL 1

Accepted Solution

by:
ssite earned 100 total points
ID: 1339413
The right way to pass strings betweek apps is to use Atoms. Better yet, global atoms. Look it up in win32 help. It's pretty simple to do, but the string is limited to 255 chars.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1339414
There is a way to pass message to tControl: by calling the dispatch method eg  myControl.dispatch( userMessage);
0
 
LVL 1

Expert Comment

by:ssite
ID: 1339415
Did you use control.perform ?

maybe 1125 is used as an internal mesage in the component. Try WM_MYMESSAGE = WM_USER + 100

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now