(也许我用烦人的细节来解释这个问题,tl; dr版本在底部)
我一直在尝试使用JNA在Java中创建一个简单的Winsock测试应用程序。我调用的第一个函数是WSAStartup(),使用以下代码:
public interface Ws2_32 extends Library {
Ws2_32 INSTANCE = (Ws2_32) Native.loadLibrary("ws2_32", Ws2_32.class);
int WSAStartup(short version, LPWSADATA lpwsaData);
}
public static void main(String[] args){
LPWSADATA lpwsaData = new LPWSADATA();
short version = 2;
int result = Ws2_32.INSTANCE.WSAStartup(version,lpwsaData);
System.out.println("WSAStartup() returned: " + result);
if((resultado = Ws2_32.INSTANCE.WSAStartup(version,lpwsaData)) == 0){
System.out.println("LPWSADATA struct:");
System.out.println("wVersion: " + lpwsaData.wVersion);
System.out.println("wHighVersion: " + lpwsaData.wHighVersion);
System.out.print("szDescription: ");
for(byte b : lpwsaData.szDescription){
System.out.print((char) b);
}
System.out.print("\n");
System.out.print("szSystemStatus: ");
for(byte b : lpwsaData.szSystemStatus){
System.out.print((char) b);
}
System.out.print("\n");
System.out.println("iMaxSockets: " + lpwsaData.iMaxSockets);
System.out.println("iMaxUdpDg: " + lpwsaData.iMaxUdpDg");
}
}
这样可行,我得到以下值:
wVersion:2
wHighVersion:514
szDescription:WinSock 2.0
szSystemStatus:正在运行
iMaxSockets:0
iMaxUdpDg:0
WSAStartup()成功返回后,我尝试调用WSAEnumProtocols()但是我收到以下错误:
线程“main”中的异常java.lang.UnsatisfiedLinkError:查找函数'WSAEnumProtocols'时出错:找不到指定的过程。
然后我使用Dependency Walker打开了ws2_32.dll,发现没有该名称的函数。我只找到了3个名字相似的名字:WSAEnumProtocolsA(),WSAEnumProtocolsW()和WSCEnumProtocols()。 我将使用WSAEnumProtocolsA()作为示例,但我使用了下一个过程,并使用了所有3个并获得了相同的结果:
首先我调用了WSAStartup()并且没有返回任何错误。 根据WSAEnumProtocols的MSDN定义,对函数的第一次调用应该是这样的: WSAEnumProtocols(null,wsaprotocol_info,lpdwBufferLength) 第一个参数为null,第二个参数是指向WSAPROTOCOL_INFO结构的指针,第三个参数是缓冲区的长度。如果该长度为零,则该函数应返回-1(SOCKET_ERROR)并且对WSAGetLastError()的调用应返回WSAENOBUFS,这意味着缓冲区不足以包含WSAEnumProtocols()返回的信息,并应设置变量lpdwBufferLength具有可传递给WSAEnumProtocols的最小缓冲区大小以检索所有请求的信息。 我不能让这个工作。 WSAEnumProtocols()返回-1,但lpdwBufferLength的值未更改,WSAGetLastError()返回0而不是10055(WSAENOBUFS)
我还尝试使用WSASetLastError()并将其设置为其他一些错误代码然后调用WSAGetLastError(),但它总是返回0.。
TL;博士 我无法使用WSAEnumProtocols(null,wsaprotocol_info,lpdwBufferLength)。 WSAEnumProtocols()返回-1,但lpdwBufferLength的值未更改,WSAGetLastError()返回0而不是10055(WSAENOBUFS)
更新:
这是我用来声明Winsock函数的接口
public interface Ws2_32 extends Library {
Ws2_32 INSTANCE = (Ws2_32) Native.loadLibrary("ws2_32", Ws2_32.class);
int WSAStartup(short version, LPWSADATA lpwsaData);
int WSAEnumProtocolsW(int[] lpiProtocols, WSAPROTOCOL_INFO lpProtocolBuffer, int lpdwBufferLength);
int WSACleanup();
int WSAGetLastError();
int WSASetLastError(int iError);
}
这是我调用函数的代码:
public class TestWSAEnumProtocolsA {
public void start(){
WSAPROTOCOL_INFO wsaprotocol_info = new WSAPROTOCOL_INFO();
LPWSADATA lpwsaData = new LPWSADATA();
int lpdwBufferLength = -2;
int result = 0;
short version = 514;
if((result = Ws2_32.INSTANCE.WSAStartup(version, lpwsaData)) != 0){
System.out.println("Error #" + result + " at WSAStartup()");
return;
} else {
System.out.println("WSAStartup() finished correctly.");
if((result = Ws2_32.INSTANCE.WSAEnumProtocols(null, wsaprotocol_info, lpdwBufferLength)) == -1){
System.out.println("WSAEnumProtocolsW() returned: " + result);
System.out.println("lpdwBufferLength is: " + lpdwBufferLength);
System.out.println("WSAGetLastError() returned: " + Ws2_32.INSTANCE.WSAGetLastError());
System.out.println("Now I'm setting it to 10004");
Ws2_32.INSTANCE.WSASetLastError(10004);
System.out.println("WSAGetLastError() returned: " + Ws2_32.INSTANCE.WSAGetLastError());
}
}
}
}
该代码产生以下输出:
WSAStartup()正确完成。
WSAEnumProtocolsW()返回:-1
lpdwBufferLength是:-2
WSAGetLastError()返回:0
现在我将其设置为10004
WSAGetLastError()返回:0
这就是我在调用这些函数时定义的结构:
public class WinSock2_structs {
public static class LPWSADATA extends Structure{
public short wVersion;
public short wHighVersion;
public byte szDescription[] = new byte[256+1];
public byte szSystemStatus[] = new byte[128+1];
public short iMaxSockets;
public short iMaxUdpDg;
public char lpVendorInfo;
}
public static class WSAPROTOCOLCHAIN extends Structure{
public int ChainLen;
public int ChainEntries[] = new int[7];
}
public static class GUID extends Structure{
public int Data1;
public short Data2;
public short Data3;
public short Data4;
public byte Data5[] = new byte[8];
}
public static class WSAPROTOCOL_INFO extends Structure{
public int dwServiceFlags1;
public int dwServiceFlags2;
public int dwServiceFlags3;
public int dwServiceFlags4;
public int dwProviderFlags;
public GUID ProviderId;
public int dwCatalogEntryId;
public WSAPROTOCOLCHAIN ProtocolChain;
public int iVersion;
public int iAddressFamily;
public int iMaxSockAddr;
public int iMinSockAddr;
public int iSocketType;
public int iProtocol;
public int iProtocolMaxOffset;
public int iNetworkByteOrder;
public int iSecurityScheme;
public int dwMessageSize;
public int dwProviderReserved;
public char szProtocol[] = new char[256];
}
}
答案 0 :(得分:1)
您正在调用WSAStartup()
两次,这需要您两次调用WSACleanup()
才能正确卸载WinSock。你应该只调用WSAStartup()
一次。
szProtocol
结构的WSAPROTOCOL_INFO
成员是TCHAR
个元素的数组。 TCHAR
映射到char
或wchar_t
,具体取决于调用的应用是针对ANSI / MBCS还是UNICODE编译的。这就是ws2_32.dll中没有WSAEnumProtocols()
函数的原因。而是有单独的WSAEnumProtocolsA()
(对于Ansi)和WSAEnumProtocolsW()
(对于Unicode)函数。由于Java使用Unicode字符串,因此您应该使用JNA代码WSAEnumProtocolsW()
。 WSAStartup()
不使用TCHAR
,仅使用char
,这就是为什么没有单独的WSAStartupA()
和WSAStartupW()
函数的原因。
如果您的JNA代码无法使WSAEnumProtocols()
和WSAGetLastError()
正常工作,则可能是您声明/使用它们错误,但您没有显示任何代码,所以没有人可以肯定地说为什么它不适合你。
更新:尝试这样的事情(我不使用JNA,所以这可能需要一些调整,但这会给你一般的想法):
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);
}
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");
}
}
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());
}
}
}