Re: String between unmanaged and managed



Directly from Help on PostMessage:

"The system only does marshalling for system messages (those in the range 0
to (WM_USER-1)). To send other messages (those >= WM_USER) to another
process, you must do custom marshalling."

Also, PostMessage doesn't wait, so it returns and your function could easily
exit before the receiver copies off the data (it's quite likely to happen in
fact, and would give you "random" data and nice access violations at times).

You should probably be using SendMessage with WM_COPYDATA.

-Chris

<andrerus@xxxxxxxxx> wrote in message
news:1159288322.637180.301570@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Here is the code:

wchar_t* textitem = new wchar_t[256];
int textlen = SendMessage((HWND) lParam, TB_GETBUTTONTEXT, (WPARAM)
LOWORD(wParam), (LPARAM) textitem);

if(textlen != -1)
{
if(wcsncmp(textitem, LPCSTR_2_LPCWSTR("play >"), 4) == 0)
PostMessage(hwndinoc, 20000, (WPARAM) textitem, textlen);
else
PostMessage(hwndinoc, 21000, (WPARAM) textitem, textlen);
}
else PostMessage(hwndinoc, 22000, (WPARAM) textitem, textlen);

This code is included into the WM_COMMAND case of the wndProc of the
mainpage.
The hwndinoc var is global and it is defined during the inoculation of
Wndproc.
As you can see, the mainpage so notifies the event to my appl
(hwndinoc), using a specified msg value. "play >" is the string
actually included into the button I'm trying to get. My appl always
receive code 21000.

Could you explain me what am I forgotting?
Considers that I haven't sourcecode of the application owning the page
and the button, but I inoculate to it the new wndproc from my appl.

Thanks,

Andrea

<ctacke/> ha scritto:

So the problem is in how you get the string. Show us that code.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



<andrerus@xxxxxxxxx> wrote in message
news:1159187097.455954.323890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
What are you trying to do? Get some text from a button and send it to
another process?

I'm trying to do just that

Just get and pass the string.

I have tried, the problem is that I always receive an invalid string,
even if TB_GETBUTTONTEXT return the correct length, so I pass to the
next process an invalid and always random string (I'm sure that the
problem is a related to the text format, non at all, because of
randomness on vaues received.

Andrea

<ctacke/> ha scritto:

Why not back up and tell us the big picture. What are you trying to
do?
Get some text from a button and send it to another process? If that's
the
case, I have no idea why you're trying any of these crazy, completely
invalid steps. Just get and pass the string.

-Chris


<andrerus@xxxxxxxxx> wrote in message
news:1159015719.315767.112980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm not able to use debugger, because the function that contain the
code will be executed by a process different from test application
(don't forget that it's a DLL, and it is used to inoculate wndproc
to
another application), and breakpoints in that portion of code are
ignored. A test that I made was instead to post different messages
if
received text was equal or not to a constant string, (I tried using
LPWSTR and LPSTR): the received string is always different from
constant one. Also the length is different from expected: the
sendmessage result is 6, that is correct, but the length of
receiving
buffer is 1 or 2, always random, this means for me that the button
responds reading wrong memory (fact that could explain the random
value), and that the passing from unmanaged to managed is correct.
I tried also using TB_GETBUTTONINFO, but nothing change.

Any other idea?

Thank you for your precious help,

Andrea


Paul G. Tobey [eMVP] ha scritto:

Look at the string (as char[]), in the debugger. If every other
byte
is
a
zero, it's Unicode. If that's the case, don't do anything to it;
just
pass
it to PostMessage.

You're doing crazy stuff there. Why would you shift the first
value
by
16
bits? Let's fix the real problem, which is at the point where the
string
is
extracted from the toolbar, then worry about the next step.

Paul T.

<andrerus@xxxxxxxxx> wrote in message
news:1158942075.713385.154630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I supposed that, but the documentation of TB_GETBUTTONTEXT talk
about
LPSTR, not LPWSTR.

However, I also tried to not convert it, and to declare text (the
buffer) as LPWSTR, but nothing changes.
The last test I make was to declare text as wchar_t*, and to post
the
first four chars as int
obtaining WPARAM as (text[0]<<16) + text[1] and so on with
LPARAM.
Reading them from managed side I obtain different and random
values,
why this?

Andrea

Paul G. Tobey [eMVP] ha scritto:

Getting the text for a button, a window, or practically anything
else
in
Windows CE returns a Unicode string, *not* an ASCII string. You
need
to
learn about what that means. Then you'll see that your message
sent
to
the
toolbar window returns a Unicode string, which you then damage
severely
by
running it through your conversion function before using
PostMessage
to
send
it.

Paul T.

<andrerus@xxxxxxxxx> wrote in message
news:1158939440.935261.261600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
Some were I found this function:

LPCWSTR LPCSTR_2_LPCWSTR(const char* inString)
{
int len = strlen(inString) + 1;
wchar_t *outString = new wchar_t[len];
mbstowcs(outString, inString, len);
return (LPCWSTR) outString;
}

Now I have to post a message containing text (as wParam) from
an
unmanaged part to a managed one.
I do something like this:

#unmanaged1
PostMessage(destHwnd, myMsg, (WPARAM)
LPCSTR_2_LPCWSTR("string"),
0);

#unmanaged2
...
LPSTR text = new char[256];
SendMessage(toolbarHwnd, TM_GETBUTTONTEXT, wID, text);
PostMessage(destHwnd, myMsg, (WPARAM) LPCSTR_2_LPCWSTR(text),
0);


#managed
...
string s = Marshal.PtrToStrUni(m.WParam);

In case unmanaged1 I always receive correct string, instead in
case
unmanged2 never.

I'm becoming crazy!!!

Andrea







.