CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Aug 2010
    Posts
    50

    Unhappy Another WM_COPYDATA issue

    Hi. Here is some code, i'll write the explanations after:

    Code:
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Public Structure MsgSTR
            Public code As String
            Public index1 As Integer
            Public index2 As Integer
            Public arr() As Integer
        End Structure
    
    <StructLayout(LayoutKind.Sequential)> _
        Public Structure CopyData
            Public dwData As IntPtr
            Public cbData As Integer
            Public lpData As MsgSTR
        End Structure
    
    Private Declare Auto Function SendMessage Lib "user32" _
         (ByVal hWnd As IntPtr, _
          ByVal Msg As Integer, _
          ByVal wParam As IntPtr, _
          ByVal lParam As CopyData) As Boolean
    
        Private Declare Auto Function FindWindow Lib "user32" _
         (ByVal lpClassName As String, _
          ByVal lpWindowName As String) As IntPtr
    
        Private Const WM_COPYDATA As Integer = &H4A
    
    Private Function mesajSMSSTR(ByVal msg As MsgSTR) As Boolean
            Dim ClientWindow As IntPtr
            Dim a() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
    
            For Each p In a
                If UCase(p.ProcessName) = "SOMEPROCESS" Then
                    ClientWindow = p.MainWindowHandle
                    Exit For
                End If
            Next
           
            If Not ClientWindow.Equals(IntPtr.Zero) Then
    
                Dim message As MsgSTR = msg
                Dim data As CopyData
    
                data.lpData = message
                data.cbData = Marshal.SizeOf(message)
    
                frmTest.SendMessage(ClientWindow, frmTest.WM_COPYDATA, Me.Handle, data)
                Return True
            Else
                Return False
            End If
    End Function
    
    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
           
                Dim msg As MsgSTR
                msg.code = Trim(txtCode.Text)
                msg.index1 = CInt(Trim(txtI1.Text))
                msg.index2 = CInt(Trim(txtI2.Text))
                ReDim msg.arr(40)
                For i = 0 To Trim(txtArr.Text).Split(" ").Length - 1
                    msg.arr(i) = CInt(Trim(txtArr.Text).Split(" ")(i))
                Next
    
    
    
                If mesajSMSSTR(msg) Then
                    MsgBox("Message sent")
                Else
                    MsgBox("Message not sent. Process not running.")
                End If
           
    End Sub
    Basically i have a structure with a string, two 32bit integers and an array of 32bit integers. I need to send this structure using WM_COPYDATA to an application made in VC++ 6.0, from my VB .NET 2008 framework 3.5.

    When i execute the code above, only the (first) string is sent. If i comment the lines related to Arr() (both in the structure and in the _click event), only the first 16 characters of my string are being sent.

    For testing purposes, the receiving application uses memcopy to get any data that is sent to it and displays it in a messagebox.

    I have searched google and these forums a lot for similar problems but everything i tried yielded the same result - the integer values are not sent at all.

    If i put 3 strings in my structure instead, only the first is sent. Adding a vbNullString to the end of the first string (MSGSTR.code) results in an empty message box in the receiving application.

    Any help is appreciated, thanks.
    Last edited by Crusader2010; November 16th, 2011 at 05:33 AM.

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

Featured