[英]How can I pass a Python Socket object to a DLL written in C?
Is there any way of converting the python socket object to a void pointer? 有什么方法可以将python套接字对象转换为void指针吗?
I am using ctypes and I have tried using cast and the conversion functions of python. 我正在使用ctypes,并且尝试使用cast和python的转换函数。
The C code that I am trying to convert in python is this: 我试图在python中转换的C代码是这样的:
long UNILIB_CALL UL_cbOpen ( void **psocket, char * ipAddress,
unsigned int port, int connectionTimeout, int sendTimeout, int recvTimeout)
{
printf("%s %d ",ipAddress,port);
long ssendTimeout=sendTimeout *100;
long rrecvTimeout=recvTimeout *100;
printf("Address:%u,%u,%u", psocket,*psocket,&psocket);
(*psocket) = (void*)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("Address after:%u,%u,%u", psocket,*psocket,&psocket);
getchar();
RETURN_IF_ERRROR ((SOCKET)(*psocket) == INVALID_SOCKET,"SOCKET", 0);
long iResult=0;
//char RecvTimeout[21]; sprintf_s (RecvTimeout, sizeof RecvTimeout, "%d", recvTimeout);
iResult = setsockopt((SOCKET)(*psocket),SOL_SOCKET,SO_RCVTIMEO, (char*)&rrecvTimeout,
rrecvTimeout) ;
RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"setsockopt", WSAGetLastError());
char SendTimeout[21];
sprintf_s (SendTimeout, sizeof SendTimeout, "%d", sendTimeout);
iResult = setsockopt((SOCKET)(*psocket),SOL_SOCKET,SO_SNDTIMEO, (char*)&ssendTimeout,
sizeof ssendTimeout) ;
RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"setsockopt", WSAGetLastError());
SOCKADDR_IN clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( ipAddress );
clientService.sin_port = htons( port );
iResult = connect( (SOCKET)(*psocket), (SOCKADDR*) &clientService,
sizeof(clientService) );
RETURN_IF_ERRROR ( iResult == SOCKET_ERROR,"connect", WSAGetLastError());
return 1;
}
The Equivalent Python Code is -> 等效的Python代码是->
def UL_cbOpen(psocket,ipAddress,port,connectionTimeout,sendTimeout,recvTimeout):
print "Open CallBack\n";
print "PORT: {0}".format(port);
#print "IPAddress:{0}".format(ipAddress);
#print "Port:{0}".format(port);
ssendTimeout=c_long();
rrecvTimeout=c_long();
ssendTimeout=sendTimeout*100;
rrecvTimeout=recvTimeout*100;
try:
psocket=socks.socksocket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
print "Socket created";
except:
print "Failed to create socket";
psocket.setproxy(socks.PROXY_TYPE_SOCKS5,"my.web-proxy.example.com")
#PROXY_TYPE_SOCKS4,PROXY_TYPE_HTTP
hostname=gethostbyaddr(ipAddress);
psocket.setsockopt(SOL_SOCKET, SO_RCVTIMEO, rrecvTimeout);
psocket.setsockopt(SOL_SOCKET,SO_SNDTIMEO,ssendTimeout);
try:
psocket.connect((ipAddress,int(port)));
print "Connected to Printer";
except:
print "Failed Connection";
return 1;
This is a call back function and I have used: 这是一个回调函数,我使用了:
ulopen=WINFUNCTYPE(c_long,POINTER(c_void_p),c_char_p,c_uint,c_int,c_int,c_int);
to create the call back. 创建回叫。
UNILIB_CALL
is __stdcall
and I have used WinDLL to get library handle. UNILIB_CALL
是__stdcall
,我已经使用WinDLL来获取库句柄。
When the callback occurs I get an error: 当回调发生时,我得到一个错误:
Socket Pointer must have been allocated.
套接字指针必须已分配。
Is there any way of dereferencing pointer to a pointer? 有什么办法可以取消对指针的引用?
Can you suggest some changes in the code so that sockets can be handled as they have been in the C code? 您可以建议对代码进行一些更改,以便套接字可以像在C代码中一样进行处理吗?
Sockets have the fileno()
function. 套接字具有
fileno()
函数。
Maybe you can use it and create a new socket out of this file descriptor. 也许您可以使用它并根据此文件描述符创建一个新的套接字。
Converting the Python object to a void pointer will not help. 将Python对象转换为void指针将无济于事。 The C function requires a pointer to a C socket object, and the Python socket object is not the same as a C socket object.
C函数需要一个指向C套接字对象的指针,而Python套接字对象与C套接字对象不同。
As far as I know, you cannot use Python in this way to get a socket back for a C program to use. 据我所知,您不能以这种方式使用Python来获取供C程序使用的套接字。 Why are you trying to do this?
你为什么要这样做?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.