6

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

4
  • I'm not familiar with Wine, but I believe this issue has something to do with the calling conventions, en.wikipedia.org/wiki/X86_calling_conventions, for these functions. Most kernel32.dll uses stdcall. You can try to call some functions from msvcrt.dll or ws2_32.dll, most its functions use cdecl, you will not get segfault. Look here, codepad.org/DzX33DYz.
    – user1129665
    Jan 23, 2013 at 2:49
  • @Sp. Thanks, I've found some functions I can call from kernel32.dll.so my self. However I can't find any difference between them and the functions I need in my code. Could you please suggest me any tool to find out calling conventions for function inside .so file? Jan 23, 2013 at 7:03
  • I use IDA pro, it's very useful for this job.
    – user1129665
    Jan 23, 2013 at 9:07
  • You might try running python in wine and see if it works better for what you're doing, winetricks makes installing it simple. Also, if it needs a complex data type passed in as the argument, you have to tell ctypes what to do, in the ctypes documentation, there is information for setting the argument type when calling functions, you may need to do that.
    – Perkins
    May 23, 2013 at 4:40

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.