First of all This works perfectly in x32 python, but fails in x64 python with the following error

OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF

I got a hello.dll written in C++ which exports the following function

int __stdcall GetMessage(char **pMsgContainer)
{
    if (!pMsgContainer) return -1;
    if (*pMsgContainer) return -2;
    *pMsgContainer = NULL;
    
    char* msg = "Hello world!";
    
    size_t nLen = strlen(msg);
    *pMsgContainer = new char[nLen + 1];
    strncpy((*pMsgContainer), msg, nLen);
    (*pMsgContainer)[nLen] = 0;
    
    return 0;
}

And I call it from Python 3.9 using the following code

import ctypes

dll = ctypes.WinDLL(r"D:\path-to\hello.dll")
GetMessage = dll.GetMessage

GetMessage.argtypes = [ctypes.POINTER(ctypes.c_char_p)]
GetMessage.restype = ctypes.c_int

pBuf = ctypes.c_char_p()
res = GetMessage(ctypes.byref(pBuf))

if (res == 0) :
    print ("Message:", pBuf.value)
else :
    print ("Error:", res)

I have compiled two versions of the dll (32 and 64 bit), and tested both of them using a test program in c++, and can confirm both of them work as expected.

Below is C++ code to run it, and I confirm it works perfectly for both 32 and 64 bit dll versions

char *pBuf = NULL;
int res = GetMessage(&pBuf);

As I stated earlier, this works perfectly with 32 bit python, but fails to work in 64 bit python

I've spent two days on this matter and already checked all the related questions on stackoverflow

Thanks everyone in advance!

0

There are 0 answers