8 messages in net.java.dev.jna.usersRe: [jna-users] Invoking WinINet Inte...
FromSent OnAttachments
Rogan DawesApr 28, 2008 9:39 am 
Albert StrasheimApr 28, 2008 1:04 pm 
Timothy WallApr 28, 2008 1:22 pm 
Rogan DawesApr 28, 2008 1:29 pm 
Rogan DawesApr 28, 2008 1:30 pm 
Timothy WallApr 28, 2008 1:45 pm 
Rogan DawesApr 28, 2008 3:21 pm 
Timothy WallApr 28, 2008 5:17 pm 
Actions with this message:
Paste this link in email or IM:
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:Rogan Dawes (lis@dawes.za.net)
Date:Apr 28, 2008 3:21:57 pm
List:net.java.dev.jna.users

Timothy Wall wrote:

On Apr 28, 2008, at 12:40 PM, Rogan Dawes wrote:

INTERNET_PER_CONN_OPTION and INTERNET_PER_CONN_OPTION_LIST are defined as:

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 type of pOptions should be Pointer or StructureByReference, which will make it 4 bytes and given the structure a total size of 20 bytes.

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; }

Change pOptions to be Pointer, and use structs[0].getPointer() to initialize it.

Hi Timothy,

Thanks for your help. That change, along with completing the Union for INTERNET_PER_CONN_OPTION (it also has a FILETIME that I had omitted, thinking it wouldn't make any difference, but it did) allows me to execute the InternetQueryOption call successfuly, and get a "true" return value.

However, the INTERNET_PER_CONN_OPTION[] are never populated, other than by me. i.e. the JNI call makes no change to the values of the INTERNET_PER_CONN_OPTION elements.

I'm not sure whether I should be treating the "LPTSTR pszValue" as a Pointer or as a WString. This memory is allocated by the InternetQueryOption call, and I am therefore responsible for freeing it myself. Whichever approach I take, however, the value is still null.

Any suggestions are welcome.

Thanks

Rogan

Here is my current code:

------ snip ------ 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.LongByReference; import com.sun.jna.win32.StdCallLibrary;

public class WindowsProxy {

public static void getCurrentProxyOptions() { INTERNET_PER_CONN_OPTION option = new INTERNET_PER_CONN_OPTION(); INTERNET_PER_CONN_OPTION[] options = (INTERNET_PER_CONN_OPTION[]) option.toArray(1); options[0].dwOption = INTERNET_PER_CONN_PROXY_SERVER;

INTERNET_PER_CONN_OPTION_LIST list = new INTERNET_PER_CONN_OPTION_LIST(); list.dwOptionCount = options.length; list.dwOptionError = 0; list.pOptions = options[0].getPointer(); list.dwSize = list.size(); LongByReference size = new LongByReference(list.size());

System.out.println("About to call InternetQueryOption");

if (!WinINet.INSTANCE.InternetQueryOptionW(null, INTERNET_OPTION_PER_CONNECTION_OPTION, list.getPointer(), size)) { throw new RuntimeException("error calling JNI"); }

System.out.println("Called InternetQueryOption");

options[0].value.setType(Pointer.class); System.out.println(options[0].value.pszValue.getString(0));

}

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 FILETIME extends Structure { public int dwLowDateTime; public int dwHighDateTime; } public static class Value extends Union { public int dwValue; public Pointer pszValue; public FILETIME ftValue; } 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 Pointer pOptions; }

private interface WinINet extends StdCallLibrary {

WinINet INSTANCE = (WinINet) Native.loadLibrary("wininet", WinINet.class);

boolean InternetQueryOptionW(Pointer unused, int dwOption, Pointer lpBuffer, LongByReference size);

}

public static void main(String[] args) { Native.setProtected(true); getCurrentProxyOptions(); } }