Crash immediately after calling a function of dll successfully

I can make sure this dll is accurate.
Why does the program crash immediately after successfully calling the function APlayer_Create and prompts: The process has ended and the exit code is -1073740771 (0xC000041D)

        aplayerCaller = ctypes.CDLL(application_path + '\APlayerCaller.dll')

        global CALLBACK,Callback_cfun,Callback_address,aaa
        aaa = self.OnMessage_Callback
        CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_int,ctypes.c_int,ctypes.c_int)
        Callback_cfun =CALLBACK(aaa)
        Callback_address = ctypes.cast(Callback_cfun,ctypes.c_void_p).value
        #aa = ctypes.cast(OnMessage_Callback_cfun, ctypes.c_void_p).value
        print(CALLBACK,Callback_cfun,Callback_address)
        aplayer = aplayerCaller.APlayer_Create(winID, 0, 0, self.width(), self.height(),
                                                   Callback_address, 0, 0, 0, 0, 0, 0, 0)

    def OnMessage_Callback(self,a2, a3,a4):
        qs = QMessageBox.information(self,'zzz','test',)
        print(f'a1={self},a2={a2},a3={a3},a4={a4}')

a2, a3, a4, I can get the data normally and print it out once, but then it crashes immediately and prompts an error message (-1073740771 (0xC000041D)) in pycharm. If it is normal, it should keep outputting a2, a3, a4 until my mouse stop moving

Beware that '\APlayerCaller.dll' is not valid in python 3.12 you need to double the \ to be \\.
Does the code wrk outside of PyCharm?

You can use the Win32 FormatError function to turn the code into text.
See FormatMessage function (winbase.h) - Win32 apps | Microsoft Learn
You often can get help on windows errors code with a web search like windows error I found that 0xC000041d.
I did that and it seems that code is I found that 0xC000041d = STATUS_FATAL_USER_CALLBACK_EXCEPTION

I can confirm that the dll can run normally in my code. As long as I change the callback function ‘Callback_address’ to 0, the code can run normally. Once changed to ‘Callback_address’, after calling ‘OnMessage_Callback’ and printing, the program It exited abnormally. This OnMessage_Callback should have been called multiple times in succession, but it was only called once and crashed immediately.

You will need to figure out what is wrong with your callback function.

Changing CDLL to WinDLL solved my problem!

thank you very much!

1 Like