Segfault on calling standard windows .dll from python ctypes with wine
Question: I'm trying to call some function from Kernel32.dll in my Python script running
on Linux. As Johannes Weiß pointed [How to call Wine dll from python on
Linux?](http://stackoverflow.com/questions/4052690/how-to-call-wine-dll-from-
python-on-linux/4053954#4053954) I'm loading **kernel32.dll.so** library via
**ctypes.cdll.LoadLibrary()** and it loads fine. I can see kernel32 loaded and
even has **GetLastError()** function inside. However whenever I'm trying to
call the function i'm gettings segfault.

    
    
    import ctypes
    
    kernel32 = ctypes.cdll.LoadLibrary('/usr/lib/i386-linux-gnu/wine/kernel32.dll.so')
    
    print kernel32
    # <CDLL '/usr/lib/i386-linux-gnu/wine/kernel32.dll.so', handle 8843c10 at b7412e8c>
    
    print kernel32.GetLastError
    # <_FuncPtr object at 0xb740b094>
    
    gle = kernel32.GetLastError
    # OK
    
    gle_result = gle()
    # fails with
    # Segmentation fault (core dumped)
    
    print gle_result
    

First I was thinking about calling convention differences but it seems to be
okay after all. I'm ending with testing simple function GetLastError function
without any params but I'm still getting Segmentation fault anyway.

My testing system is Ubuntu 12.10, Python 2.7.3 and wine-1.4.1 (everything is
32bit)

**UPD**

I proceed with my testing and find several functions that I can call via
ctypes without segfault. For instance I can name Beep() and GetCurrentThread()
functions, many other functions still give me segfault. I created a small C
application to test kernel32.dll.so library without python but i've got
essentially the same results.

    
    
    int main(int argc, char **argv) 
    {
    
       void *lib_handle;
    
       #define LOAD_LIBRARY_AS_DATAFILE            0x00000002
    
       long (*GetCurrentThread)(void);
       long (*beep)(long,long);
       void (*sleep)(long);   
       long (*LoadLibraryExA)(char*, long, long);
    
    
       long x;
       char *error;
    
       lib_handle = dlopen("/usr/local/lib/wine/kernel32.dll.so", RTLD_LAZY);
    
       if (!lib_handle) 
       {
          fprintf(stderr, "%s\n", dlerror());
          exit(1);
       }
    
       // All the functions are loaded e.g. sleep != NULL
       GetCurrentThread = dlsym(lib_handle, "GetCurrentThread");
       beep = dlsym(lib_handle, "Beep");
       LoadLibraryExA = dlsym(lib_handle, "LoadLibraryExA");
       sleep = dlsym(lib_handle, "Sleep");
    
    
    
    
       if ((error = dlerror()) != NULL)  
       {
          fprintf(stderr, "%s\n", error);
          exit(1);
       }
    
       // Works
       x = (*GetCurrentThread)();   
       printf("Val x=%d\n",x);
    
       // Works (no beeping, but no segfault too)
       (*beep)(500,500);    
    
       // Segfault  
       (*sleep)(5000);      
    
       // Segfault  
       (*LoadLibraryExA)("/home/ubuntu/test.dll",0,LOAD_LIBRARY_AS_DATAFILE);
    
    
       printf("The End\n");
       dlclose(lib_handle);
       return 0;
    }
    

I was trying to use different calling conventions for Sleep() function but got
no luck with it too. When I comparing function declarations\implementation in
Wine sources they are essentially the same

Declarations

    
    
     HANDLE WINAPI GetCurrentThread(void) // http://source.winehq.org/source/dlls/kernel32/thread.c#L573
     BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur ) // http://source.winehq.org/source/dlls/kernel32/console.c#L354
     HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags) // http://source.winehq.org/source/dlls/kernel32/module.c#L928
     VOID WINAPI DECLSPEC_HOTPATCH Sleep( DWORD timeout ) // http://source.winehq.org/source/dlls/kernel32/sync.c#L95
    
     WINAPI is defined to be __stdcall 
    

However some of them works and some don't. As I can understand this sources
are for kernel32.dll file and kernel32.dll.so file is a some kind of proxy
that supposed to provide access to kernel32.dll for linux code. Probably I
need to find exact sources of kernel32.dll.so file and take a look on
declarations.

Is there any tool I can use to take a look inside .so file and find out what
functions and what calling conventions are used?


Answer: The simplest way to examine a DLL is to use the `nm` command, i.e.

    
    
    $ nm kernel32.dll.so | grep GetLastError
    7b86aae0 T _GetLastError
    

As others have pointed out, the default calling convention for Windows C DLLs
is `stdcall`. It has nothing to do with using Python. On the Windows platform,
`ctypes.windll` is available.

However, **I am not even sure what you are trying to do is at all possible**.
Wine is a full-blown Windows emulator and it is safe to guess that at least
you would have to start it with `wine_init` before loading any other
functions. The Windows API probably have some state (set when Windows boots).

The easiest way to continue is probably to install a Windows version of Python
under Wine and run your script from there.

