Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are calling <code>WSAStartup()</code> twice, which will require you to call <code>WSACleanup()</code> twice to unload WinSock correctly. You should only be calling <code>WSAStartup()</code> once.</p> <p>The <code>szProtocol</code> member of the <code>WSAPROTOCOL_INFO</code> structure is an array of <code>TCHAR</code> elements. <code>TCHAR</code> maps to <code>char</code> or <code>wchar_t</code> depending on whether the calling app is compiled for ANSI/MBCS or UNICODE. That is why there is no <code>WSAEnumProtocols()</code> function in ws2_32.dll. There are separate <code>WSAEnumProtocolsA()</code> (for Ansi) and <code>WSAEnumProtocolsW()</code> (for Unicode) functions instead. Since Java uses Unicode strings, you should have your JNA code use <code>WSAEnumProtocolsW()</code>. <code>WSAStartup()</code> does not use <code>TCHAR</code>, only <code>char</code>, which is why there are no separate <code>WSAStartupA()</code> and <code>WSAStartupW()</code> functions for it.</p> <p>If your JNA code is not able to get <code>WSAEnumProtocols()</code> and <code>WSAGetLastError()</code> working correctly, its likely that you are are declaring/using them wrong, but you did not show any of that code, so nobody can say for sure why it is not working for you.</p> <p><strong>Update:</strong> try something like this (I don't use JNA, so this may need some tweaking, but this will give you the general idea):</p> <pre><code>public interface Ws2_32 extends Library { // I don't know how to declare fixed size arrays in JNA, // so you will have to adjust these Structue declarations // as needed... public static class WSAData extends Structure { short wVersion; short wHighVersion; byte szDescription[257]; byte szSystemStatus[129]; short iMaxSockets; short iMaxUdpDg; String lpVendorInfo; }; public static class WSAPROTOCOLCHAIN extends Structure { int ChainLen; int ChainEntries[7]; }; public static class WSAPROTOCOL_INFOW extends Structure { int dwServiceFlags1; int dwServiceFlags2; int dwServiceFlags3; int dwServiceFlags4; int dwProviderFlags; GUID ProviderId; int dwCatalogEntryId; WSAPROTOCOLCHAIN ProtocolChain; int iVersion; int iAddressFamily; int iMaxSockAddr; int iMinSockAddr; int iSocketType; int iProtocol; int iProtocolMaxOffset; int iNetworkByteOrder; int iSecurityScheme; int dwMessageSize; int dwProviderReserved; char szProtocol[256]; }; Ws2_32 INSTANCE = (Ws2_32) Native.loadLibrary("ws2_32", Ws2_32.class); int WSAStartup(short version, WSADATA lpwsaData); int WSAEnumProtocolsW(int[] lpiProtocols, WSAPROTOCOL_INFOW[] lpProtocolBuffer, IntByReference lpdwBufferLength); int WSACleanup(); int WSAGetLastError(); int WSASetLastError(int iError); } </code></pre> <p>.</p> <pre><code>public static void main(String[] args){ WSADATA wsaData = new WSADATA(); short version = 2; int result = Ws2_32.INSTANCE.WSAStartup(version, wsaData); System.out.println("WSAStartup() returned: " + result); if(result == 0){ System.out.println("WSADATA struct:"); System.out.println("wVersion: " + wsaData.wVersion); System.out.println("wHighVersion: " + wsaData.wHighVersion); System.out.print("szDescription: "); for(byte b : wsaData.szDescription){ System.out.print((char) b); } System.out.print("\n"); System.out.print("szSystemStatus: "); for(byte b : wsaData.szSystemStatus){ System.out.print((char) b); } System.out.print("\n"); System.out.println("iMaxSockets: " + wsaData.iMaxSockets); System.out.println("iMaxUdpDg: " + wsaData.iMaxUdpDg"); } } </code></pre> <p>.</p> <pre><code>public class TestWSAEnumProtocolsA { public void start(){ WSAPROTOCOL_INFOW[] wsaprotocol_info = new WSAPROTOCOL_INFOW[1]; WSADATA wsaData = new WSADATA(); IntByReference dwBufferLength = new IntByReference(628); // sizeof WSAPROTOCOL_INFOW, in bytes int result = 0; short version = 2; if((result = Ws2_32.INSTANCE.WSAStartup(version, wsaData)) != 0){ System.out.println("Error #" + result + " at WSAStartup()"); return; } System.out.println("WSAStartup() finished correctly."); if((result = Ws2_32.INSTANCE.WSAEnumProtocolsW(null, wsaprotocol_info, dwBufferLength)) == -1){ System.out.println("WSAEnumProtocolsW() returned: " + result); System.out.println("dwBufferLength is: " + dwBufferLength.getValue()); System.out.println("WSAGetLastError() returned: " + Ws2_32.INSTANCE.WSAGetLastError()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload