How to wrap the capSetCallbackXXXXXX function

I try the following code:


public delegate Int32 capErrorCallback(IntPtr hWnd, Int32 nID,
[MarshalAs(UnmanagedType.LPWStr)]
String lpsz);

public static Boolean capSetCallbackOnError(IntPtr hwnd, capErrorCallback errorCallback)
{
return AVICapSendMessage(hwnd, WM_CAP_SET_CALLBACK_ERRORW, 0,
(Int32)errorCallback.Method.MethodHandle.Value) == 0 false : true;
}

but the code looks doesn't work, the error cause a NullReferenceException. I am not sure which cause the problem. Could anybody give any advice



Answer this question

How to wrap the capSetCallbackXXXXXX function

  • Alan1203

    Maybe the usage of errorCallback.Method.MethodHandle.Value isn't correct or something else
  • Lee Meyers

    I try my code in VS 2005, the exception is more specific-it's AccessViolationException, but i don't know which cause the exception, managed code or unmanaged.:(
  • Kapil Singhal

    I solved this problem by using the new GetFunctionPointerForDelegate function in .NET 2.0.

    public static Boolean capSetCallbackOnError(IntPtr hwnd, capErrorCallback errorCallback)
    {
    IntPtr ad = Marshal.GetFunctionPointerForDelegate(errorCallback);

    return AVICapSendMessage(hwnd, WM_CAP_SET_CALLBACK_ERRORW, 0,
    (Int32)ad) == 0 false : true;
    }


  • How to wrap the capSetCallbackXXXXXX function