I have a problem when converting a VB code to C# code. Here is the code in VB,
<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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label4.Text = (TextBox1.Text & ":" & TextBox2.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
UseProxy(Label4.Text)
WebBrowser1.Navigate(TextBox3.Text)
End Sub
I have tried using some online converters to convert this code and i got the same results. This is the c# code,
[Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)]
private static bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength)
{
}
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
}
private void UseProxy(string strProxy)
{
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
Struct_INTERNET_PROXY_INFO struct_IPI = default(Struct_INTERNET_PROXY_INFO);
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
Label4.Text = (TextBox1.Text + ":" + TextBox2.Text);
}
private void Button2_Click(System.Object sender, System.EventArgs e)
{
UseProxy(Label4.Text);
WebBrowser1.Navigate(TextBox3.Text);
}
I am using VS 12 and in a windows form application the code gives me a error saying,
Error 2 'WindowsFormsApplication1.Form1.InternetSetOption(System.IntPtr, int, System.IntPtr, int)': not all code paths return a value
Error 1 The type or namespace name 'Runtime' could not be found (are you missing a using directive or an assembly reference?)
I have tried using System.Runtime
, using System.Runtime.InteropServices
and System.Runtime.InteropServices.WindowsRuntime
but no use.
I have a console application which sends httpwebrequest and do some work. So I somehow need the program to use proxy when connecting. So when the program starts, it will also start the proxy and all the requests the program send will be using proxy.
I also want the proxy to just affect the program and not all the computer as I want the user to be able to browse the computer freely.
--Edit-- I have got a solution for above. Now this is the problem. Now this code doesn't seem to work with console app. I used
static void Main()
{
Console.WriteLine("Ip before Proxy /r/n");
HTTPGet req = new HTTPGet();
req.Request("http://checkip.dyndns.org");
string[] a = req.ResponseBody.Split(':');
string a2 = a[1].Substring(1);
string[] a3 = a2.Split('<');
string a4 = a3[0];
Console.WriteLine(a4);
UseProxy("219.93.183.106:8080");
Console.WriteLine("Ip after Proxy /r/n");
HTTPGet req1 = new HTTPGet();
req1.Request("http://checkip.dyndns.org");
string[] a1 = req1.ResponseBody.Split(':');
string a21 = a1[1].Substring(1);
string[] a31 = a21.Split('<');
string a41 = a31[0];
Console.WriteLine(a41);
Console.ReadLine();
}
I get both the results as my IP. The HTTPGet is a class from this question in stack overflow : c# - Get public/external IP address?
Any help on how to use it on console ? or am I doing something wrong ?