Results 1 to 3 of 3

Thread: [RESOLVED] VB6 and VB.NET Interprocess Comm.

  1. #1

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Resolved [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:
    1. Private Type MyStructure
    2.   dwData As Long
    3.   cbData As Long
    4.   lpData As Long
    5. 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:
    1. Private Type MyStructure
    2.               dwData As Long
    3.               cbData As Long
    4.               lpData As Long
    5.       End Type
    6.  
    7.       Private Const WM_COPYDATA = &H4A
    8.  
    9.   Public Function SendMessageToVBNET()
    10.     Dim hwndTarget As Long
    11.     Dim MessageId, i As Long
    12.     Dim a As String
    13.     Dim cds As MyStructure
    14.     Dim ThWnd As Long
    15.     Dim buf(1 To 255) As Byte
    16.  
    17.     ThWnd = FindWindow(vbNullString, "VB.NET Main Form")
    18.     a$ = "I'm from VB6"
    19.     Call CopyMemory(buf(1), ByVal a$, Len(a$))
    20.     cds.dwData = 3
    21.     cds.cbData = Len(a$) + 1
    22.     cds.lpData = VarPtr(buf(1))   'Converting a string 'a' to long so we can send it with the message
    23.  
    24.     If WindowMessagingInitialised = False Then
    25.       InitWindowMessaging                           'this starts the subclassing
    26.     End If
    27.  
    28.     hwndTarget = VBNET_WindowHandle         'this gets the handle of target
    29.  
    30.     MessageId = VB6_TO_VBNET_MessageId    'this is an indicator for the target that message is from vb6
    31.  
    32.     If hwndTarget <> 0 Then
    33.                 i = SendMessage(hwndTarget, WM_COPYDATA, MainForm.hwnd, cds)
    34.  
    35.     End If
    36.   End Function

    On the receiver side:
    Using Marshalling to convert pointer to structure

    VB Code:
    1. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    2.         Dim Mypoint As MyStructure
    3.  
    4.  
    5.         If m.Msg = WM_COPYDATA Then
    6.             Mypoint = CType(Marshal.PtrToStructure(m.LParam, GetType(MyStructure)), MyStructure)
    7.             MsgBox("Value of X> " & Mypoint.x & " <Value of Y> " & Mypoint.y & " <Value of XY> " & Mypoint.xy & " <-")
    8.  
    9.  
    10.         End If
    11.         MyBase.WndProc(m)
    12.  
    13.     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________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Private Type MyStructure
    2.   dwData As Long
    3.   cbData As Long
    4.   lStrLen As Long
    5.   bData[MAXLENGTH] as byte
    6. End Type

  3. #3

    Thread Starter
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: VB6 and VB.NET Interprocess Comm.

    The problem was indeed with the marshalling thing.This is how i solved it

    VB Code:
    1. public void SendMessageToVB6()
    2.         {
    3.             IntPtr hwndTarget;
    4.             IntPtr MessageId;
    5.             String message;
    6.             hwndTarget = this.VB6_WindowHandle;
    7.  
    8.             message="Hi,Did You get my message";
    9.             IntPtr ptr = Marshal.StringToHGlobalAnsi(message);
    10.             COPYDATASTRUCT cds = new COPYDATASTRUCT();
    11.             cds.dwData = IntPtr.Zero;
    12.             cds.cbData = message.Length;
    13.             cds.lpData = ptr;
    14.            
    15.             long result = SendMessage(hwndTarget,WM_COPYDATA,0,ref cds);
    16.  
    17.             Marshal.FreeHGlobal(ptr);
    18.  
    19.            
    20.         }

    Also i changed my sendmessage declaration to
    VB Code:
    1. 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:
    1. public struct COPYDATASTRUCT
    2.         {
    3.             public IntPtr dwData;
    4.             public int cbData;
    5.             public IntPtr lpData;
    6.         }

    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
  •  



Click Here to Expand Forum to Full Width