all groups > dotnet interop > june 2004 >
You're in the

dotnet interop

group:

delegate for C library callback function with retval param


delegate for C library callback function with retval param Rua Haszard Morris
6/30/2004 4:02:01 PM
dotnet interop: I'm trying to use a callback with the following signature from C# code.

DWORD CALLBACK YourStreamProc(
HSTREAM handle,
void *buffer,
DWORD length,
DWORD user
);

This callback is invoked from a library (in C; the library is BASS www.un4seen.com). The buffer parameter points to an array, allocated by the lib, which my callback fills with data.

I'm using the lib wrapped by a (third-party) VB wrapper lib.

It declares a delegate for the callback:
Public Delegate Function STREAMPROC_Handler(ByVal handle As Integer, ByVal buffer As Integer, ByVal length As Integer, ByVal user As Integer) As Integer

This doesn't seem right
- it's not possible to cast the buffer int to a pointer /array
- and the buffer won' t be returned to the caller unless it declared "ByRef"

Am I correct on these points?

I've tried declaring the parameter as ByRef, and various types (object, array, System.IntPtr), but I never recieve a buffer that I can write to. It's always 0 or <undefined value>.

Is it possible to use this callback from managed code?

How do I declare the delegate for the callback (in VB, C#)?

thanks for any help...
Rua HM

RE: delegate for C library callback function with retval param Rua Haszard Morris
7/6/2004 5:06:01 PM
once again after a week or 2 to think & read docs I answer my own question.

1. The delegate should be declared as:
Public Delegate Function STREAMPROC_Handler(ByVal handle As Integer, ByVal buffer As System.IntPtr, ByVal length As Integer, ByVal user As Integer) As Integer
that is, the pointer is a ByVal System.IntPtr.

2. In the callback, this is coerced (manhandled) eventually to a short pointer (in my case anyway) & now allows access to the mem:
void* bugger = buffer.ToPointer();
short* bugger2 = (short*)(bugger);
for (int i=0; i<len; i++)
bugger2[i] = (short) ran.Next();


sweet as,
Thanks all.

[quoted text, click to view]
AddThis Social Bookmark Button