Runtime check error # 0 vb.net callback from C dll

I am writing an Add-inn Application A in VB.Net and DLL B in C. Application. The transfer callback method for dll B. When a certain event occurs, the dll calls a callback from A. Everything works fine on my PC, but when I go to my laptop, I get the error:

Runtime Check Failure # 0 - The ESP value was not stored properly during the function call. This is usually the result of a call to a function declared with one call, with a function pointer declared with a different calling convention.

This is part of the C code:

typedef void (__cdecl * OFFICE_PTR)();
void  TAPIClient::tapiCallBack(
DWORD hDevice,
DWORD dwMessage,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2,
DWORD dwParam3){
switch (dwMessage)
{
    case LINE_CALLSTATE:
        switch (dwParam1)
        {
            case LINECALLSTATE_OFFERING:
                                    if(dwInstance!=NULL)
                {
                    try
                    {   
                        OFFICE_PTR vbFunc =(OFFICE_PTR)dwInstance;
                        vbFunc( );//Critical moment
                    }
                    catch(...)
                    {
                        MessageBox (NULL, L"( (OFFICE_PTR)dwInstance )(&sCallNr)",L"ERROR",MB_OK);
                    }
                }
            break;
        };
    break;
}

      

}

Where dwInstance is the address of the application. Callback method

This is part of the VB.Net code:

Public Class TapiPlugin

Public Delegate Sub P_Fun()

Private Declare Function startSpy _
    Lib "TAPIClient.dll" _
    (ByVal pFun As P_Fun) As IntPtr

Public Shared Sub simpleTest()
    MsgBox("Plugin sub simpleTest")
End Sub

Public Sub onStart()
    Dim pBSTR As IntPtr
    pBSTR = startSpy(AddressOf simpleTest)
    MsgBox(Marshal.PtrToStringAuto(pBSTR))
    Marshal.FreeBSTR(pBSTR)
End Sub

End Class

      

An error occurred while trying to call 'vbFunc ()'. I would appreciate any help.: D

+1


source to share


3 answers


If calling convention cdecl

, you need to declare your delegate like this:

<UnmanagedFunctionPointer(CallingConvention.Cdecl)>
Public Delegate Sub P_Fun()

      



You can only do this in .NET 2.0 and after, as this attribute was not introduced before (and the interop level was not changed to validate it before).

If the calling convention is valid stdcall

, then the delegate can remain as is. You said it was stdcall

, but I have my doubts as the exception is clearly telling you that there might be a mismatch in the calls.

+1


source


Is it possible for two computers to have different pointer sizes? Maybe this is a 64-bit machine and the other is only 32?

typedef void (__cdecl * OFFICE_PTR)();
void  TAPIClient::tapiCallBack(
DWORD hDevice,
DWORD dwMessage,
DWORD dwInstance,
...){
...
        OFFICE_PTR vbFunc =(OFFICE_PTR)dwInstance;
        vbFunc( );//Critical moment

      



The DWORD type is not valid for passing pointer types. You should be using INT_PTR I think.

0


source


I believe that this is not a reason to check it out. I passed the callback as a global pointer of type OFFICE_PTR and got the same result. On PC it works fine on laptop, it crashes :(

A must apologize for the mistake I wrote that def looks like this:

typedef void (__cdecl * OFFICE_PTR)();

      

but for real it looks like

typedef void (__stdcall * OFFICE_PTR)();

      

0


source







All Articles