SendMessage - 将字符串从VB6传递到VB.NET

时间:2015-06-23 00:05:53

标签: vb.net winapi vb6

下午好,

我正在尝试使用SendMessage将字符串从VB6 EXE传递到.NET 2013 EXE。我知道消息正在进入.NET EXE,因为我能够在它上面设置一个断点,当我从VB6 EXE调用SendMessage时它会出现。我遇到的问题是检索字符串。

这就是我试图这样做的方式:

VB6代码:

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal msg As Long, wParam As Long, lParam As Any) As Long

Private Const APPVIEWER_OPEN = &H400

Private Sub Command1_Click()
  Dim hwndAppViewer As Long
  Dim bytBuffer(1 To 255) As Byte
  Dim sParams As String
  Dim lStringAddress As Long

  hwndAppViewer = FindWindow(vbNullString, "DotNetReceiver")

  If hwndAppViewer > 0 Then
    sParams = "STRINGDATA"
    CopyMemory bytBuffer(1), sParams, Len(sParams)
    lStringAddress = VarPtr(bytBuffer(1))
    SendMessage hwndAppViewer, APPVIEWER_OPEN, Me.hwnd, lStringAddress
  End If
End Sub

这是.NET代码:

Imports System.Runtime.InteropServices

Public Class Form1
  Protected Overrides Sub WndProc(ByRef m As Message)
    Dim sPolicyInformation As String

    If m.Msg = &H400 Then
      sPolicyInformation = Marshal.PtrToStringAnsi(m.LParam)
    Else
      MyBase.WndProc(m)
    End If
  End Sub
End Class

当我尝试检索字符串时出现问题。我收到一个空白字符串。我注意到VB6 lStringAddress中的数字和.NET m.lParam中的数字完全不同,所以我一定不知道我是如何通过lParam传递地址的。

我可能缺少什么想法?

谢谢。

1 个答案:

答案 0 :(得分:1)

您正在向VB.NET发送ANSI字符串。 VB6专为所有MS的操作系统而设计,9x不是unicode。因此,传递给API调用的所有字符串都将转换为ANSI。 Windows将在收到VB.NET程序时将该ANSI字符串转换为unicode。

使用sendmessagew函数并发送一个空终止的字节数组的第一个元素。

Dim MyStr() as byte
MyStr = "cat" & chrw(0)

只传递给SendMessageW的第一个元素,即MyStr(0)。 Windows API使用空终止的C字符串。 COM和VB6使用BStr(大小标头和非空终止字符串)。

通过ref传递字符串时,您传递标题的地址。通过值传递时,您传递第一个字符的地址(如果您在结尾处添加空值,则将其设为c字符串)。

相关问题