Search code examples
pythoncswighandlevoid-pointers

SWIG/Python: Passing pointer into function


I basically want to do exactly this:

swig: how to pass void* into generic function

I seem to be able to get a value that Python can hold, but when I pass it back in, it is deemed null and causes a fail. So I'm thinking I need some kind of PyObject conversion. I don't know why void pointers should be so hard to handle.

(I'm actually working with HMODULE, but I think if I can get void pointers to work, I can get them to work as well.

Basically, I just want to:

>>hmod = example.load_module("fooModName")
>>example.run_module(hmod, par1, par2)

How do I define the input and output typemaps such that these two calls can be supported?

I am trying to work with HMODULE (defined in Windows.h , eventually as a void pointer)

HMODULE load_library(char *pathDLL);
int     get_api(HMODULE dll);   // loads/updates api

===========================================================

I have figured out my actual problem.

My routines when wrapped would be:

>>ModuleName = example.find_module_name("lookDir")
>>HModule = example.load_module(ModuleName)

The problem is that the returned name is a char string (char *) but the LoadLibrary() MSDN routine needs a LPCWSTR. I can readily convert the string within the routine, but that breaks it for other use, so I'd really like to convert the string within an input wrapper.

I think the solution is a typemap of some sort.

I am trying this:

%typemap(in) char* inpathDLL {
static wchar_t LpathDLL[2048];
MultiByteToWideChar(CP_ACP, 0, $input, -1, LpathDLL, 2048);
$1 = LpathDLL;
}

This is failing. It seems to thing the input is a python string object, so I somehow have to get it to convert it to a char * before applying the next conversion.

How do I get this input string converted?

ANSWER:

OK, I found a (the?) fix. The trick is to use 'check' which adds code after the default conversion:

%typemap(check)char* inpathDLL {
  if ($1 != NULL) {
    static wchar_t LpathDLL[2048];
    MultiByteToWideChar(CP_ACP, 0, $1, -1, LpathDLL, 2048);
    $1 = (char *)LpathDLL;
  }
}

Solution

  • Swig supports that usage directly, with no typemaps required.

    One way to get your code snippet to work is as follows:

    /* File : example.c */
    
    void *
    load_module(const char* name)
    {
        static int module_handle = 12;
        printf("Hoory, loaded %s\n", name);
        return &module_handle;
    }
    void
    example_run_module(void* handle, int par1, int par2)
    {
        printf("Yay! Running module %d: %d %d\n",
            *(int*)handle, par1, par2);
    }
    

     

    /* example.i */
    %module example
    %{
    void * load_module(const char* name);
    void example_run_module(void* handle, int par1, int par2);
    %}  
    
    void * load_module(const char* name);
    void example_run_module(void* handle, int par1, int par2);
    

     

    # SConscript
    Import('env')
    env.Replace(SWIGFLAGS=['-python'])
    env.ParseConfig('/usr/bin/python-config --cflags --ldflags')
    lib = env.SharedLibrary('_example.so', ['example.c', 'example.i'])
    env.Clean(lib, 'example.pyc')
    

    Result:

    In [5]: import example
    
    In [6]: example.load_module("hayhay")
    Hoory, loaded hayhay
    Out[6]: <Swig Object of type 'void *' at 0x7f18cca05180>
    
    In [7]: import example
    
    In [8]: x=example.load_module("hayhay")
    Hoory, loaded hayhay
    
    In [9]: example.example_run_module(x, 3, 4)
    Yay! Running module 12: 3 4