Python: accessing DLL function using ctypes — access by function *name* fails

前端 未结 3 785
不思量自难忘°
不思量自难忘° 2021-01-12 04:11

myPythonClient (below) wants to invoke a ringBell function (loaded from a DLL using ctypes). However, attempting to access ring

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 04:45

    All is working now :) To summarize your posts:

    Write DLL in C++:

    // Header
    extern "C"
    {   // Name in DLL will be "MyAdd" - but you won't be able to find parameters etc...
        __declspec(dllexport) int MyAdd(int a, int b);
    }  
    // Name will be with lot of prefixes but some other info is provided - IMHO better approach
    __declspec(dllexport) int MyAdd2(int a, int b);
    
    //.cpp Code
    __declspec(dllexport) int MyAdd(int a, int b)
    {   return a+b;
    }
    __declspec(dllexport) int MyAdd2(int a, int b)
    {   return a+b;
    } 
    

    Then you can use program link.exe to see real function name in dll. link.exe is for example in MSVC2010 here:

    c:\program files\microsoft visual studio 10.0\VC\bin\link.exe
    

    use:

    link /dump /exports yourFileName.dll
    

    you see Something like:

    ordinal hint RVA      name
          1    0 00001040 ?MyAdd2@@YAHHH@Z = ?MyAdd2@@YAHHH@Z (int __cdecl MyAdd2(int,int))
          2    1 00001030 MyAdd = _MyAdd
    

    Then in python you can import it as:

    import ctypes
    
    mc = ctypes.CDLL('C:\\testDll3.dll')
    
    #mc.MyAdd2(1,2) # this Won't Work - name is different in dll
    myAdd2 = getattr(mc,"?MyAdd2@@YAHHH@Z") #to find name use: link.exe /dump /exports fileName.dll 
    print myAdd2(1,2)
    #p1 = ctypes.c_int (1) #use rather c types
    print mc[1](2,3) # use indexing - can be provided using link.exe
    
    print mc.MyAdd(4,5)
    print mc[2](6,7) # use indexing - can be provided using link.exe
    

提交回复
热议问题