2012-12-14 136 views
0

的目的是使用winapi sendmessage將消息從vb.net應用程序發送到另一個應用程序。我無法讓它工作。你的幫助是極大的讚賞將C++轉換爲VB.NET

這是我有什麼,但它似乎並不工作

Public Class WinAPI 
    Private hwnd As Integer 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal lpClassName As String, _ 
      ByVal lpWindowName As String) As IntPtr 

    'FindWindowByClass 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal lpClassName As String, _ 
      ByVal zero As IntPtr) As IntPtr 

    'FindWindowByCaption 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal zero As IntPtr, _ 
      ByVal lpWindowName As String) As IntPtr 

    Private Declare Auto Function SendMessage Lib "user32" _ 
     (ByVal hWnd As IntPtr, _ 
     ByVal Msg As Integer, _ 
     ByVal wParam As IntPtr, _ 
     ByRef lParam As COPYDATASTRUCT) As Boolean 

    Public Const WM_COPYDATA As Integer = &H4A 

    <StructLayout(LayoutKind.Sequential)> _ 
    Structure COPYDATASTRUCT 
     Dim dwData As Long 
     Dim cbData As Long 
     Dim lpData As IntPtr 
    End Structure 

    Public Sub SendToeSignal(ByVal strMessage As String) 
     hwnd = FindWindow(vbNullString, "eSignalSink") 
     ' hwnd = FindWindow("eSignalSink", "vbNullString") 
     Dim DataStruct As New COPYDATASTRUCT 
     ' strMessage = "1" & "," & strMessage & Chr(0) & vbCr 'Null terminated & carriage return 
     strMessage = "1" & "," & strMessage & vbCr 'Null terminated & carriage return 

     DataStruct.dwData = 1 
     DataStruct.cbData = strMessage.Length * Marshal.SystemDefaultCharSize 
     DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage) 

     SendMessage(hwnd, WM_COPYDATA, 0, DataStruct) 
     Marshal.FreeCoTaskMem(DataStruct.lpData) 

    End Sub 
End Class 
+2

是什麼* *似乎要怎麼辦呢? –

回答

2

它看起來像你有你COPYDATASTRUCT試試這個,而不是一個VB6樣式定義。

從上面的PInvoke鏈接:

所有的
<StructLayout(LayoutKind.Sequential)> _ 
Structure COPYDATASTRUCT 
    Public dwData As IntPtr 
    Public cdData As Integer 
    Public lpData As IntPtr 
End Structure 

首先請你幫個忙,並啓用Option Strict expecially當您使用API​​函數工作。您正在發送應用程序之間的結構,並且需要確保您可以在接收應用程序中檢索它。我對您的示例代碼進行了一些更改,並且確實可行,在主窗體名爲TestApp的測試程序中接收數據。

您與修改

Option Strict On 
Imports System.Runtime.InteropServices 

Public Class Form1 

    Public Sub New() 

     ' This call is required by the designer. 
     InitializeComponent() 

     Dim myWinAPI As WinAPI = New WinAPI 
     myWinAPI.SendToeSignal("Hello World") 
     ' Add any initialization after the InitializeComponent() call. 
    End Sub 
End Class 

Public Class WinAPI 

    Private hwnd As IntPtr 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal lpClassName As String, _ 
      ByVal lpWindowName As String) As IntPtr 

    'FindWindowByClass 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal lpClassName As String, _ 
      ByVal zero As IntPtr) As IntPtr 

    'FindWindowByCaption 
    Private Declare Auto Function FindWindow Lib "user32" _ 
      (ByVal zero As IntPtr, _ 
      ByVal lpWindowName As String) As IntPtr 

    Private Declare Auto Function SendMessage Lib "user32" _ 
      (ByVal hWnd As IntPtr, _ 
      ByVal Msg As Integer, _ 
      ByVal wParam As IntPtr, _ 
      ByRef lParam As COPYDATASTRUCT) As Boolean 

    Public Const WM_COPYDATA As Integer = &H4A 

    <StructLayout(LayoutKind.Sequential)> _ 
    Structure COPYDATASTRUCT 
     Dim dwData As IntPtr 
     Dim cbData As Integer 
     Dim lpData As IntPtr 
    End Structure 

    Public Sub SendToeSignal(ByVal strMessage As String) 
     hwnd = FindWindow(IntPtr.Zero, "TestApp") 
     Dim DataStruct As New COPYDATASTRUCT 
     strMessage = "1" & "," & strMessage & vbCr 'Null terminated & carriage return 

     DataStruct.dwData = CType(1, IntPtr) 
     DataStruct.cbData = strMessage.Length * Marshal.SystemDefaultCharSize 
     DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage) 

     SendMessage(hwnd, WM_COPYDATA, IntPtr.Zero, DataStruct) 
     Marshal.FreeCoTaskMem(DataStruct.lpData) 

    End Sub 

    Public Sub New() 

    End Sub 
End Class 

接收應用

Imports System.Runtime.InteropServices 
Imports System.Text 

Public Class Form1 
    <StructLayout(LayoutKind.Sequential)> _ 
    Structure COPYDATASTRUCT 
     Dim dwData As IntPtr 
     Dim cbData As Integer 
     Dim lpData As IntPtr 
    End Structure 
    Public Const WM_COPYDATA As Integer = &H4A 
    Dim split() As String = New String() {",", " "} 
    Dim myData() As String 

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
     MyBase.WndProc(m) 
     If m.Msg = WM_COPYDATA Then 
      Dim CD As COPYDATASTRUCT = DirectCast(m.GetLParam(GetType(COPYDATASTRUCT)), COPYDATASTRUCT) 
      Dim B As Byte() = New Byte(CD.cbData - 1) {} 
      Dim lpData As IntPtr = CD.lpData 
      Marshal.Copy(lpData, B, 0, CD.cbData) 
      Dim strData As String = Encoding.[Default].GetString(B) 
      myData = strData.Split(split, StringSplitOptions.None) 
     End If 
    End Sub 
End Class 
+0

更改結構,仍然不工作 – user1905155

+0

@ user1905155請參閱我的編輯 –

+0

謝謝問題解決 – user1905155

0

感謝,幾乎我的應用程序工作的第一個例子中的例子。 接收端應用程序需要ANSI(Winamp的),繩子,讓我不得不改變線路:

DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage) 

DataStruct.lpData = Marshal.StringToCoTaskMemAnsi(strMessage) 

而且它的工作就像一個魅力,第一個例子是正確拿到了三分球爲x64 - x32。只是希望我找到這首在我24小時的追求

贏10 Visualstudio2017 vb.net