🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
3 messages
3 total messages Started by Andrue Cope [TeamB] Mon, 18 Sep 2006 09:43
Passing a delegate to a DLL as a 'void *'
#162
Author: Andrue Cope [TeamB]
Date: Mon, 18 Sep 2006 09:43
30 lines
2034 bytes
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
Re: Passing a delegate to a DLL as a 'void *'
#163
Author: Marc Rohloff TeamB
Date: Mon, 18 Sep 2006 21:50
23 lines
2069 bytes
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
Re: Passing a delegate to a DLL as a 'void *'
#164
Author: Andrue Cope [TeamB]
Date: Tue, 19 Sep 2006 07:57
27 lines
1760 bytes
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.

-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads