分類:[VB.NET]
2006/11/20(Mon) 15:57:12 編集(投稿者)
VB用に書き直してみた。
Imports System
Imports System.Runtime.InteropServices
Public Class CopyDataTest
Private Const WM_COPYDATA As Integer = &H4A
<DllImport("user32.dll")> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
' ※ WM_COPYDATA用にlParamの型をByRef lParam As COPYDATASTRUCT にしてある。
' そのほかにSendMessageを多用するのであれば、IntPtrにするか(Marshal.StructureToPtrを使う)、
' 関数名を変えるなどして対処する。
<DllImport("user32.dll")> _
Private Shared Function SendMessage _
(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As IntPtr, ByRef lParam As COPYDATASTRUCT) As IntPtr
End Function
Private Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Public Shared Sub Main(ByVal args() As String)
Dim hWnd As IntPtr
Dim cds As COPYDATASTRUCT
Dim s As String
hWnd = FindWindow(Nothing, "recv")
If Not hWnd.Equals(IntPtr.Zero) Then
s = "ほげ"
cds = New COPYDATASTRUCT
cds.dwData = IntPtr.Zero
cds.lpData = Marshal.StringToHGlobalUni(s)
cds.cbData = (s.Length + 1) * 2
SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, cds)
Marshal.FreeHGlobal(cds.lpData)
End If
End Sub
End Class