ctypes Errors with argv

Viewed 549

I'm trying to call in my C ++ library from python with types but I can't pass as IP arguments and HEX code, can anyone help me?

import sys 
import ctypes 

lib = ctypes.CDLL('./hello.so') 

LP_c_char = ctypes.POINTER(ctypes.c_char) 
LP_LP_c_char = ctypes.POINTER(LP_c_char) 

lib.connect_pe_func.argtypes = (ctypes.c_int, LP_LP_c_char)  

argc = 2
argv = ["192.168.2.170","2600000026"]
for i, arg in enumerate(sys.argv): 
  enc_arg = arg.encode('utf-8') 
  argv[i] = ctypes.create_string_buffer(enc_arg) 

lib.connect_pe_func(argc, argv)

this is the error message, how do I insert IP and Hex code into the argv vector without having this error?

---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-21-f59eabe02690> in <module>
     17   argv[i] = ctypes.create_string_buffer(enc_arg)
     18
---> 19 lib.connect_pe_func(argc, argv)

ArgumentError: argument 2: <class 'TypeError'>: expected LP_LP_c_char instance instead of list

for completeness I also insert the C ++ code that I compiled and made into a hello.so library

#include <stdio.h>
#include <errno.h>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include "connect_PE_func.h"
using namespace std;


extern "C" char * connect_pe_func(int argc, char *argv[])
{
    int sockfd, n;
    int connected = 0;
    struct sockaddr_in servaddr;
    std::string serveraddr = argv[1];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(serveraddr.c_str());
    servaddr.sin_port = htons(9761);

    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    std::string pref_hex;
    std::string hex("0x");
    std::string test = argv[2];
    size_t numbytes = test.size() / 2;

    uint8_t command[numbytes];

    for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
    {
        pref_hex = hex + test.substr(x, 2);
        cout << pref_hex;
        command[w] = stoi(pref_hex, nullptr, 16);
    }

    int bytes_to_send = sizeof(command);

    send(sockfd, command, bytes_to_send, 0);
    uint8_t output_command[numbytes];
    recv(sockfd, output_command, bytes_to_send, 0);

    char test_out[10];

    for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
    {
        test_out[x] = (char)output_command[w];
    }
    return test_out;
};

extern "C" char * hello_world(char * name){
    char * output = (char *) calloc(sizeof(name)+7, sizeof(char));
    strcat(output, "Hello ");
    strcat(output, name);
    strcat(output, "\0");
    //output[sizeof(output)-1] = '/0';
    return output;

};
1 Answers

Python lists can't be passed as pointer of pointers.

To create and populate a pointer of pointers from Python, you need:

  • to create a pointer array: p = (LP_c_char*len(argv))() (allocates the space for the pointers)
  • to cast it into a pointer of pointers: na = ctypes.cast(p, LP_LP_c_char) (makes it compatible with the decayed pointer of pointers form)

or without the redefinitions:

p = (ctypes.POINTER(ctypes.c_char)*len(argv))()
na = ctypes.cast(p, ctypes.POINTER(ctypes.POINTER(ctypes.c_char)))

Fixed code:

import sys
import ctypes

lib = ctypes.CDLL('./hello.so')

LP_c_char = ctypes.POINTER(ctypes.c_char)
LP_LP_c_char = ctypes.POINTER(LP_c_char)

lib.connect_pe_func.argtypes = (ctypes.c_int, LP_LP_c_char)

argv = ["192.168.2.170","2600000026"]
argc = len(argv)

p = (LP_c_char*len(argv))()
for i, arg in enumerate(argv):  # not sys.argv, but argv!!!
  enc_arg = arg.encode('utf-8')
  p[i] = ctypes.create_string_buffer(enc_arg)

na = ctypes.cast(p, LP_LP_c_char)

lib.connect_pe_func(argc, na)

To test this, I've created a very simple c++ code (instead of yours)

#include <stdio.h>

extern "C" char * connect_pe_func(int argc, char *argv[])
{
   for (int i=0;i<argc;i++)
   {
      puts(argv[i]);
   }
   return 0;
}

built with: g++ -shared -o hello.so test.cpp

running the python module proves that arguments are passed all right:

192.168.2.170
2600000026

Inspired by Pointers and arrays in Python ctypes

Related