-
Mar 3rd, 2008, 12:18 PM
#1
Thread Starter
Junior Member
[2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Ive been struggling with the Win32 api call SendMessage and WM_COPYDATA to send strings to e.g. notepad using VB.NET 2005. Anyone has any sample code to share on how to do this?
With regards,
A
-
Mar 3rd, 2008, 01:03 PM
#2
Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
-
Mar 3rd, 2008, 01:08 PM
#3
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
This is the current code im working on, its really an atempt to port some VB6 code that is working. The development platform is Vista 64-bit.
The FindWindowWild(TitleBarSearchName) provides a hWnd to the correct window, this has been verified using Spy++...
Things i need to consider later on is to assure its unicode being sent so
Code:
Private Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, _
ByVal wParam As IntPtr, <MarshalAs(UnmanagedType.AnsiBStr)> ByVal lParam As String) As IntPtr
<StructLayout(LayoutKind.Sequential)> _
Private Structure CopyData
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Public Sub SendToApplication(ByVal TitleBarSearchName As String, ByVal strMessage As String)
Dim data As CopyData
' set up the data...
data.lpData = Marshal.StringToHGlobalAuto(strMessage)
data.cbData = strMessage.Length * Marshal.SystemDefaultCharSize
' send the data
SendMessage(FindWindowWild(TitleBarSearchName), WM_COPYDATA, IntPtr.Zero, data)
' free the pointer...
Marshal.FreeHGlobal(data.lpData)
SendMessage(FindWindowWild(TitleBarSearchName), New IntPtr(WM_COPYDATA), IntPtr.Zero, strMessage)
End Sub
-
Mar 3rd, 2008, 01:10 PM
#4
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Oh and here is another version im trying in parallell with..
VB Code:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Namespace Core.Utils
Public Structure COPYDATASTRUCT
Public dwData As Integer
Public cbData As Integer
Public lpData As IntPtr
End Structure
Public Class cMsgStrings
Const LMEM_FIXED As Integer = 0
Const LMEM_ZEROINIT As Integer = 64
Const LPTR As Integer = (LMEM_FIXED Or LMEM_ZEROINIT)
Const WM_COPYDATA As Integer = 74
<DllImport("kernel32.dll")> _
Public Shared Function LocalAlloc(ByVal flag As Integer, ByVal size As Integer) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Public Shared Function LocalFree(ByVal p As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
'Create wrappers for the memory API's similar to
'Marshal.AllocHGlobal and Marshal.FreeHGlobal
Public Shared Function AllocHGlobal(ByVal cb As Integer) As IntPtr
Dim hMemory As New IntPtr()
hMemory = LocalAlloc(LPTR, cb)
Return hMemory
End Function
Public Shared Sub FreeHGlobal(ByVal hMemory As IntPtr)
If hMemory <> IntPtr.Zero Then
LocalFree(hMemory)
End If
End Sub
Public Shared Sub SendMsgString(ByVal hWndDest As IntPtr, ByVal sScript As String)
Dim oCDS As New COPYDATASTRUCT()
Dim status As String
oCDS.cbData = (sScript.Length + 1) * 2
oCDS.lpData = LocalAlloc(64, oCDS.cbData)
Marshal.Copy(sScript.ToCharArray(), 0, oCDS.lpData, sScript.Length)
oCDS.dwData = 1
Dim lParam As IntPtr = AllocHGlobal(oCDS.cbData)
Marshal.StructureToPtr(oCDS, lParam, False)
status = SendMessage(hWndDest, WM_COPYDATA, IntPtr.Zero, lParam)
MsgBox(Hex(hWndDest))
LocalFree(oCDS.lpData)
FreeHGlobal(lParam)
End Sub
Public Shared Function GetMsgString(ByVal lParam As IntPtr) As String
Dim st As COPYDATASTRUCT = DirectCast(Marshal.PtrToStructure(lParam, GetType(COPYDATASTRUCT)), COPYDATASTRUCT)
Dim str As String = Marshal.PtrToStringUni(st.lpData)
Return str
End Function
End Class
End Namespace
-
Mar 3rd, 2008, 01:48 PM
#5
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
-
Mar 3rd, 2008, 01:52 PM
#6
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Ok same goes here... FindWindowWild(TitleBarSearchName, False) provides the hWnd. This code works like a charm in all environments, its VB6 code. Compiled in VB6 this works on Vista too and does the trick.
vb Code:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const WM_COPYDATA = &H4A Type COPYDATASTRUCT dwData As Long cbData As Long lpData As Long End Type Public Sub SendToApplication(strMessage As String) Dim DataStruct As COPYDATASTRUCT Dim strInternal As String strInternal = strMessage & vbCr DataStruct.dwData = 1 DataStruct.lpData = StrPtr(strInternal) DataStruct.cbData = (LenB(strInternal) + 2) result = SendMessage(FindWindowWild(TitleBarSearchName, False), WM_COPYDATA, 0, DataStruct) End Sub
-
Mar 3rd, 2008, 02:33 PM
#7
Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
-
Mar 3rd, 2008, 02:37 PM
#8
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
This function searches for a Window caption and returns a hwnd.
-
Mar 3rd, 2008, 02:53 PM
#9
Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
-
Mar 3rd, 2008, 03:01 PM
#10
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Nope, already tried this code but without success...
-
Mar 3rd, 2008, 03:35 PM
#11
Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
I was able to get some unprintable binary garbage into notepad using this code:
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WM_COPYDATA As Integer = &H4A
Private Const WM_SETTEXT = &HC
Private Const WM_USER As Integer = &H400
<StructLayout(LayoutKind.Sequential)> _
Private Structure COPYDATASTRUCT
Public dwData As Integer
Public cbData As Integer
Public lpData As String
End Structure
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage( _
ByVal hWnd As HandleRef, _
ByVal Msg As UInteger, _
ByVal wParam As IntPtr, _
ByRef lParam As String) As IntPtr
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Public Sub SendToApplication(ByVal TitleBarSearchName As String, ByVal strMessage As String)
Dim cds As COPYDATASTRUCT
Dim SnapHandle As IntPtr = FindWindow(TitleBarSearchName, vbNullString)
Me.Text = SnapHandle.ToInt32.ToString
If Not SnapHandle = 0 Then
Dim href As New HandleRef(Me, SnapHandle)
Dim SnapHandle2 As IntPtr = FindWindowEx(SnapHandle, 0&, "edit", vbNullString)
Me.Text = SnapHandle.ToInt32.ToString
If Not SnapHandle2 = 0 Then
Dim href2 As New HandleRef(Me, SnapHandle2)
cds.lpData = strMessage
cds.cbData = cds.lpData.Length + 1
Dim I As IntPtr = SendMessage(href2, WM_SETTEXT, 0&, cds.lpData)
End If
End If
GC.KeepAlive(Me)
End Sub
Private Sub sndButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles sndButton.Click
SendToApplication(Me.searchTxtBox.Text, Me.msgTxtBox.Text)
End Sub
End Class
-
Mar 3rd, 2008, 03:59 PM
#12
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Yes, but you are using WM_SETTEXT rather than WM_COPYDATA in your code.
-
Mar 3rd, 2008, 04:04 PM
#13
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
The code is a bit of a mess but, its hours of trail and error...
Ive used a similar approach to set or change the Notepad title window caption with WM_SETTEXT... but the end application requires and listens to a WM_COPYDATA. Ive included what i got so far... It contains a lot of junk code an unused sections... Just cant seem to figure this out.
vb Code:
Option Strict Off Imports System.Environment Imports System.Runtime.InteropServices Module Core Private Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean Private Declare Function EnumWindows Lib "User32" (ByVal lpEnumFunc As CallBack, ByVal lParam As Integer) As Integer Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Boolean Private Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer Private Declare Function LocalAlloc Lib "Kernel32" (ByVal uFlags As Integer, ByVal uBytes As Integer) As IntPtr Private Declare Function LocalFree Lib "Kernel32" (ByVal hMem As IntPtr) As IntPtr ' Private Declare Auto Function SendMessage Lib "User32" (ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByRef lParam As COPYDATASTRUCT) As Integer ' Private Declare Auto Function SendMessage Lib "User32" (ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer ' Private Declare Auto Function SendMessage Lib "User32" (ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer Private Declare Auto Function SendMessageA Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, _ ByVal wParam As IntPtr, <MarshalAs(UnmanagedType.AnsiBStr)> ByVal lParam 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 CopyData) As Boolean '############################### '# win32api constants # '############################### Public Const WM_COPYDATA As Integer = &H4A Public Const WM_KEYDOWN As Integer = &H100 Public Const WM_KEYUP As Integer = &H101 Public Const WM_GETTEXT As Integer = &HD Public Const WM_KILLFOCUS As Integer = &H8 Public Const WM_SETFOCUS As Integer = &H7 Public Const WM_NCHITTEST As Integer = &H84 Public Const WM_PAINT As Integer = &HF Public Const WM_ACTIVATE As Integer = &H6 Public Const WM_NCACTIVATE As Integer = &H86 Public Const WM_SETCURSOR As Integer = &H20 Public Const WM_COMMAND As Integer = &H111 Public Const WM_MENUSELECT As Integer = &H11F Public Const WM_SETTEXT = 12 Public Const GW_CHILD As Integer = 5 Public Const GW_HWNDFIRST As Integer = 0 Public Const GW_HWNDLAST As Integer = 1 Public Const GW_HWNDNEXT As Integer = 2 Public Const GW_HWNDPREV As Integer = 3 <StructLayout(LayoutKind.Sequential)> _ Private Structure CopyData Public dwData As IntPtr Public cbData As Integer Public lpData As IntPtr End Structure Public Event WndH(ByVal hWnd As Integer) Dim sPattern As String Dim hFind As Integer Dim mMatchCase As Boolean = True Public Function VarPtr(ByVal o As Object) As Integer Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned) Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32 GC.Free() Return ret End Function Private Function returnHwnd(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean RaiseEvent WndH(hwnd) Dim k As Long, sName As String If IsWindowVisible(hwnd) Then sName = Space$(128) k = GetWindowText(hwnd, sName, 128) If k > 0 Then sName = Left$(sName, CInt(k)) If Not mMatchCase Then sName = sName.ToUpper() If sName Like sPattern Then hFind = hwnd Return False Exit Function End If End If End If Return True End Function Public Sub GetWindows() Call EnumWindows(AddressOf returnHwnd, 0) End Sub Public Function FindWindowWild(ByVal sWild As String, Optional ByVal bMatchCase As Boolean = True) As Long mMatchCase = bMatchCase On Error Resume Next sPattern = sWild hFind = 0 If Not bMatchCase Then sPattern = UCase(sPattern) Call GetWindows() Return hFind End Function ' Currently not working! Public Sub SendToCommandPrompt(ByVal TitleBarSearchName As String, ByVal strMessage As String) Dim data As CopyData ' set up the data... data.lpData = Marshal.StringToHGlobalAuto(strMessage) data.cbData = strMessage.Length * Marshal.SystemDefaultCharSize ' send the data SendMessage(FindWindowWild(TitleBarSearchName), WM_COPYDATA, IntPtr.Zero, data) ' free the pointer... Marshal.FreeHGlobal(data.lpData) SendMessageA(FindWindowWild(TitleBarSearchName), New IntPtr(WM_COPYDATA), IntPtr.Zero, strMessage) End Sub End Module
-
Mar 4th, 2008, 09:02 AM
#14
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Are you sure your parameters are correct (Notepad is waiting for 1 as dwData etc)? The Message returns false no matter what I try and I tried ways that are supposed to work.
Here is a C# example that uses WM_COPYDATA. It sends the message but Notepad returns False.
Another code I changed to work with Notepad fails too:
Code:
Dim notepadHWND As IntPtr
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal ClassName As String, ByVal WindowName As String) As IntPtr
Private Declare Auto Function SendMessage Lib "user32" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As COPYDATASTRUCT _
) As Integer
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Private Structure COPYDATASTRUCT
Public dwData As Integer
Public cbData As Integer
Public lpData As IntPtr
End Structure
Private Const WM_COPYDATA As Integer = &H4A
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'get the handle of Notepad's window
notepadHWND = FindWindow("Notepad", "z123.txt - Notepad")
Send("HI")
End Sub
Public Function Send(ByVal obj As Object) As Integer
' Try to do a binary serialization on obj.
' This will throw and exception if the object to
' be passed isn't serializable.
' Try to do a binary serialization on obj.
' This will throw and exception if the object to
' be passed isn't serializable.
Dim b As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = _
New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()
b.Serialize(stream, obj)
stream.Flush()
' Now move the data into a pointer so we can send
' it using WM_COPYDATA:
' Get the length of the data:
Dim dataSize As Integer = CInt(stream.Length)
If (dataSize > 0) Then
' This isn't very efficient if your data is very large.
' First we copy to a byte array, then copy to a CoTask
' Mem object... And when we use WM_COPYDATA windows will
' make yet another copy! But if you're talking about 4K
' or less of data then it doesn't really matter.
Dim data(dataSize) As Byte
stream.Seek(0, System.IO.SeekOrigin.Begin)
stream.Read(data, 0, dataSize)
Dim ptrData As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(dataSize)
System.Runtime.InteropServices.Marshal.Copy(data, 0, ptrData, dataSize)
Dim cds As COPYDATASTRUCT = New COPYDATASTRUCT()
cds.cbData = dataSize
cds.dwData = 1
cds.lpData = ptrData
Dim res As Integer = SendMessage(notepadHWND, WM_COPYDATA, Me.Handle.ToInt32(), cds)
' Clear up the data:
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptrData)
End If
stream.Close()
End Function
-
Mar 4th, 2008, 09:14 AM
#15
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Well actually im trying to send to AutoCAD. Ive made some sort of successful atempts today although it just sends gibberish characters and im assuming its a unicode problem... Its just that my old VB6 code allows me to send to Notepad without a problem.
I get this below when sending msg = "asdf" as a string.
>Unknown command "F獡晤". Press F1 for help.
>Command: f獡晤
>Unknown command "F獡晤". Press F1 for help.
>Command: f獡晤
Cant believe this.
/A
-
Mar 4th, 2008, 09:18 AM
#16
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Currently im playing with the code similar to below, which atleast sends something to AutoCAD - but as i mentioned, incorrect characters.
vb Code:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _ Public Shared Function SendMessage( _ ByVal hwnd As IntPtr, _ ByVal wMsg As IntPtr, _ ByVal wParam As Long, _ ByRef lParam As COPYDATASTRUCT) As IntPtr End Function '<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Structure COPYDATASTRUCT Dim dwData As Integer Dim cbData As IntPtr Dim lpData As String End Structure Public Sub SendToCommandPrompt(ByVal strMessage As String) Dim DataStruct As COPYDATASTRUCT DataStruct.dwData = 1 'DataStruct.cbData = Len(strMessage) * 2 DataStruct.cbData = Encoding.Unicode.GetByteCount(strMessage) DataStruct.lpData = Encoding.Unicode.GetString(Encoding.ASCII.GetBytes(strMessage)) SendMessage(FindWindowWild("*AutoCAD*"), WM_COPYDATA, 0, DataStruct) End Sub Declare Function SendMessageUU Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As COPYDATASTRUCT) As Long Private Sub SendToCommandPrompt2(ByVal strMessage As String) Dim DataStruct As COPYDATASTRUCT DataStruct.dwData = 1 DataStruct.lpData = strMessage DataStruct.cbData = (Encoding.Unicode.GetByteCount(strMessage) + 1) SendMessageUU(FindWindowWild("*AutoCAD*"), WM_COPYDATA, 0, DataStruct) End Sub
-
Mar 4th, 2008, 10:34 AM
#17
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
If it sends something, then it is working
Try Encoding.Default here and there instead of ASCII & Unicode.
-
Mar 4th, 2008, 10:37 AM
#18
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Ill give it a try, problem is finding the correct value for .cbData
DataStruct.cbData = (Encoding.Unicode.GetByteCount(strMessage) + 1)
and also, the behavior can be kinda sluggish.. i.e. it doesnt work 10 time out of 10, but maybe 8-9 times out of 10.
/A
-
Mar 6th, 2008, 03:06 PM
#19
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
The Code below does the best job in sending data to my application, however im having issues determining the size of the string and again, everything gets screambled into Chineese unicode characters.
VB Code:
Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByRef lParam As COPYDATASTRUCT) As Long
Structure COPYDATASTRUCT
Dim dwData As Long
Dim cbData As Long
Dim lpData As String
End Structure
Public Sub SendToCommandPrompt(ByVal strMessage As String)
Dim DataStruct As COPYDATASTRUCT
DataStruct.dwData = 1
DataStruct.lpData = strMessage
DataStruct.cbData = Marshal.SizeOf(DataStruct)
Dim ByteArray() As Byte
Dim Ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(DataStruct))
ReDim ByteArray(Marshal.SizeOf(DataStruct) + 1)
'now copy strcutre to Ptr pointer
Marshal.StructureToPtr(DataStruct, Ptr, False)
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(DataStruct))
'now use ByteArray ready for use
Marshal.FreeHGlobal(Ptr)
SendMessage(hwnd_autocad, WM_COPYDATA, 0, DataStruct)
End Sub
-
Mar 10th, 2008, 01:26 PM
#20
Addicted Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
How about...
vb.net Code:
Dim DataStruct As New COPYDATASTRUCT
strMessage = strMessage & Chr(0) 'Null terminated
DataStruct.dwData = 0
DataStruct.cbData = strMessage.Length * Marshal.SystemDefaultCharSize
DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage)
SendMessage(hwnd_autocad, WM_COPYDATA, 0, DataStruct)
Marshal.FreeCoTaskMem(DataStruct.lpData)
------------------------------------------------------------------------------------------
C# sample below is what I use for SendMessage
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
internal struct COPYDATASTRUCT : IDisposable
{
public int dwData;
public int cbData;
public IntPtr lpData;
public void Dispose()
{
lpData = IntPtr.Zero;
}
}
COPYDATASTRUCT cds = new COPYDATASTRUCT();
string data = strMessage + "\0";
cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
cds.lpData = Marshal.StringToCoTaskMemAuto(data);
SendMessage(hwnd_autocad, WM_COPYDATA, IntPtr.Zero, ref cds);
Marshal.FreeCoTaskMem(cds.lpData);
cds.Dispose();
Last edited by michaelerice; Mar 10th, 2008 at 01:42 PM.
-
Mar 10th, 2008, 03:41 PM
#21
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Holy Cow! A problem i believed to be unsolvable is now solved...
With your suggestion plus changing below actually did the trick!
Code:
DataStruct.dwData = 1
Your the man! Im eternally greatful!
-
Mar 10th, 2008, 03:59 PM
#22
Thread Starter
Junior Member
Re: [2005] Using SendMessage with WM_COPYDATA to send strings to e.g. notepad
Code:
Just posting the finished working code as i got it..
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 SendToCommandPrompt(ByVal strMessage As String)
Dim DataStruct As New COPYDATASTRUCT
strMessage = strMessage & Chr(0) 'Null terminated
DataStruct.dwData = 1
DataStruct.cbData = strMessage.Length * Marshal.SystemDefaultCharSize
DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage)
SendMessage(hwnd_autocad, WM_COPYDATA, 0, DataStruct)
Marshal.FreeCoTaskMem(DataStruct.lpData)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|