Connecting Tech Pros Worldwide Forums | Help | Site Map

Marshalling a function pointer to a delegate without .NET 2.0

Jason Bell
Guest
 
Posts: n/a
#1: Nov 17 '05
I know this functionality already exists in .NET 2.0 via
Marshal.GetDelegateForFunctionPointer, but I'd rather not force my users to
use a beta framework (not to mention the fact that I'm still using good old
vs .net 2002, that refuses to use the 2.0 framework).

Specifically, I'm using an external declaration of the Simple Directmedia
Library function SDL_GL_GetProcAddress to acquire the procedure addresses
of OpenGL extensions. Unfortunately, you can't make simple external
declarations for them. In other languages you simply acquire function
pointers to them.

This is the external declaration for SDL_GL_GetProcAddress:

[DllImport("sdl.dll", EntryPoint = "SDL_GL_GetProcAddress")]
public static extern IntPtr GetProcAddress(string funcname);

You provide the name of the extension function, and the return value is a
pointer to that function.

I'm willing to use the unsafe version as well, I'll just wrap it in a safe
wrapper to make sure it's CLS compliant.

[DllImport("sdl.dll", EntryPoint = "SDL_GL_GetProcAddress")]
private unsafe static extern void * GetProcAddress(string funcname);

So how would one go about marshaling the function pointer to a delegate in
1.0 or 1.1? Surely there's some roundabout way of doing it.

Mattias Sjögren
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Marshalling a function pointer to a delegate without .NET 2.0


>So how would one go about marshaling the function pointer to a delegate in[color=blue]
>1.0 or 1.1?[/color]

The simple answer is that you can't do it. The easiest solution is to
write a wrapper in a language that supports calling functions by
function pointer (such as C++).



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jason Bell
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Marshalling a function pointer to a delegate without .NET 2.0


Yeh I figured I'd wind up writing an unmanaged library, at least until 2.0
is the standard. I had just hoped there was some circuitous route in .NET
that allowed you to call an unmanaged function pointer.

Thanks for the help.

Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote in
news:uQFASb4LFHA.2492@TK2MSFTNGP14.phx.gbl:
[color=blue][color=green]
>>So how would one go about marshaling the function pointer to a
>>delegate in 1.0 or 1.1?[/color]
>
> The simple answer is that you can't do it. The easiest solution is to
> write a wrapper in a language that supports calling functions by
> function pointer (such as C++).
>
>
>
> Mattias
>[/color]

Closed Thread