Q: C# exception when calling callback function I've made this to call unmanaged function from C code. pCallback is a function pointer so on the managed side is a delegate.
[DllImport("MyDLL.dll")]

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

Now I am setting
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's wrong. Can anyone help me?

A: Try calling Marshal.GetLastWin32Error() to get the Win32 error code.  
Then compare the error code against this list: http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx
It's still not as much info as a good exception object, but it might point you in the right direction.

A: There is not a lot of detail here, but my guess is this may be a calling convention issue.  I always try to explicitly set the calling convention when using DllImport;
[DllImport("msvcrt.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, double d); 

The calling convention can affect how parameters are put on the call stack and how they are cleaned up afterward.
See here
You'd have to find the correct convention from the headers or docs provided with your unmanaged library.

A: You have to make sure that your reference to the callback is not collected by the Garbage Collector. The reference to the callback has to be alive in managed memory for as long as the callback is expected to be call. 
One way to go around this is to create a Managed C++ layer in the middle

A: Try to use 
[UnmanagedFunctionPointer(CallingConvention.xxx, CharSet = CharSet.xxx)]
public delegate ...

