Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing a delegate to a DLL as a 'void *'

1 view
Skip to first unread message

Andrue Cope [TeamB]

unread,
Sep 18, 2006, 5:43:38 AM9/18/06
to
I have a DLL written in C++ and I need to pass it a delegate so that it
can call me back. The DLL has been around for a while and it takes
commands using a structure to contain input and output data. Callbacks
are passed to it in 'void *' pointers members.

I can't work out how to do the final assignment in C#.

If I try to just code the assignment:

fsCmd.Value_VOIDS_2=TheProgressCB;

I get "[C# Error] SDL_FileSystem.cs(116): Cannot implicitly convert
type 'SDL.TCBProgress' to 'void*'" which doesn't really surprise me.

The declarations are:

unsafe public delegate int TCBProgress( uint MaxValue,
uint NowValue,
sbyte * Message );

and fsCmd.Value_VOIDS_2 is 'void *'.

The only solution I've come up with so far seems a little silly and
that's to add a function to our DLL that takes a delegate and a void *
by reference then performs the assignment and returns. Surely there's a
way of performing that assignment using C#?
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Marc Rohloff [TeamB]

unread,
Sep 18, 2006, 5:50:29 PM9/18/06
to
On 18 Sep 2006 02:43:38 -0700, Andrue Cope [TeamB] wrote:

> I have a DLL written in C++ and I need to pass it a delegate so that it
> can call me back. The DLL has been around for a while and it takes
> commands using a structure to contain input and output data. Callbacks
> are passed to it in 'void *' pointers members.

Why don't you the type of fsCmd.Value_VOIDS_2 to be a TCBProgress
delegate instead?

A common trick is to have several different structures matching the
expected types of the fields and then overload the DLL call for each
possible type.

Another possibility which /might/ work is to either use type
information to get the _methodPtr and _methodPtrAux fields or just use
the HashCode (which currently is the same thing). Of course this is
not guaranteed to work the same in different versions of the
framework.

--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com

Andrue Cope [TeamB]

unread,
Sep 19, 2006, 3:57:19 AM9/19/06
to
Marc Rohloff [TeamB] wrote:

> Why don't you the type of fsCmd.Value_VOIDS_2 to be a TCBProgress
> delegate instead?
>

I could do but traditionally we have one structure per module (which is
why the member name is so generic).

> A common trick is to have several different structures matching the
> expected types of the fields and then overload the DLL call for each
> possible type.

Yeah, makes sense.

> Another possibility which might work is to either use type


> information to get the _methodPtr and _methodPtrAux fields or just use
> the HashCode (which currently is the same thing). Of course this is
> not guaranteed to work the same in different versions of the
> framework.

Best not to do that.

0 new messages