|
-
May 4th, 2004, 10:00 AM
#1
How to get string thru lParam in window message
Hi All,
I have two dialog based applications in which first dialog has implemented a user message, with wParam and lParam as parameter.
Now from other dialog based application I want to send the above user message to get a string value. (It is somewhat similar to WM_GETTEXT message)
I have implemented code like this
Application 2:-
Code:
void SomeFunction()
{
CWnd* pWnd = FindWindow(); // Find the Application 1's Window (i.e. CUsrSNDlg)
TCHAR pname[100];
memset(pname, '\0', sizeof(TCHAR)*100);
pWnd->SendMessage(WM_USR_GETNAME, 0, (LPARAM)(LPCTSTR)pname);
}
Application 1:-
Code:
LRESULT CUsrSNDlg::OnUsrGetName(WPARAM wParam, LPARAM lParam)
{
::CopyMemory((LPVOID)lParam, m_UsrName, 100); // Here copy fails as this is a address of other appln's address space
return TRUE;
}
Here I want to use lParam as an out parameter to send a string value back.
Can anyone please tell me that how can I get string value in lParam?
Regards,
Tushar
-
May 4th, 2004, 10:05 AM
#2
Use WM_COPYDATA instead of user message.
Search this forum and you'll find examples.
-
May 4th, 2004, 10:10 AM
#3
Use WM_COPYDATA and the COPYDATASTRUCT structure to do this.
Here's sample code form MSDN:
Code:
// ************ Globals ************
//
#define MYDISPLAY 1
typedef struct tagMYREC
{
char s1[80];
char s2[80];
DWORD n;
} MYREC;
COPYDATASTRUCT MyCDS;
MYREC MyRec;
HRESULT hResult;
BOOL CALLBACK InfoDlgProc( HWND, UINT, WPARAM, LPARAM );
// ************ Code fragment ****************
// Get data from user. InfoDlgProc stores the information in MyRec.
//
DialogBox( ghInstance, "InfoDlg", hWnd, (DLGPROC) InfoDlgProc );
//
// Copy data into structure to be passed via WM_COPYDATA.
// Also, we assume that truncation of the data is acceptable.
//
hResult = StringCbCopy( MyRec.s1, sizeof(MyRec.s1), szFirstName );
if (hResult != S_OK)
return False;
hResult = StringCbCopy( MyRec.s2, sizeof(MyRec.s2), szLastName );
if (hResult != S_OK)
return False;
MyRec.n = nAge;
//
// Fill the COPYDATA structure
//
MyCDS.dwData = MYPRINT; // function identifier
MyCDS.cbData = sizeof( MyRec ); // size of data
MyCDS.lpData = &MyRec; // data structure
//
// Call function, passing data in &MyCDS
//
hwDispatch = FindWindow( "Disp32Class", "Hidden Window" );
if( hwDispatch != NULL )
SendMessage( hwDispatch,
WM_COPYDATA,
(WPARAM)(HWND) hWnd,
(LPARAM) (LPVOID) &MyCDS );
else
MessageBox( hWnd, "Can't send WM_COPYDATA", "MyApp", MB_OK );
The receiving application has a hidden window which receives the information from WM_COPYDATA and displays it to the user.
// ************ Globals ************
//
#define MYDISPLAY 1
typedef struct tagMYREC
{
char s1[80];
char s2[80];
DWORD n;
} MYREC;
PCOPYDATASTRUCT pMyCDS;
void WINAPI MyDisplay( LPSTR, LPSTR, DWORD );
//
// ************ Code fragment ****************
//
case WM_COPYDATA:
pMyCDS = (PCOPYDATASTRUCT) lParam;
switch( pMyCDS->dwData )
{
case MYDISPLAY:
MyDisplay( (LPSTR) ((MYREC *)(pMyCDS->lpData))->s1,
(LPSTR) ((MYREC *)(pMyCDS->lpData))->s2,
(DWORD) ((MYREC *)(pMyCDS->lpData))->n );
}
break;
-
May 4th, 2004, 10:26 AM
#4
Wow, such a nice example from MSDN!!! 
Anyhow, we can observe that OP already uses MFC.
So...
Code:
BOOL CDlgSender::Send(HWND hWndReceiver, LPCTSTR pszText)
{
COPYDATASTRUCT cds;
cds.lpData = (LPVOID)pszText;
cds.cbData = (_tcslen( pszText ) + 1) * sizeof(TCHAR);
cds.dwData = (DWORD)m_hWnd; //you may put anything else here
BOOL bRet = ::SendMessage( hWndReceiver,
WM_COPYDATA,
(WPARAM)m_hWnd,
(LPARAM)&cds );
return bRet;
}
BOOL CDlgReceiver::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCDS)
{
CString strText = (LPCTSTR)pCDS->lpData;
// ...enjoy...
return TRUE; // handled
}
...it's a little bit easier, isn't it?
Last edited by ovidiucucu; May 4th, 2004 at 10:49 AM.
-
May 5th, 2004, 12:49 AM
#5
Thanks for the answer,
I implemented like that, but I need the data on the sender's side, which I am not getting.
As I mentioned you guys earlier that I want to get data and not to send.
Application 2:-
Code:
void SomeFunction() // This can be any function under any window class in my appln 2
{
CWnd* pWnd = FindWindow(); // Find the Application 1's Window (i.e. CUsrSNDlg)
TCHAR pname[100];
memset(pname, '\0', sizeof(TCHAR)*100);
COPYDATASTRUCT cds;
cds.lpData = (LPVOID)pname;
cds.cbData = 100 * sizeof(TCHAR);
cds.dwData = (DWORD)pWnd->m_hWnd; //you may put anything else here
BOOL bRet = ::SendMessage(pWnd->m_hWnd , WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds );
// Here I want "pname" to have a valid user name copied in applicn 1
// But pname will be still blank here
}
Application 1:-
Code:
LRESULT CUsrSNDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
lstrcpy((LPTSTR)pCopyDataStruct->lpData, m_UsrName); // everything goes fine here (lpData will have m_UsrName copied correctly)
// But this will not reflect on the appln 2's side
return TRUE;
}
Please suggest me the correct way. Am I missing something in my implementation?
Regards,
Tushar
-
May 5th, 2004, 01:57 AM
#6
Aha...
Just reverse: send m_UsrName using WM_COPYDATA from Application 1 to Application 2.
Is there any problem to do like that?
-
May 5th, 2004, 03:42 AM
#7
Ya.. It will be a problem, as I mentioned earliar the sender can be any window or may be a windowless object (a command line interface). So the reverse way is not possible.
One thing I want to know how WM_GETTEXT message works?
how he returns the window text thru lParam?
Thanks n Regards,
Tushar
-
May 5th, 2004, 10:24 AM
#8
Well, in this case use, for example, a pipe.
Read Interprocess Communications chapter in MSDN.
How WM_GETTEXT works? I don't know. Ask MS-team or deeply Google search 
Keep ahead!
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
|