开发者

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?

I am using ctypes and I have tried using cast and the conversion functions of python.

The C code that I am trying to convert in python is this:

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

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.

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?


Converting the Python object to a void pointer will not help. 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.

As far as I know, you cannot use Python in this way to get a socket back for a C program to use. Why are you trying to do this?


Sockets have the fileno() function.

Maybe you can use it and create a new socket out of this file descriptor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜