C # exception when calling callback function

I did this to call an unmanaged function from C. pCallback is a pointer to a function, so the managed party is a delegate.

[DllImport("MyDLL.dll")]

public static extern Result SetCallback(
            IntPtr handle,
            Delegate pCallback,
            CallbackType Type);

Now i install

private delegate void pfnCallback(uint PromptID, ttsEventType evt, IntPtr lData);
private pfnCallback cb = new pfnCallback(cback);

public Form1()
{

    (...)
    Wrapper.SetCallback(handle, cb, IntPtr.Zero, CallBackType.DEFAULT);
    (...)
    public static void cback(uint PromptID, ttsEventType evt, IntPtr lData)
    { }
    }

When debugging, I see that it runs the cback function once, and then I get an exception with no data, just saying: "An unhandled win32 exception occurred in WindowsApp2.vshost.exe [4372]. I don’t understand what is False. Can anyone will you help me?

+3
source share
4 answers

Try using

[UnmanagedFunctionPointer(CallingConvention.xxx, CharSet = CharSet.xxx)]
public delegate ...
+1
source

Try calling Marshal.GetLastWin32Error () to get the Win32 error code.

: http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx

, , .

+2

, , . DllImport;

[DllImport("msvcrt.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, double d); 

, .

.

, .

+2

, . , , .

- ++-

+1

Source: https://habr.com/ru/post/1733219/


All Articles