Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to send a message to another application and pass a string using LPARAM

Sending Application:

C++
const UINT WM_GAURD_WINPROC = RegisterWindowMessage( "GAURD");
char name[MAX_PATH] = "E:\Net-Worm\Groups\Worm\Worm.Win32.Socks.gh";
SendMessage(HWND_BROADCAST,WM_GAURD_WINPROC,NULL,(LPARAM)name);



Receiving Application
C++
void __fastcall TForm1::WndProc( TMessage& Message)
{
if(Message.Msg == WM_GAURD_WINPROC)
{
        ShowMessage("Msg Recieved");
        char *s = (char *)(Message.LParam);
	if (strcmp(s,"E:\Net-Worm\Groups\Worm\Worm.Win32.Socks.gh")==0) {
			Scan(s);
	}
	ShowMessage(s);
}
}


The "Msg Recieved" is shown but the string that I'm trying to send seems to be nonesense. How should I pass this parameter and also how can I work with it in the receiving application?
By the way, I am working in C++ builder.
Thanks.
Posted

problem with the line :
C++
char name[MAX_PATH] = "E:\Net-Worm\Groups\Worm\Worm.Win32.Socks.gh";
//you are actually getting: 
"E:Net-WormGroupsWormWorm.Win32.Socks.gh"


Another issue is name is a local variable, I think, but not sure, your second application is not getting proper access to that address. as far I know OS wont allow you to access.

You better follow the above suggestion or you can allocate global memory.
 
Share this answer
 
Comments
lilyNaz 17-Jan-12 5:01am    
Thank you, you're right. The first solution worked out.
JackDingler 17-Jan-12 11:44am    
The registry isn't intended to be used for inter-process communications.

It works, but it adds unnecessary overhead to your application and the operating system which must take time to service the requests.

Using GlobalAlloc and passing an HGLOBAL is a better solution.
Albert Holguin 17-Jan-12 13:11pm    
+5, that's probably an issue...
I see something else here...

The '\' character is a control character in C/C++.
If you want it expressed as a string literal then use, '\\'.

Try this:

char name[MAX_PATH] = "E:\\Net-Worm\\Groups\\Worm\\Worm.Win32.Socks.gh";
 
Share this answer
 
Comments
lilyNaz 18-Jan-12 20:50pm    
Thanks,I tried this one but it didn't work out. I'm trying to pass the name using the message and not the registry since you mentioned the overhead.
JackDingler 19-Jan-12 9:25am    
Even in the registry you'll get a mangled strign if you don't constrcut it properly.

What you'll need is to use GlobalAlloc. Then lock the memory, copy the string to that memory. Unlock it, then send the HANDLE in your post message instead of the string address.

Then the receiving process will Lock the handle, to get a pointer to the string.

This is the most common technique used in clipboard applications.

This is basic stuff. It's covered in most beginner's windows books.
By seeing HWND_BROADCAST i guess you are sending this message to an another process.If the second application is a different process, you cannot send the address of a local variable in your function. Each process has its own memory space.

Possible solutions: use inter process tools like pipe/files/sockets , you can get plenty of information about these from codeproject.
 
Share this answer
 
You can use registry to pass the value
Write to registry
C++
HKEY RegKey;
RegCreateKey(HKEY_LOCAL_MACHINE,_T("Software\\Worms"),&RegKey);
RegSetValueEx(RegKey,_T("PATH_NAME"),NULL,REG_SZ,(BYTE * const)name,MAXPATH);


Read from registry

RegOpenKey(HKEY_LOCAL_MACHINE,_T("Software\\Worms"),&RegKey)
RegQueryValueEx(RegKey,_T("PATH_NAME"),NULL,NULL,(BYTE * const)name,&vRetSize)
 
Share this answer
 
Comments
lilyNaz 17-Jan-12 4:59am    
Thank you so much, It worked out.
Albert Holguin 17-Jan-12 13:04pm    
Registry for inter-process communications is a horrible idea.
JackDingler 17-Jan-12 13:16pm    
I like the way you phrased that.
Albert Holguin 17-Jan-12 13:32pm    
lol, I can't believe someone would suggest that as a viable option...
JackDingler 17-Jan-12 14:12pm    
When you're unfamiliar with programming in a given environment, you're likely to find non-standard ways of getting things done.

The part that troubles me, is that inter-process communication is a topic hidden in every beginner Windows book.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900