

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
8 messages in net.java.dev.jna.usersRe: [jna-users] Invoking WinINet Inte...| From | Sent On | Attachments |
|---|---|---|
| Rogan Dawes | Apr 28, 2008 9:39 am | |
| Albert Strasheim | Apr 28, 2008 1:04 pm | |
| Timothy Wall | Apr 28, 2008 1:22 pm | |
| Rogan Dawes | Apr 28, 2008 1:29 pm | |
| Rogan Dawes | Apr 28, 2008 1:30 pm | |
| Timothy Wall | Apr 28, 2008 1:45 pm | |
| Rogan Dawes | Apr 28, 2008 3:21 pm | |
| Timothy Wall | Apr 28, 2008 5:17 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Invoking WinINet InternetQueryOption/InternetSetOption? | Actions... |
|---|---|---|
| From: | Timothy Wall (twal...@dev.java.net) | |
| Date: | Apr 28, 2008 1:22:51 pm | |
| List: | net.java.dev.jna.users | |
Don't use an array of Structure as a field unless you want the array inlined. Java arrays are *not* interchangeable with native pointers in the context of structure fields.
On Apr 28, 2008, at 12:40 PM, Rogan Dawes wrote:
Hi folks,
I am trying to use JNA to control the Windows system-wide proxy settings.
To do this, you use the WinINet InternetQueryOption to see what the current proxy values are, and InternetSetOption to update them. This causes an immediate change to the proxy values.
A C example follows (from http://support.microsoft.com/kb/226473):
---- snip -----
INTERNET_PER_CONN_OPTION_LIST List; INTERNET_PER_CONN_OPTION Option[5]; unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL; Option[1].dwOption = INTERNET_PER_CONN_AUTODISCOVERY_FLAGS; Option[2].dwOption = INTERNET_PER_CONN_FLAGS; Option[3].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; Option[4].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); List.pszConnection = NULL; List.dwOptionCount = 5; List.dwOptionError = 0; List.pOptions = Option;
if(!InternetQueryOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, &nSize)) printf("InternetQueryOption failed! (%d)\n", GetLastError()); if(Option[0].Value.pszValue != NULL) printf("%s\n", Option[0].Value.pszValue);
if((Option[2].Value.dwValue & PROXY_TYPE_AUTO_PROXY_URL) == PROXY_TYPE_AUTO_PROXY_URL) printf("PROXY_TYPE_AUTO_PROXY_URL\n");
if((Option[2].Value.dwValue & PROXY_TYPE_AUTO_DETECT) == PROXY_TYPE_AUTO_DETECT) printf("PROXY_TYPE_AUTO_DETECT\n");
INTERNET_VERSION_INFO Version; nSize = sizeof(INTERNET_VERSION_INFO);
InternetQueryOption(NULL, INTERNET_OPTION_VERSION, &Version, &nSize);
if(Option[0].Value.pszValue != NULL) GlobalFree(Option[0].Value.pszValue);
if(Option[3].Value.pszValue != NULL) GlobalFree(Option[3].Value.pszValue);
if(Option[4].Value.pszValue != NULL) GlobalFree(Option[4].Value.pszValue);
---- snip ----
INTERNET_PER_CONN_OPTION and INTERNET_PER_CONN_OPTION_LIST are defined as:
typedef struct { DWORD dwOption; union { DWORD dwValue; LPTSTR pszValue; FILETIME ftValue; } Value; } INTERNET_PER_CONN_OPTION, *LPINTERNET_PER_CONN_OPTION;
typedef struct { DWORD dwSize; LPTSTR pszConnection; DWORD dwOptionCount; DWORD dwOptionError; LPINTERNET_PER_CONN_OPTION pOptions; } INTERNET_PER_CONN_OPTION_LIST, *LPINTERNET_PER_CONN_OPTION_LIST;
The function signature for InternetQueryOption is (from http://msdn2.microsoft.com/en-us/library/aa385101(VS.85).aspx):
BOOL InternetQueryOption( __in HINTERNET hInternet, __in DWORD dwOption, __out LPVOID lpBuffer, __inout LPDWORD lpdwBufferLength );
I tried the following code, but I just keep getting VM crashes (EXCEPTION_ACCESS_VIOLATION) when I run it :-(
------- SSCCE follows ------------------
import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.Structure; import com.sun.jna.Union; import com.sun.jna.WString; import com.sun.jna.ptr.IntByReference; import com.sun.jna.win32.StdCallLibrary;
public class WindowsProxy {
public static void getProxy() { INTERNET_PER_CONN_OPTION option = new INTERNET_PER_CONN_OPTION(); Structure[] structs = option.toArray(1); INTERNET_PER_CONN_OPTION[] options = (INTERNET_PER_CONN_OPTION[]) structs; options[0].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
INTERNET_PER_CONN_OPTION_LIST list = new INTERNET_PER_CONN_OPTION_LIST(); list.dwOptionCount = options.length; list.dwOptionError = 0; list.pOptions = options; list.dwSize = list.size(); IntByReference size = new IntByReference(list.size());
System.out.println("About to call InternetQueryOption"); if (!WinINet.INSTANCE.InternetQueryOptionW(null, INTERNET_OPTION_PER_CONNECTION_OPTION, list, size)) throw new RuntimeException("error"); System.out.println("Called InternetQueryOption");
options[0].value.setType(String.class); System.out.println(options[0].value.pszValue); }
// // PER_CONN_FLAGS // private static int PROXY_TYPE_DIRECT = 0x00000001; // direct to net private static int PROXY_TYPE_PROXY = 0x00000002; // via named proxy private static int PROXY_TYPE_AUTO_PROXY_URL = 0x00000004; // autoproxy URL private static int PROXY_TYPE_AUTO_DETECT = 0x00000008; // use autoproxy detection
// // Options used in INTERNET_PER_CONN_OPTION struct // private static int INTERNET_PER_CONN_FLAGS = 1; private static int INTERNET_PER_CONN_PROXY_SERVER = 2; private static int INTERNET_PER_CONN_PROXY_BYPASS = 3; private static int INTERNET_PER_CONN_AUTOCONFIG_URL = 4; private static int INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;
private static int INTERNET_OPTION_PER_CONNECTION_OPTION = 75;
public static class INTERNET_PER_CONN_OPTION extends Structure {
public int dwOption; public static class Value extends Union { public int dwValue; public String pszValue; } public Value value; }
public static class INTERNET_PER_CONN_OPTION_LIST extends Structure { public int dwSize; public WString pszConnection; public int dwOptionCount; public int dwOptionError; public INTERNET_PER_CONN_OPTION[] pOptions; }
private interface WinINet extends StdCallLibrary {
WinINet INSTANCE = (WinINet) Native.loadLibrary("wininet", WinINet.class);
boolean InternetQueryOptionW(Pointer unused, int dwOption, INTERNET_PER_CONN_OPTION_LIST lpBuffer, IntByReference size); }
public static void main(String[] args) { getProxy(); } }
---- snip ----
Any help getting this working would be MUCH appreciated.
Thanks!
Rogan







