Click to See Complete Forum and Search --> : Posting string using postmessage() function
priyank9
September 18th, 2006, 11:24 AM
Hi,
In my application I need to post a string using postmessage() function. Here my string is of type LPCSTR without null-termination. I post this string using WPARAM parameter of postmessage() function, but on the receiver side I get nothing. Can I know how can I post this string using postmessage() function?
Thanks,
Priyank
wildfrog
September 18th, 2006, 11:31 AM
You can post string pointers, you just have to make sure that the string/pointer is valid when the message is eventually handled. So, don't post local strings, strings located on the stack etc., that my be deleted before the message is handled.
- petter
MikeAThon
September 18th, 2006, 11:43 AM
As Petter says, the typical reason why your receiver "gets nothing" is that you have posted a pointer to a string that no longer exists by the time the receiver handles the message. The string was probably created on the stack in the function that called PostMessage, and by the time the message is handled, the function has completed and all stack variables have been destroyed.
One solution is to allocate memory for the string in the function that calls PostMessage, and de-allocate it in the receiver:
// ... in the function that calls PostMessage
char* pStr = new char[256];
// ... insert characters into pStr
PostMessage( hWnd, WM_CUSTOM_MESSAGE, (WPARAM)pStr, 0L );
// ... in the receiver function that handles the message
char* pStr = (char*) wParam;
// ... use the string
delete [] pStr;
Mike
priyank9
September 19th, 2006, 07:32 AM
// ... in the function that calls PostMessage
char* pStr = new char[256];
// ... insert characters into pStr
PostMessage( hWnd, WM_CUSTOM_MESSAGE, (WPARAM)pStr, 0L );
// ... in the receiver function that handles the message
char* pStr = (char*) wParam;
// ... use the string
delete [] pStr;
Thanks for your reply. I tried this one also but it is not working. Actually I made an API Hook DLL to hook any application for ExtTextOut() winapi. Now when my DLL get this api call I need to process string in my other application. But I am not able to send or post this string to my application. Any suggestion how can I get this string in my application?
Thanks,
Priyank
wildfrog
September 19th, 2006, 07:55 AM
Pointers (to strings etc) cannot be passed between two applications.
To send data between different applications you can use the WM_DATACOPY message.
See Data Copy (http://windowssdk.msdn.microsoft.com/en-us/library/ms648710.aspx) for an example.
- petter
VladimirF
September 19th, 2006, 10:05 AM
To send data between different applications you can use the WM_DATACOPY message.
See Data Copy (http://windowssdk.msdn.microsoft.com/en-us/library/ms648710.aspx) for an example.
- petter
WM_COPYDATA
wildfrog
September 19th, 2006, 10:29 AM
WM_COPYDATA :thumb:
- petter
MikeAThon
September 19th, 2006, 11:06 AM
For other forms of interprocess communication (IPC), i.e., other than WM_COPYDATA, see "Interprocess Communications" at http://windowssdk.msdn.microsoft.com/en-us/library/ms690478.aspx , which serves as a jumping-off point for describing the following IPC mechanisms:
Clipboard
COM
Data Copy (i.e., WM_COPYDATA)
DDE
File Mapping
Mailslots
Pipes
RPC
Windows Sockets
Mike
priyank9
September 20th, 2006, 04:44 AM
Thanks Everybody,
cheers,
Priyank
priyank9
September 20th, 2006, 07:26 AM
Thanks for your reply.
Now I am using WM_COPYDATA to send string to my other application. I also want to send 2 integer values with this string. So, I made a structure of 1 string and 2 integers. But on the other side I am able to get 2 integer values but not string. I get blank string on that side. Here I am sending my code.
Sender application
struct MYDATASTRUCT
{
LPCSTR myString;
int myX;
int myY;
};
MYDATASTRUCT myStringData;
myStringData.myX = X;
myStringData.myY = Y;
myStringData.myString = lpString;
COPYDATASTRUCT myData;
myData.dwData = 0;
myData.cbData = sizeof(myStringData);
myData.lpData = &myStringData;
::SendMessage(g_mainHwnd,WM_COPYDATA,NULL,(LPARAM)&myData);
Receiver application
PCOPYDATASTRUCT myData = (PCOPYDATASTRUCT)lParam;
MYDATASTRUCT* myStringData = (MYDATASTRUCT*)myData->lpData;
int X = myStringData->myX;
int Y = myStringData->myY;
LPCSTR lpString = myStringData->myString;
Now when I am sending this string directly without structure and integer value I am able to get that string on receiver application.
Can anyone find what is the problem with structure code?
Thanks,
Priyank
VladimirF
September 20th, 2006, 10:33 AM
Can anyone find what is the problem with structure code?
Your struct doesn't contain the string, ONLY a pointer to it.
MikeAThon
September 20th, 2006, 11:40 AM
Also, when sending to other applications, it's generally recommended to use ::SendMessageTimeout(), to avoid the problems caused if the other application is "hung".
If the other application is "hung", then SendMessage() will never return, and your application will become hung too. SendMessageTimeout() avoids this; remember to check the return value for failure.
Mike
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.