process communication between vb6 and c#

A

amw

Hello,

i have two applications (once in vb6 and once in c#) which have to
communicate together. They have to send and receive strings.

I used api-method sendmessage, but it doesn't work.

in vb:

Private Declare Function SendMessageByStr& Lib
"user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
ByVal _
lParam As String)

[..]

[code:1:5274173893]Call SendMessageByStr(hwndTarget, MessageId, 4,
"test")[/code:1:5274173893]

in C#:
[code:1:5274173893]unsafe protected override void WndProc(ref
Message m)
{
if (m.Msg == VB6_TO_CSHARP_MessageId.ToInt32())
{
try
{
string url = new
string((char*)(m.LParam.ToPointer()), 0,
m.WParam.ToInt32());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
base.WndProc(ref m);
} [/code:1:5274173893]

There is an exception by reading string. Has anyone sourcecode to send
and receive strings from VB6 and C# applications?

As an example project use this from codeproject (this project only
send normaly messages, no strings):
http://www.codeproject.com/dotnet/VB6andVBNETWindowMessages.asp

Can you help?

Sincerely,
Andre

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
W

Wiktor Zychla

There is an exception by reading string. Has anyone sourcecode to send
and receive strings from VB6 and C# applications?

you cannot send strings (pointers) in such way because the memory owned by
VB6 application cannot be accessed within the C# application. most probably
that's why you got an exception.

the simplest solution in your situation could be to send the string
char-after-char and end the transmission with some terminator. this way you
will not send a pointer but rather a long sequence of numbers. in vb6 code
just decompose the string into the sequence of digits (ASCII or any other
encoding) and in c# code compose the string back.

another solution would be to allocate some global memory (for example
GlobalAddAtom) at vb6 side and read the memory at C# side but I think it's
not worth an effort in your case.

Regards,
Wiktor Zychla
 
A

amw

Wiktor Zychlawrote:
[quote:60f30c4742]There is an exception by reading string. Has anyone
sourcecode to send
and receive strings from VB6 and C# applications?
the simplest solution in your situation could be to send the string
char-after-char and end the transmission with some terminator. this
way you
will not send a pointer but rather a long sequence of numbers. in vb6
code
just decompose the string into the sequence of digits (ASCII or any
other
encoding) and in c# code compose the string back.
[/quote:60f30c4742]

Thanks for your answer! Have you got an example code for
(de-)composing strings to char?

Regards,
Andre

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
W

Wiktor Zychla

Thanks for your answer! Have you got an example code for
(de-)composing strings to char?

I am afraid that this is a really basic question.

vb6: (decomposing)
Dim s as string ' string to send
Dim l as Long
For l = 1 To Len(s)
MsgBox Asc(Mid(s, l, 1))
Next

c#: (recreating string from byte[])

byte[] b; // received data
System.Text.Encoding.ASCII.GetString( b );
 
A

amw

I am afraid that this is a really basic question. :lol:

Ok, i know what you mean, now... It works.

It's a good idea (thanks!), but i hope there is an another way to send
strings in one message!

Regards,
Andre

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top