Ofer Elboher

Dear sirs,

I have a managed C# app which uses an unmanaged C DLL.

One of the functions exported by this DLL lets its caller pass a pointer to a callback to be later on called by the DLL in order to report status updates. This function allows the caller to pass NULL for the function pointer in order to cancel a previously set callback, so that no more status update reports will be made.

Now, though I successfully call this function with a pointer to a new callback function, and the C# callback gets called with status update reports, I fail to call it with a NULL function pointer.

Can I do this, and, if so, then how exactly

Here're the relevant portions of my code:

1) The type definition for the callback in my C DLL:

typedef void (WINAPI *StatusUpdateCallbackPointer) (UpdateStruct* usp);

2) The declaration of the exported function im my C DLL:

void WINAPI SetStatusCallback (StatusUpdateCallbackPointer sucp);

3) The import of the exported function in my C# app:

[DllImport("MyDll.dll")]

public static extern void SetStatusCallback (StatusUpdateCallbackPointer sucp);

Thanks in advance,

Ofer.




Re: Visual C# General Passing NULL for Callback Function Parameter

Galin Iliev

I think SetStatusCallback (null) should work.

Have you test it

Another approach is to create delegate instance that point to your empty function and to set it like

void MyStatusFucn(){} //change signature according delegate

//and set it with

SetStatusCallback (new StatusUpdateCallbackPointer (MyStatusFucn))

Hope this helps






Re: Visual C# General Passing NULL for Callback Function Parameter

Mark Dawson

Hi,

what is the error you are getting when you pass in null Did you try IntPtr.Zero instead of null

Mark.






Re: Visual C# General Passing NULL for Callback Function Parameter

Ofer Elboher

SetStatusCallback (null) worked indeed. Don't know why of all solutions I tried I didn't think on this one...

The trick with void MyStatusFucn(){} doesn't work; the function is null, but it has a regular pointer.

Thanks a lot!

Sincerely,

Ofer.






Re: Visual C# General Passing NULL for Callback Function Parameter

Ofer Elboher

I got different compilation errors, as I tried different options.

As said above, simply passing null solved the problem, but passing IntPtr.Zero didn't work as-is, because it requires conversion.

Thanks for helping,

Ofer.






Re: Visual C# General Passing NULL for Callback Function Parameter

timvw

I'd another signature so that you can pass in an IntPtr... And then use that function to call it with IntPtr.Zero...

[DllImport("MyDll.dll")]

public static extern void SetStatusCallback (IntPtr myIntPtr);