VB.net proxy exception - vb.net

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.

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?

Set Proxy Credential in Web Browser using vb.net

hi i have a problem during navigate in webbrowser using private proxy
i can navigate with a public proxy with this script but i can't do this with proxy authentification (ip:port:user:pass)
''''''''''''''''''''''proxy'''''''''''''''''''''''
Friend Structure Struct_INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public proxy As IntPtr
Public proxyBypass As IntPtr
End Structure
<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
Private Sub RefreshIESettings(ByVal strProxy As String)
Const INTERNET_OPTION_PROXY = 38
Const INTERNET_OPEN_TYPE_PROXY = 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 = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI))
Marshal.StructureToPtr(struct_IPI, intptrStruct, True)
Dim iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI))
End Sub
''''''''''''''''''''''End proxy'''''''''''''''''''''''*
any help please

Puzzling Output When Reading Memory Address In VB

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?

Using different proxies on different WebBrowsers simultaneously

as the title I was wondering if there was a method to use different proxy on various WebBrowser simultaneously.
At first I tried to insert 5 WebBrowser and create 5 threads, and each performs this sub (which I found on the internet) to change proxy:
Public Structure Struct_INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public proxy As IntPtr
Public proxyBypass As IntPtr
End Structure
Private Declare Auto Function InternetSetOption Lib "wininet.dll" (ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal lpdwBufferLength As Integer) As Boolean
Private Sub RefreshIESettings(ByVal strProxy As String)
Const INTERNET_OPTION_PROXY As Integer = 38
Const INTERNET_OPEN_TYPE_PROXY As Integer = 3
Dim s_IPI As Struct_INTERNET_PROXY_INFO
s_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY
s_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy)
s_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("Global")
Dim intptrStruct As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(s_IPI))
System.Runtime.InteropServices.Marshal.StructureToPtr(s_IPI, intptrStruct, True)
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(s_IPI))
End Sub
but it was a failure because the ip was found to be the same or in some cases were the same and 4 ip 1 different.

How can I set a proxy with authentication (username & password) on the webbrowser control in VB.net?

I'm already using InternetSetOption and wininet to change the proxy settings for Internet Explorer which works since the webbrowser control uses it too, but how can I add it so that it can use authentication-required proxies (user:pass)?
Here's my code:
Public Structure Struct_INTERNET_PROXY_INFO
Public dwAccessType As Integer
Public proxy As IntPtr
Public proxyBypass As IntPtr
End Structure
Private Sub setProxy(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
Private Declare Auto Function InternetSetOption Lib "wininet.dll" (ByVal hInternet As IntPtr, ByVal dwOption As Integer, ByVal lpBuffer As IntPtr, ByVal lpdwBufferLength As Integer) As Boolean
And this is what I use whenever I want to change the proxy:
setProxy("ip:port")
So how can I add support for authentication?