Convert C++ to VB.NET - vb.net

the purpose is to send a message from an vb.net application to another application using winapi sendmessage. I cannot get it to work. Your help is greatly appreciated
This is what I have, but it does not seems to work
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

It looks like you have a VB6 style definition of your COPYDATASTRUCT try this instead.
From above PInvoke link:
<StructLayout(LayoutKind.Sequential)> _
Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cdData As Integer
Public lpData As IntPtr
End Structure
First of all do yourself a favor and enable Option Strict expecially when you are working with API functions. You are sending a structure between your applications and will need to make sure you can retrieve it at the receiving application. I made some changes to your example code and it does work, receiving the data in a test program that the main Form is named TestApp.
Your example with modifications
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
Receiving Application
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

Thanks for the first example that almost worked for my application.
The receiver application need the string in ANSI ( WinAmp ) so I had to change the line:
DataStruct.lpData = Marshal.StringToCoTaskMemAuto(strMessage)
to
DataStruct.lpData = Marshal.StringToCoTaskMemAnsi(strMessage)
Besides that it worked like a charm, first example that got the pointers correctly for x64 - x32. Just wish I found this first in my 24 hr quest
Win 10. Visualstudio2017 vb.net

Related

GetWindowText() Doesn't Work For Greek

I'm using this code to get a list of open windows:
Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
Private Callback As EnumWindowDelegate = New EnumWindowDelegate(AddressOf EnumWindowProc)
Private Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
If IsWindowVisible(hWnd) Then
Dim TheLength As Integer = GetWindowTextLengthA(hWnd)
Dim TheReturn(TheLength * 2) As Byte
GetWindowText(hWnd, TheReturn, TheLength + 1)
Dim TheText As String = ""
For x = 0 To (TheLength - 1) * 2
If TheReturn(x) <> 0 Then
TheText &= Chr(TheReturn(x))
End If
Next
If TheText <> "" Then
ListBox1.Items.Add(TheText & " (" & CStr(hWnd) & ")")
End If
End If
Return True
End Function
Private Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
Private Declare Auto Function GetWindowText Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Txt As Byte(), ByVal Lng As Integer) As Integer
Private Declare Function IsWindowVisible Lib "User32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowTextLengthA Lib "User32.dll" (ByVal hwnd As IntPtr) As Integer
Usage: EnumWindows(Callback, IntPtr.Zero)
It works BUT if a window with a greek title is opened ex. 'Μουσική' this code outputs 'œ¿Åùº® '. As you can see something is wrong. Is there a way to fix this?
P.S. Sorry for my bad English :)
Ok I think i found the solution: Use GetWindowTextA instead of GetWindowText.
#Trevor suggested this in the comments of the question and I tried it but it didn't work. Somehow I managed to make it work.
Thanks to everybody who tried to help :). Here is the working code:
Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
Private Callback As EnumWindowDelegate = New EnumWindowDelegate(AddressOf EnumWindowProc)
Private Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
If IsWindowVisible(hWnd) Then
Dim TheLength As Integer = GetWindowTextLengthA(hWnd)
Dim TheReturn(TheLength * 2) As Byte
GetWindowText(hWnd, TheReturn, TheLength + 1)
Dim TheText As String = ""
For x = 0 To (TheLength - 1) * 2
If TheReturn(x) <> 0 Then
TheText &= Chr(TheReturn(x))
End If
Next
If Not TheText = "" Then
ListBox1.Items.Add(TheText & " (" & CStr(hWnd) & ")")
End If
End If
Return True
End Function
Private Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As IntPtr, ByVal Txt As Byte(), ByVal Lng As Integer) As Integer
Private Declare Function IsWindowVisible Lib "User32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Function GetWindowTextLengthA Lib "User32.dll" (ByVal hwnd As IntPtr) As Integer

How to retrieve open excel filename from remote pc using vb.net

Hi currently I'm having this code below.
It retrieves the filename of open excel document from process and displays it.
For now it can retrieve it from my own pc, but when I wanted to remotely retrieves it from other pc, it doesn't work.
I have authorized access and I could actually get the process id and name of remote pcs only.
Dim w As Object
Dim processQ As String
Dim processes As Object
Dim processA As Object
Dim pname As String
w = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & pc & "\root\cimv2")
processQ = "SELECT * FROM win32_process WHERE name = 'EXCEL.EXE'"
processes = w.execquery(processQ)
For Each processA In processes
activeprocess = Process.GetProcessById(processA.processid)
MsgBox(processA.processid & processA.name)
Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(processA.processid)
MsgBox(windows.Count())
For Each kvp As KeyValuePair(Of IntPtr, String) In windows
Dim value As String = kvp.Value.ToString
If InStr(value, "Excel") = False Then
MsgBox(value)
End If
Next
Next
Can anyone tell me what should I do and what's wrong with this?
<DllImport("USER32.DLL")>
Private Shared Function GetShellWindow() As IntPtr
End Function
<DllImport("USER32.DLL")>
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<DllImport("USER32.DLL")>
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
<DllImport("USER32.DLL", SetLastError:=True)>
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, <Out()> ByRef lpdwProcessId As UInt32) As UInt32
End Function
<DllImport("USER32.DLL")>
Private Shared Function IsWindowVisible(ByVal hwnd As IntPtr) As Boolean
End Function
Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
<DllImport("USER32.DLL")>
Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
End Function
Private hShellWindow As IntPtr = GetShellWindow()
Private dictWindows As New Dictionary(Of IntPtr, String)
Private currentProcessID As Integer
Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
dictWindows.Clear()
currentProcessID = processID
EnumWindows(AddressOf enumWindowsInternal, 0)
Return dictWindows
End Function
Public Function enumWindowsInternal(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
If (hwnd <> hShellWindow) Then
Dim windowPid As UInt32
If Not IsWindowVisible(hwnd) Then
Return True
End If
Dim length As Integer = GetWindowTextLength(hwnd)
If (length = 0) Then
Return True
End If
GetWindowThreadProcessId(hwnd, windowPid)
If (windowPid <> currentProcessID) Then
Return True
End If
Dim stringBuilder As New StringBuilder(length)
GetWindowText(hwnd, stringBuilder, (length + 1))
dictWindows.Add(hwnd, stringBuilder.ToString)
End If
Return True
End Function
Sorry I'm quite new to vb.net.. but I'm learning thanks!

VB.NET Sending Strings via Postmessage

OK, this problem has been bugging me for a long time.
I have the code for the service which communicates a string to the client via PostMessage:
Public Sub SendToClient(msgs As String, types As Integer, hwnd As Long)
postMessage(hwnd, 0, Nothing, msgs)
End Sub
Then I have the client that receives the string:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If (m.Msg = 0) Then
Dim a as string
a = Marshal.PtrToStringAuto(m.LParam)
end if
MyBase.WndProc(m)
End Sub
However, the client sends an error, or some jumble of binary data or even just a blank string sometimes. By the way, the m.LParam is a number.
Can someone tell me what is the right way to send/receive strings via postmessage.
I do it like this:
Public Sub SendMessageToApp(ByVal NombreVentana As String, ByVal Mensaje As String, ByVal sender As Form)
Dim hWnd As IntPtr
Dim mCopyData As COPYDATASTRUCT
hWnd = CType(FindWindow(Nothing, NombreVentana), IntPtr)
Dim message As New System.Text.StringBuilder
If (CInt(hWnd) <> 0) Then
message.Append(Mensaje)
Dim pCopyData As IntPtr = Marshal.AllocHGlobal(message.Length() + 40)
mCopyData.lpData = Marshal.StringToHGlobalAnsi(message.ToString)
mCopyData.cbData = message.Length
mCopyData.dwData = CType(_messageID, IntPtr)
Marshal.StructureToPtr(mCopyData, pCopyData, False)
SendMessage(hWnd, WM_COPYDATA, CInt(sender.Handle), pCopyData)
Marshal.FreeHGlobal(mCopyData.lpData)
Marshal.FreeHGlobal(pCopyData)
End If
End Sub
Receiver window:
Declarations and definitions:
Const WM_COPYDATA As Integer = 74
Const SIG_LENGTH As Integer = 36
Const MAX_COPY_LENGTH As Integer = 128
Const SigConnect As String = "F7B82657-BD18-4ee6-B182-78721293821C"
Dim CDCount As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As IntPtr) As Integer
Dim hWndSender As Integer
Private Const _messageID As Integer = 10
'Estructura obligatoria para poder utilizar el API CopyData
<StructLayout(LayoutKind.Sequential)> _
Private Structure COPYDATASTRUCT
Public dwData As IntPtr
Public cbData As Integer
Public lpData As IntPtr
End Structure
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_COPYDATA Then
Dim Estructura As COPYDATASTRUCT
Dim tipo As Type = Estructura.GetType
Dim message As String
Estructura = CType(m.GetLParam(GetType(COPYDATASTRUCT)), COPYDATASTRUCT)
'Here you get the message
message = Marshal.PtrToStringAnsi(Estructura.lpData, Estructura.cbData)
End If

Get Text From Specific Textboxes From External Application - Visual Basic .Net

I can get text from external application text box but now I want to get text from my desired text box from external application.
My English is not so good that's why see Image Below.
The Below Code Return The First Text Box Value Only.
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WM_GETTEXT As Integer = &HD
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
ByVal childAfter As IntPtr, _
ByVal lclassName As String, _
ByVal windowTitle As String) As IntPtr
End Function
Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Find the running notepad window
Dim Hwnd As IntPtr = FindWindow(Nothing, TextBox1.Text)
'Alloc memory for the buffer that recieves the text
Dim Handle As IntPtr = Marshal.AllocHGlobal(100)
'send WM_GWTTEXT message to the notepad window
Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)
'copy the characters from the unmanaged memory to a managed string
Dim Text As String = Marshal.PtrToStringUni(Handle)
'Display the string using a label
Label1.Text = Text
'Find the Edit control of the Running Notepad
Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)
'Alloc memory for the buffer that recieves the text
Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
'Send The WM_GETTEXT Message
NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)
'copy the characters from the unmanaged memory to a managed string
Text = Marshal.PtrToStringUni(Hndl)
'Display the string using a label
Label2.Text = Text
End Sub
End Class
You'll have to loop through children of the main window (External Application) and get their properties.
You'll use the following:
<DllImport("User32.dll")> _
Public Function EnumChildWindows _
(ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
ByVal lParam As IntPtr) As Boolean
End Function
Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
For for details, check this How can I get properties of controls contained in a popup message box using VB.Net
' try this on excel vbe
External_application_handle=findwindow(vbNullString,"External_application")
textbox_1_handle=findwindowex(External_application_handle,0&,"Edit",vbNullString)
next_handle=textbox_1_handle
textbox_2_handle=findwindowex(External_application_handle,next_handle,"Edit",vbNullString")
Length = SendMessage(textbox_2_handle, WM_GETTEXTLENGTH, 0,0)
buffer$=space(Length)
call sendmessage(textbox_2_handle,Length+1,buffer$)
msgbox buffer

Find Specific Open File Dialog and populate

I'm trying to find a method to identify a Open File Dialog and send a message to the "file name" field. Afterwards it needs to send an "Enter" key or "Open" command to the button.
I'm doing this in VB, but I'm sure I can cope if someone can help in C# as well.
I've been digging through the API this entire day and came up with a couple of possibilities but I am unfamiliar with how to implement this in DotNet4.
I used to work with API in VB6 but it seems like things are a bit different now.
If someone could provide me with a small example I'd be grateful.
Some of the API I've looked at is FindWindow and FindWindowEx.
Edit:
I found some code that is worth looking at. This code needs to be used inside a module.
I will post more as I find more answers.
Imports System.Runtime.InteropServices
Imports System.Text
Module modEnumWindows
Private windowList As New ArrayList
Private errMessage As String
Public Delegate Function MyDelegateCallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Declare Function EnumWindows Lib "user32" (ByVal x As MyDelegateCallBack, ByVal y As Integer) As Integer
Declare Auto Function GetClassName Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpClassName As System.Text.StringBuilder, _
ByVal nMaxCount As Integer) As Integer
Declare Auto Function GetWindowText Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal lpClassName As System.Text.StringBuilder, _
ByVal nMaxCount As Integer) As Integer
Private Function EnumWindowProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
'working vars
Dim sTitle As New StringBuilder(255)
Dim sClass As New StringBuilder(255)
Try
Call GetClassName(hwnd, sClass, 255)
Call GetWindowText(hwnd, sTitle, 255)
windowList.Add(sClass.ToString & ", " & hwnd & ", " & sTitle.ToString)
Catch ex As Exception
errMessage = ex.Message
EnumWindowProc = False
Exit Function
End Try
EnumWindowProc = True
End Function
Public Function getWindowList(ByRef wList As ArrayList, Optional ByVal errorMessage As String = "") As Boolean
windowList.Clear()
Try
Dim del As MyDelegateCallBack
del = New MyDelegateCallBack(AddressOf EnumWindowProc)
EnumWindows(del, 0)
getWindowList = True
Catch ex As Exception
getWindowList = False
errorMessage = errMessage
Exit Function
End Try
'wList.Clear()
wList = windowList
End Function
End Module
By using this you'll be able to identify the Window Text, HWND and Class. Hope this helps people a bit. The next step for me will be to identify the field I wish to send the data to.