Puzzling Output When Reading Memory Address In VB - vb.net

So I have this code
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Const PROCESS_WM_READ As Integer = &H10
<DllImport("kernel32.dll")>
Public Shared Function OpenProcess(dwDesiredAccess As Integer, bInheritHandle As Boolean, dwProcessId As Integer) As IntPtr
End Function
<DllImport("kernel32.dll")>
Public Shared Function ReadProcessMemory(hProcess As Integer, lpBaseAddress As Integer, lpBuffer As Byte(), dwSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim notepadProcess As Process = Process.GetProcessesByName("GeometryDash")(0)
Dim processHandle As IntPtr = OpenProcess(PROCESS_WM_READ, False, notepadProcess.Id)
Dim bytesRead As Integer = 0
Dim buffer As Byte() = New Byte(23) {}
'The address in this line is hard-coded. Use whatever is appropriate for your situation.
ReadProcessMemory(CInt(processHandle), 117317672, buffer, buffer.Length, bytesRead)
MsgBox(Encoding.Unicode.GetString(buffer))
End Sub
End Class
It outputs 2 or 3 weird Japanese characters, the address "117317672" is "06FE2028" in decimal. I think it's not liking how I'm giving it the address, how should I put in the address?

Related

DLLImport vs DinamicInvoke or Method.Invoke in VB.NET

I want to call function MoveFile from "Kernel32.dll".
1)First approach:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("KERNEL32.DLL", EntryPoint:="MoveFileW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function MoveFile(ByVal src As String, ByVal dst As String) As Boolean
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim src As String = "C:\Users\Baha1990\Documents\Visual Studio 2017\Projects\WindowsApp1\TEST FILE\1\MSACCESS.accdb"
Dim dst As String = "C:\Users\Baha1990\Documents\Visual Studio 2017\Projects\WindowsApp1\TEST FILE\2\MSACCESS.accdb"
Dim RetVal As Boolean = MoveFile(src, dst)
End Sub
End Class
It works pretty well
Second approach:
Imports System.Runtime.InteropServices
Public Class Form1
<Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
<Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Ansi, ExactSpelling:=True)> Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal procName As String) As IntPtr
End Function
<UnmanagedFunctionPointer(CallingConvention.StdCall)>
Public Delegate Function _MoveFile(ByVal src As String, ByVal dst As String) As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim src As String = "C:\Users\Baha1990\Documents\Visual Studio 2017\Projects\WindowsApp1\TEST FILE\1\MSACCESS.accdb"
Dim dst As String = "C:\Users\Baha1990\Documents\Visual Studio 2017\Projects\WindowsApp1\TEST FILE\2\MSACCESS.accdb"
Dim pDll As IntPtr = IntPtr.Zero
pDll = LoadLibrary("kernel32.dll")
Dim pProc2D = GetProcAddress(pDll, "MoveFileW")
Dim MoveFile = Marshal.GetDelegateForFunctionPointer(pProc2D, GetType(_MoveFile))
Dim RetVal = MoveFile.DynamicInvoke(src, dst) 'Here I get boolean as false it should be true
or
Dim RetVal = MoveFile.Method.Invoke(What to pass here in my case?)
End Sub
End Class
So Question: What is difference between DLLImport and DynamicInvoke or Method.Invoke, and why I have fail with DynamicInvoke and what params to pass to Method.Invoke?
Ok, in second approach I figure out that in this line:
<UnmanagedFunctionPointer(CallingConvention.StdCall)>
Needs additional params, after I edited as:
<UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError:=True, CharSet:=CharSet.Unicode)>
Dim RetVal = MoveFile.DynamicInvoke(src, dst) - DynamicInvoke works perfect.
So question what to pass as params to Method.Invoke still open.
Ok, I also figured out about Method.Invoke:
Dim param = {src, dst}
Dim RetVal = MoveFile.Method.Invoke(MoveFile, param) - works perfect.
So seems all three aproach works and gives same result, but what advantage gives these methods between each other. Who can explain?

How to set the Explorer window of a specific file as a child window of TopMost form?

I made some research, but I can't find something really "interesting". I tried my best to find any kind of documentation or questions that are closest to my case as following:
How to find main window title name of application
how to get the window title of a process
How to get the Title Bar Text by its Process Id
getting the name of a process
How do I get list of Process Names running
Check to see if process is running
How To Get Process Owner ID
How to get the title/name of the last active window?
Get Process ID from Window Title
and also
Process.GetProcessesByName Method
The code I am using to open the process window
Private Async Function ParentMethod() As Task
Dim filePath As String = Await Task.Run(
Function()
Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ".mp3",
SearchOption.AllDirectories).FirstOrDefault()
End Function)
If Not String.IsNullOrEmpty(filePath) Then
LinkLabel1.Text = "File exist already"
LinkLabel1.Visible = True
PictureBox7.Visible = True
Else
MsgBox("it doesn't exist")
End If
End Function
and the helper class
Imports System.IO
Imports System.Runtime.InteropServices
Public Class NativeMethods
<DllImport("shell32.dll", SetLastError:=True)>
Private Shared Function SHOpenFolderAndSelectItems(
pidlFolder As IntPtr, cidl As UInteger,
<[In], MarshalAs(UnmanagedType.LPArray)> apidl As IntPtr(),
dwFlags As UInteger) As Integer
End Function
<DllImport("shell32.dll", SetLastError:=True)>
Private Shared Sub SHParseDisplayName(
<MarshalAs(UnmanagedType.LPWStr)> name As String,
bindingContext As IntPtr, <Out> ByRef pidl As IntPtr,
sfgaoIn As UInteger, <Out> ByRef psfgaoOut As UInteger)
End Sub
Public Shared Sub OpenFolderAndSelectFile(filePath As String)
Dim dirPath As String = Path.GetDirectoryName(filePath)
Dim fileName As String = Path.GetFileName(filePath)
OpenFolderAndSelectFile(dirPath, fileName)
End Sub
Public Shared Sub OpenFolderAndSelectFile(dirPath As String, fileName As String)
Dim nativeFolder As IntPtr
Dim psfgaoOut As UInteger
SHParseDisplayName(dirPath, IntPtr.Zero, nativeFolder, 0, psfgaoOut)
If nativeFolder = IntPtr.Zero Then
' Log error, can't find folder
Return
End If
Dim nativeFile As IntPtr
SHParseDisplayName(Path.Combine(dirPath, fileName),
IntPtr.Zero, nativeFile, 0, psfgaoOut)
Dim fileArray As IntPtr()
If nativeFile = IntPtr.Zero Then
' Open the folder without the file selected if we can't find the file
fileArray = New IntPtr(-1) {}
Else
fileArray = New IntPtr() {nativeFile}
End If
SHOpenFolderAndSelectItems(nativeFolder, CUInt(fileArray.Length), fileArray, 0)
Marshal.FreeCoTaskMem(nativeFolder)
If nativeFile <> IntPtr.Zero Then
Marshal.FreeCoTaskMem(nativeFile)
End If
End Sub
End Class
then calling it with
NativeMethods.OpenFolderAndSelectFile(filepath,filename & "extension"))
Since I am opening the process this way and NOT with Process class, almost all of them are not suitable to be considered for my case as many of them refer to notepad, while I think the explorer window title and ID changes for every file ( obviously), while "notepad" process, stay "notepad".
I also tried BringToFront, but this latter moves a control in front of other controls, but in this case Explorer is not a control, right?
The least I want to do is to
Get a list of active windows & their process names
as It will waste memory and time usage for no reason as I will need to "filter" process to find my process.
Hope we can find a solution to this, Thanks in advance.
Mattia
This is the solution to it using FindWindowW e SetWindowPos Api.
It is showing Explorer folder on top of top most form.
<DllImport("user32.dll", EntryPoint:="FindWindowW")>
Public Shared Function FindWindowW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpClassName As String, <MarshalAs(UnmanagedType.LPTStr)> ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")>
Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As Boolean
End Function
Shared ReadOnly HWND_TOPMOST As IntPtr = New IntPtr(-1)
Const SWP_NOSIZE As UInt32 = &H1
Const SWP_NOMOVE As UInt32 = &H2
Const SWP_SHOWWINDOW As UInt32 = &H40
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim inptr = FindWindowW("CabinetWClass", Nothing)
SetWindowPos(inptr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
End Sub

load image in picturebox quickly

I have a program that shows previews of jpgs in small pictureboxes. The loading of these images is slow, and takes almost a second for each. That is because the images are big (8/9 mB). I would need to load them quickly, for example by loading a thumb of the picture. I would like to avoid putting all into the memory since there could be hundreds of pictures.
what is your advice on this?
THanks
You need to resize the pictures in advance.
Creating tumbnails would be equally slow because you need to read the whole file, before you can even start making the tumbnail.
What you could do is, just like windows, create a tumbnail 'data base', where you store the tumbs for each picture. And only use the full size picture if needed.
So if you have like,
picture001.jpg
picture002.jpg
picture003.jpg
Create tumbs for each one;
picture001.jpg
picture001_tumb.jpg
picture002.jpg
picture002_tumb.jpg
picture003.jpg
picture003_tumb.jpg
So in the loading of the picuters detect, if the _tumb.jpg is there, if not create and store it. Which ofcourse, needs to be done in a background worker, because you need your main app to responsive...
You can also use this code, to request the icon from the windows shell;
Imports System.Runtime.InteropServices
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Text
' http://www.vbforums.com/showthread.php?617626-How-do-I-extract-a-256X256-image-out-of-an-icon&highlight=IShellItemImageFactory
Module GetShellIcon
<Flags()> _
Public Enum SIIGBF
SIIGBF_RESIZETOFIT = 0
SIIGBF_BIGGERSIZEOK = 1
SIIGBF_MEMORYONLY = 2
SIIGBF_ICONONLY = 4
SIIGBF_THUMBNAILONLY = 8
SIIGBF_INCACHEONLY = 16
End Enum
Public Enum SIGDN As UInteger
NORMALDISPLAY = 0
PARENTRELATIVEPARSING = &H80018001UI
PARENTRELATIVEFORADDRESSBAR = &H8001C001UI
DESKTOPABSOLUTEPARSING = &H80028000UI
PARENTRELATIVEEDITING = &H80031001UI
DESKTOPABSOLUTEEDITING = &H8004C000UI
FILESYSPATH = &H80058000UI
URL = &H80068000UI
End Enum
<ComImportAttribute(), GuidAttribute("bcc18b79-ba16-442f-80c4-8a59c30c463b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IShellItemImageFactory
Sub GetImage(ByVal size As SIZE, ByVal flags As SIIGBF, ByRef phbm As IntPtr)
End Interface
<ComImport()> <Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IShellItem
Sub BindToHandler(ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)> ByVal bhid As Guid, <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, ByRef ppv As IntPtr)
Sub GetParent(ByRef ppsi As IShellItem)
Sub GetDisplayName(ByVal sigdnName As SIGDN, ByRef ppszName As IntPtr)
Sub GetAttributes(ByVal sfgaoMask As UInt32, ByRef psfgaoAttribs As UInt32)
Sub Compare(ByVal psi As IShellItem, ByVal hint As UInt32, ByRef piOrder As Integer)
End Interface
<DllImport("shell32.dll", CharSet:=CharSet.Unicode, PreserveSig:=False)> _
Public Sub SHCreateItemFromParsingName(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPath As String, ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, <MarshalAs(UnmanagedType.Interface, IidParameterIndex:=2)> ByRef ppv As IShellItem)
End Sub
<StructLayout(LayoutKind.Sequential)> _
Public Structure SIZE
Public cx As Integer
Public cy As Integer
Public Sub New(ByVal cx As Integer, ByVal cy As Integer)
Me.cx = cx
Me.cy = cy
End Sub
End Structure
Public Function GetShellIcon(ByVal Path As String, MySIIGBF As SIIGBF, Optional ByVal Width As Integer = 256, Optional ByVal Height As Integer = 256) As Bitmap
Dim ppsi As IShellItem = Nothing
Dim hbitmap As IntPtr = IntPtr.Zero
Dim uuid As New Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")
Dim bs As Bitmap
SHCreateItemFromParsingName(Path, IntPtr.Zero, uuid, ppsi)
DirectCast(ppsi, IShellItemImageFactory).GetImage(New SIZE(Width, Height), MySIIGBF, hbitmap)
bs = System.Drawing.Bitmap.FromHbitmap(hbitmap)
bs.MakeTransparent(Color.Black)
Return bs
End Function
End Module

VB.net proxy exception

My code using proxy works but when the proxy is invalid or restricted my program directly uses my IP address, i used a web browser that directly checks my ip, is there any way that i could set an exception whenever the proxy i used is invalid or restricted?
here is my working code:
Imports System.Runtime.InteropServices
#Region "Using Proxy"
<Runtime.InteropServices.DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal lpdwBufferLength As Integer) As Boolean
End Function
Public Structure Struct_INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public proxy As IntPtr
Public proxyBypass As IntPtr
End Structure
Private Sub UseProxy(ByVal strProxy As String)
Const INTERNET_OPTION_PROXY As Integer = 38
Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
Dim struct_IPI As Struct_INTERNET_PROXY_INFO
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy)
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local")
Dim intptrStruct As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI))
Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
Dim iReturn As Boolean = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI))
End Sub
#End Region
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UseProxy(ProxyText.Text)
WebBrowser1.Navigate("ipchicken.com")
End Sub
Try the solution below:
Another Stackoverflow Answer
I believe if you bind the address to that specific ip it wont work , if the ip is invalid or not working, and wont use your systems default.

VB 2010 SendMessage: sending binary data to another app

I'm trying to send structured data from a VB 2010 app that I'm developing to an existing library which expects strings to be converted into byte arrays. I'm having trouble with the sending the data as byte arrays - I can send the plain string that should be converted to bytes to a test program that I've written.
Here are the two apps. Firstly the listener process:
Imports System.Runtime.InteropServices
Public Class frmTest
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = frmTest.WM_COPYDATA Then
Dim data As CopyData
Dim message As String
' get the data...
data = CType(m.GetLParam(GetType(CopyData)), CopyData)
message = data.lpData
' add the message
txtTest.Text = message
' let them know we processed the message...
m.Result = New IntPtr(1)
Else
MyBase.WndProc(m)
End If
End Sub
Private Function UnicodeBytesToString(ByVal bytes() As Byte) As String
Return System.Text.Encoding.Unicode.GetString(bytes)
End Function
Private Const WM_COPYDATA As Integer = &H4A
<StructLayout(LayoutKind.Sequential)> _
Private Structure CopyData
Public dwData As IntPtr
Public cbData As Integer
Public lpData As String
End Structure
End Class
Secondly the process that sends the data:
Imports System.Runtime.InteropServices
Imports System.Collections.Generic
Public Class frmMain
<StructLayout(LayoutKind.Sequential)> _
Private Structure CopyData
Public dwData As IntPtr
Public cbData As Integer
Public lpData As String
End Structure
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
Private Declare Auto Function FindWindow Lib "user32" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
Private Const WM_COPYDATA As Integer = &H4A
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Dim ClientWindow As IntPtr
Dim a() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
For Each p In a
Console.WriteLine(p.ProcessName)
If p.ProcessName = "Listener" Then
ClientWindow = p.MainWindowHandle
Exit For
End If
Next
' make sure we found an active client window
If Not ClientWindow.Equals(IntPtr.Zero) Then
' if there is text to send
If txtText.Text.Length > 0 Then
Dim message As String = txtText.Text
Dim data As CopyData
' set up the data...
data.lpData = message
data.cbData = message.Length * Marshal.SystemDefaultCharSize
' send the data
frmMain.SendMessage(ClientWindow, frmMain.WM_COPYDATA, Me.Handle, data)
End If
Else
MsgBox("Could Not Find Active Client Window.")
End If
End Sub
Private Function UnicodeStringToBytes(
ByVal str As String) As Byte()
Return System.Text.Encoding.Unicode.GetBytes(str)
End Function
End Class
This all works but if I change 'Public lpData As String' to 'Public lpData As Byte()' in both and then amend 'data.lpData = message' to 'data.lpData = UnicodeStringToBytes(message)' in the sender process and 'message = data.lpData' to 'message = UnicodeBytesToString(data.lpData)' in the listener process it crashes.
How can I send a string encoded as a byte array from the sender to the listener so that the listener can decode it back to a string ?
I realise it would be easier to send the string as a string but the existing library needs it as a byte array so I'm trying to get my sender working against this test listener where I can see what's happening.
Thanks in advance !
Variable-length arrays in structures are always a pain.
Declare lpData as IntPtr in both applications.
Then in the sending app:
' set up the data...
Dim string_bytes = UnicodeStringToBytes(message)
Dim pinned = GCHandle.Alloc(string_bytes, GCHandleType.Pinned)
Try
data.dwData = New IntPtr(message.Length)
data.cbData = string_bytes.Length
data.lpData = pinned.AddrOfPinnedObject()
' send the data
frmMain.SendMessage(ClientWindow, frmMain.WM_COPYDATA, Me.Handle, data)
Finally
If pinned.IsAllocated Then pinned.Free()
End Try
In the receiving app:
' get the data...
data = CType(m.GetLParam(GetType(CopyData)), CopyData)
Dim message(0 To data.cbData - 1) As Byte
Marshal.Copy(data.lpData, message, 0, data.cbData)
' add the message
txtTest.Text = UnicodeBytesToString(message)