Archived

This topic is now archived and is closed to further replies.

steg

wndproc in c#

Recommended Posts

Hi all, I''ve overriden the WndProc in my C# applications so I can accept messages from another application (it posts messages to me). Now, I don''t know how to convert the WParam and LParam to strings, I''ve tried casting, ToString etc..none work. Any help is much appreciated. Kind regards, Steve ... WndProc(Message msg) { string text = msg.WParam.ToString(); // don''t work text = (string*)msg.WParam; // nope - has to be in unsafe }

Share this post


Link to post
Share on other sites
How about

string text = (string)msg.WParam;

?

If you''re sure that WParam is a string, of course. AFAIK, WndProc receives all kinds of messages, so it''s possible that the one that you are expecting arrives after a system message whose WParam is not a string.

Cédric

Share this post


Link to post
Share on other sites
If you are sure that the WParam is a string it will most likely be a char*, (since WParam and LParam are int types).

but afaik C# doen't allow you to use pointers, so how you convince it to accept a pointer to a NULL terminated string is any ones guess..


[edit]
Come to think of it C# migth just treat char* as char arrays in wich case
string text = new string (WParam);
migth just work... ( Im on vacation so I cant really test it - it seems somewhat unlikely that it will work though, but still it *migth* joust)
[/edit]

/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''t use it...

[edited by - guppy on July 4, 2003 9:44:09 AM]

Share this post


Link to post
Share on other sites
Try something like this:

string str = System.Runtime.InteropServices.Marshal.PtrToStringAuto( msg.WParam );


quote:

C# doen''t allow you to use pointers


This is 100% wrong.



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
quote:

C# doen''t allow you to use pointers


This is 100% wrong.



huh? I was taught that to manipulate pointers in C# ( and managed C++ ) you had to switch to unmanagede mode.

Offcourse It woudn''t be the first time one of the professors were wrong


/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
tnx - will read it when I can find the time (that is when I get back from vacation)

/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
Thanks all,

I think Arild could be correct, I tried this the other day but used PtrToStringAnsi not Auto, will try this tomorrow.

Many thanks all.

Steve

PS - using pointers in C# - you can do, just use unsafe keyword and compile with /unsafe.

Share this post


Link to post
Share on other sites
quote:
Original post by steg
I think Arild could be correct, I tried this the other day but used PtrToStringAnsi not Auto, will try this tomorrow.

If you''re on an NT-based OS(which is very likely, since neither the SDK nor VS.NET are supported on anything else), the WndProc params would most likely be a Unicode string, not Ansi. Out of curiosity - what happened when you tried PtrToStringAnsi?

Which message are you handling anyway? Maybe someone can suggest a managed way to accomplish the same goal.



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites
Arild - the messages are custom ones, a colleague has written a low level communications library which posts messages to my application (a gui front end), usual way of posting using SendMessage api.
Example :

pWnd->SendMessage(WM_SETINFORMATION, (WParam)"Set ignition to off",NULL);

where pWnd points to my app window (we normally use the thread id for this and not the handle), also sometimes send an ATOM across. Then in my wndproc in the c# app I capture this message and process accordingly.

Using the PtrToStringAnsi method I got something like "4852670" in the WParam ? Will try the auto later today.

Kind regards,
Steve


[edited by - steg on July 5, 2003 8:34:34 AM]

Share this post


Link to post
Share on other sites
I''m getting fed up with this now, I still cannot convert the WParam to a string. I really thought the code below would work :

string str = System.Runtime.InteropServices.Marshal.PtrToStringAuto( msg.WParam );

But just get a load of null characters ?

Has anybody converted WParam to a string, I know that we are passing a string into the WParam from our C++ application, surely you can pass more than integers into the WParam and LParam ?

Anybody any more ideas ?

Kind regards,
Steve

Share this post


Link to post
Share on other sites
You could try writing a C++ managed unsafe function to read it, and calling it from your C# program. At least, you'd be sure that the message is indeed there.
EDIT: Sorry, I just realized that you had done this in C# already. In fact, now that I look at my first post, sorry for posting at all.

[edited by - cedricl on July 6, 2003 1:42:26 PM]

Share this post


Link to post
Share on other sites
It seems like the problem is that the string is in a seperate application''s address space, and *that* is why you can''t get to it.

So, why are you passing strings? If you know what types of information you are going to be getting, why not just pass int''s around?

Share this post


Link to post
Share on other sites
quote:
Original post by RomSteady
It seems like the problem is that the string is in a seperate application''s address space, and *that* is why you can''t get to it.

I concur. You might want to try this:
quote:

WM_COPYDATA Message

--------------------------------------------------------------------------------

An application sends the WM_COPYDATA message to pass data to another application.


Cédric

Share this post


Link to post
Share on other sites
Thanks all,

RomSteady - I guess you could be correct, reason passing strings is that the strings contain visual text information to be displayed.

Cedricl - will try the copy data method.

Kind regards,
Steve

Share this post


Link to post
Share on other sites