From: Piranha on
I have 2 applications, exchanging data via WM_COPYDATA.

Application 1:

string tosend = "Some text";
COPYDATASTRUCT cd;
cd.dwData = SomeValue;
cd.cbData = static_cast<DWORD>(tosend.size()) + 1;
cd.lpData = const_cast<char *>(tosend.c_str());
int result = SendMessage(FindWindow(Application2,0),WM_COPYDATA,
(WPARAM)hwnd,(LPARAM)&cd);

Application 2:

case WM_COPYDATA:
PCOPYDATASTRUCT cd = (PCOPYDATASTRUCT) lParam;
int result = ProcessData(cd.dwData,cd->lpData);
return result;
break;

What I would like to do is to return a string, like this:

Application 1:

string tosend = "sometext";
COPYDATASTRUCT cd;
cd.dwData = SomeValue;
cd.cbData = static_cast<DWORD>(tosend.size()) + 1;
cd.lpData = const_cast<char *>(tosend.c_str());
string result = SendMessage(FindWindow(Application2,0),WM_COPYDATA,
(WPARAM)hwnd,(LPARAM)&cd);

Application 2:

case WM_COPYDATA:
PCOPYDATASTRUCT cd = (PCOPYDATASTRUCT) lParam;
string result = ProcessData(cd.dwData,cd->lpData);
return result;
break;

I know it doesn´t work like this, but maybe someone here can tell me,
how I can return a string or a pointer to a string?
From: ScottMcP [MVP] on
On Feb 12, 4:00 pm, Piranha <eu_pira...(a)gmx.net> wrote:
> I have 2 applications, exchanging data via WM_COPYDATA.
>
> Application 1:
>
> string tosend = "Some text";
> COPYDATASTRUCT cd;
> cd.dwData = SomeValue;
> cd.cbData = static_cast<DWORD>(tosend.size()) + 1;
> cd.lpData = const_cast<char *>(tosend.c_str());
> int result = SendMessage(FindWindow(Application2,0),WM_COPYDATA,
> (WPARAM)hwnd,(LPARAM)&cd);
>
> Application 2:
>
> case WM_COPYDATA:
> PCOPYDATASTRUCT cd = (PCOPYDATASTRUCT) lParam;
> int result = ProcessData(cd.dwData,cd->lpData);
> return result;
> break;
>
> What I would like to do is to return a string, like this:
>
> Application 1:
>
> string tosend = "sometext";
> COPYDATASTRUCT cd;
> cd.dwData = SomeValue;
> cd.cbData = static_cast<DWORD>(tosend.size()) + 1;
> cd.lpData = const_cast<char *>(tosend.c_str());
> string result = SendMessage(FindWindow(Application2,0),WM_COPYDATA,
> (WPARAM)hwnd,(LPARAM)&cd);
>
> Application 2:
>
> case WM_COPYDATA:
> PCOPYDATASTRUCT cd = (PCOPYDATASTRUCT) lParam;
> string result = ProcessData(cd.dwData,cd->lpData);
> return result;
> break;
>
> I know it doesn´t work like this, but maybe someone here can tell me,
> how I can return a string or a pointer to a string?

When application 2 receives the message it can call SendMessage with
WM_COPYDATA and application 1 will receive it before its own first
call returns. (I think. I haven't tried it.)

On the other hand, what you're trying to invent is what COM does. If
you are willing to change the interface to COM you will be able to
return a string from an interprocess call.
From: Steph on
"Piranha" <eu_piranha(a)gmx.net> a �crit dans le message de news:
9a4642a8-8d0f-44b2-b134-cca78d402a2e(a)d27g2000yqn.googlegroups.com...

>case WM_COPYDATA:
>PCOPYDATASTRUCT cd = (PCOPYDATASTRUCT) lParam;
>string result = ProcessData(cd.dwData,cd->lpData);
>return result;
>break;

>I know it doesn�t work like this, but maybe someone here can tell me,
>how I can return a string or a pointer to a string?

Return to where ?
The WindowProc returns an integer , (TRUE or FALSE usually) !
You already have the result string




From: r_z_aret on
On Fri, 12 Feb 2010 13:00:52 -0800 (PST), Piranha <eu_piranha(a)gmx.net>
wrote:

>I have 2 applications, exchanging data via WM_COPYDATA.
>
>Application 1:
>
>string tosend = "Some text";
>COPYDATASTRUCT cd;
>cd.dwData = SomeValue;
>cd.cbData = static_cast<DWORD>(tosend.size()) + 1;
>cd.lpData = const_cast<char *>(tosend.c_str());
>int result = SendMessage(FindWindow(Application2,0),WM_COPYDATA,
>(WPARAM)hwnd,(LPARAM)&cd);

If you are really sending a message (with SendMessage) , then you can
use WM_CHAR to send a string. WM_COPYDATA is useful when posting a
message (using PostMessage), because of address scope/validity issues.
Using SendMessage between applications is tricky, because the calling
applications will hang until the called application returns. Using
SendMessage in Application 2 while it is responding to a message
_sent_ (rather than posted) from Application 1 will surely hang both
applications, or at least the threads processing those messages.

Possibly better approach:
Use PostMessage and WM_COPYDATA to pass a string from Application 1 to
Application 2. While Application 2 is processing that message, it can
use PostMessage and WM_COPYDATA to send string(s) back to Application
1. The down side is that Application 1 will need to handle the
asynchronous exchange. You should consider multiple threads so the
user has a chance to cancel.

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html
From: ScottMcP [MVP] on
On Feb 13, 11:07 am, r_z_aret(a)pen_fact.com wrote:
> Possibly better approach:
> Use PostMessage and WM_COPYDATA to pass a string from Application 1 to
> Application 2.

Robert: WM_COPYDATA does not work with PostMessage. It must be sent
using SendMessage to assure that the data cannot be changed until the
receiving application has copied it.