|
-
Dec 2nd, 2006, 01:21 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] VB6 and VB.NET Interprocess Comm.
Hello everyone,
I have two apps ,one in vb and another in vb.net .Now I'm trying to send messages from one another.I'm able to detect the receipt of messages both ways.I'm also able to send a structure of the form
VB Code:
Private Type MyStructure
dwData As Long
cbData As Long
lpData As Long
End Type
from vb6 to vb.net but when i attempt to send a structure from vb.net to vb6,it is not able to convert the data back to its original structure.
The logic i used to convert is simple
On the sender side:
VB Code:
Private Type MyStructure
dwData As Long
cbData As Long
lpData As Long
End Type
Private Const WM_COPYDATA = &H4A
Public Function SendMessageToVBNET()
Dim hwndTarget As Long
Dim MessageId, i As Long
Dim a As String
Dim cds As MyStructure
Dim ThWnd As Long
Dim buf(1 To 255) As Byte
ThWnd = FindWindow(vbNullString, "VB.NET Main Form")
a$ = "I'm from VB6"
Call CopyMemory(buf(1), ByVal a$, Len(a$))
cds.dwData = 3
cds.cbData = Len(a$) + 1
cds.lpData = VarPtr(buf(1)) 'Converting a string 'a' to long so we can send it with the message
If WindowMessagingInitialised = False Then
InitWindowMessaging 'this starts the subclassing
End If
hwndTarget = VBNET_WindowHandle 'this gets the handle of target
MessageId = VB6_TO_VBNET_MessageId 'this is an indicator for the target that message is from vb6
If hwndTarget <> 0 Then
i = SendMessage(hwndTarget, WM_COPYDATA, MainForm.hwnd, cds)
End If
End Function
On the receiver side:
Using Marshalling to convert pointer to structure
VB Code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim Mypoint As MyStructure
If m.Msg = WM_COPYDATA Then
Mypoint = CType(Marshal.PtrToStructure(m.LParam, GetType(MyStructure)), MyStructure)
MsgBox("Value of X> " & Mypoint.x & " <Value of Y> " & Mypoint.y & " <Value of XY> " & Mypoint.xy & " <-")
End If
MyBase.WndProc(m)
End Sub
I have attached two zip files.
1.vb-vb.zip has two projects ,one sender and one receiver and they are working as expected.
2.vb-vbnet.zip has two projects, one in vb6 and another in vb.net using the same logic as above.
Last edited by litlewiki; Apr 13th, 2007 at 02:02 PM.
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
-
Dec 2nd, 2006, 04:34 PM
#2
Re: VB6 and VB.NET Interprocess Comm.
Do not send a pointer to your string across processes, this won't be marshalled by the OS.
Instead send a length and a byte array that contains each character of the string.
VB Code:
Private Type MyStructure
dwData As Long
cbData As Long
lStrLen As Long
bData[MAXLENGTH] as byte
End Type
-
Dec 2nd, 2006, 06:23 PM
#3
Thread Starter
Frenzied Member
Re: VB6 and VB.NET Interprocess Comm.
The problem was indeed with the marshalling thing.This is how i solved it
VB Code:
public void SendMessageToVB6()
{
IntPtr hwndTarget;
IntPtr MessageId;
String message;
hwndTarget = this.VB6_WindowHandle;
message="Hi,Did You get my message";
IntPtr ptr = Marshal.StringToHGlobalAnsi(message);
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.dwData = IntPtr.Zero;
cds.cbData = message.Length;
cds.lpData = ptr;
long result = SendMessage(hwndTarget,WM_COPYDATA,0,ref cds);
Marshal.FreeHGlobal(ptr);
}
Also i changed my sendmessage declaration to
VB Code:
public static extern long SendMessage(IntPtr hWnd, uint Msg, uint wParam, ref COPYDATASTRUCT lParam );
After a bit of googling ,i found out that COPYDATASTRUCT is a standard convention for sending messages across apps :
VB Code:
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
Where cbdata holds the length and lpdata holds the string .
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
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
|