|
-
March 14th, 2005, 12:42 AM
#1
Read the string send through PostMessage
I have an application which has 2 components. One component is a COM dll (C++). The other is the Dot Net (C#) User Interface. I am Doing a PostMessage in the COM dll and handling this postmessage in the C# User Interface. My problem is that I am not ablr to get the string which I am sending through postmessage LPAram.
Here is the code.
COM code to send the string :
CString sRestoreProg(_T("CHKPT_RESTORE_DEVICE"));
UINT nRestoreDevice = RegisterWindowMessage(sRestoreProg);
CString name = "Kdevice ";
CString * s = new CString(name);
int i = name.GetLength();
PostMessage( hRestHWnd, nRestoreDevice , i+1, (LPARAM)s );
Here is the C# code:
int m_gettext = RegisterWindowMessageA("CHKPT_RESTORE_DEVICE");
public struct MyStruct
{
public string str;
}
In the wndproc()...
if(m.Msg == m_gettext)
{
MyStruct strLPARAM = (MyStruct) m.GetLParam(typeof(MyStruct));
}
Result : strLParam.str gives "K".. which is the first alphabet of the sent string "kdevice".
How to get the whole string put of this IntPtr?
-
March 14th, 2005, 03:45 AM
#2
Re: Read the string send through PostMessage
Firstly, PostMessage won't work. You're passing a pointer to memory in the CString which will have been destroyed when your function goes out of scope - probably before the window has time to pick the message up.
Use SendMessage instead.
Secondly have a look at the System.Runtime.InteropServices.Marshal.PtrToStringAnsi method.
Darwen.
Last edited by darwen; March 14th, 2005 at 03:47 AM.
-
March 14th, 2005, 03:53 AM
#3
Re: Read the string send through PostMessage
Hi Darwen,
I cannot use Sendmessage. My application is such that it cannot wait for the sendmessage to return. Alos I analysed the method you are suggesting. It only gives me garbage in this case.
-
March 14th, 2005, 04:11 AM
#4
Re: Read the string send through PostMessage
It probably wouldn't if you did
Code:
SendMessage( hRestHWnd, nRestoreDevice , i+1, (LPARAM)(LPCSTR)s );
and in your WndProc
Code:
string sString = Marshal.PtrToStringAnsi(m.LParam);
This is providing you're not using a unicode build.
Anyway if you can't use SendMessage you'll have to have a list of strings to be 'got' by your .NET class. Whenever it receives the message it needs to call a method on your com object to retrieve all the strings in the list, and clear the list.
Darwen.
-
March 14th, 2005, 04:20 AM
#5
Re: Read the string send through PostMessage
I tried SendMessage. But still the function returns me an empty string.
I also tried implementing an article about the same. The link is http://www.codeproject.com/string/sendcstring.asp
But that also doesn't help.
-
March 14th, 2005, 04:29 AM
#6
Re: Read the string send through PostMessage
I've just noticed that 's' is a pointer to a CString. You shouldn't be doing this, it's going to leak all over the place. Try this instead (remove the CString *s = ... line).
Code:
SendMessage( hRestHWnd, nRestoreDevice , i+1, (LPARAM)(LPCSTR)name );
You can't pass a pointer to a CString like this. .NET has no concept of the CString class.
Darwen.
-
March 14th, 2005, 04:34 AM
#7
Re: Read the string send through PostMessage
It gives the following error:
error C2440: 'type cast' : cannot convert from 'class CString' to 'const char *'
-
March 14th, 2005, 04:41 AM
#8
Re: Read the string send through PostMessage
You're using a unicode build aren't you. I said it wouldn't work in unicode builds.
You'll have to do this then instead :
Code:
// .cpp
SendMessage( hRestHWnd, nRestoreDevice , i+1, (LPARAM)(LPCWSTR)name );
Code:
// C#
string sString = Marshal.PtrToStringUnicode(m.LParam);
Darwen.
-
March 14th, 2005, 04:50 AM
#9
Re: Read the string send through PostMessage
Oh and I've come up with a way of using PostMessage. You can use task memory to do this :
Code:
WCHAR *pwcText = (WCHAR *)::CoAllocTaskMem((1 + name.GetLength()) * 2);
::wcscpy(pwcText, name);
::PostMessage(hRestHWnd, nRestoreDevice , 0, (LPARAM)pwcText);
and in .NET
Code:
string sString = Marshal.PtrToStringUnicode(m.LParam);
Marshal.FreeCoTaskMem(m.LParam);
Darwen.
-
March 14th, 2005, 04:54 AM
#10
Re: Read the string send through PostMessage
If I do this then I am getting only the first alphabet of the string.
However if I do as follows:
const char * pszMsg = "Likeme";
i=7;
PostMessage( GetRestHWnd(), GetRestMsgId(), 20, (LPARAM)pszMsg );
I am getting the string.
-
March 14th, 2005, 05:03 AM
#11
Re: Read the string send through PostMessage
How do I use "CoAllocTaskMem"
The compiler does not recognize it.
-
March 14th, 2005, 05:48 AM
#12
Re: Read the string send through PostMessage
Are you using a UNICODE build or not ? You have yet to say. I NEED this information to be able to continue.
Have you changed the .NET code to use "PtrToStringUnicode" and not "PtrToStringAnsi".
Oh and sorry it's CoTaskMemAlloc that you need to call in C++.
Darwen.
-
March 14th, 2005, 05:59 AM
#13
Re: Read the string send through PostMessage
Yes. I am using a Unicode Build.
-
March 14th, 2005, 06:10 AM
#14
Re: Read the string send through PostMessage
When I use this code I get only the first alphabet of the string and not the string.
-
March 14th, 2005, 06:54 AM
#15
Re: Read the string send through PostMessage
The code above should work - please recheck with this :
Code:
CString sString = L"Hello"; // note the 'L'
WCHAR *pwcText = (WCHAR *)::CoTaskMemAlloc((1 + sString.GetLength()) * 2);
::wcscpy(pwcText, sString);
::PostMessage(hRestHWnd, nRestoreDevice , 0, (LPARAM)pwcText);
Code:
// .NET
// NOTE PtrToStringUni here !
string sString = Marshal.PtrToStringUni(m.LParam);
Marshal.FreeCoTaskMem(m.LParam);
System.Diagnostics.Debug.WriteLine(sString);
Check the output.
I know you didn't change the .NET PtrToStringAnsi because if you had then it wouldn't have worked. The above is the correct function.
I must apologise - I'm writing all of this without compiling it assuming that you can work out what I mean.
Darwen.
Last edited by darwen; March 14th, 2005 at 06:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|