digitalmars.D.learn - Dynamic Loading of C DLLs and How to Use Them
- Joshua Cearley (25/25) Mar 22 2006 braddr from #d asked me to post this here, so here we go :)
braddr from #d asked me to post this here, so here we go :) This has probably been posted before, but it's useful to keep in an accessible place. When trying to cast a function pointer to an external C version of one, it requires this really awkward trick. Placing the alias outside of a class at the module level inside an extern (C) will make an alias of a C function pointer. You can then use this alias to convert the function pointers. Here is an example: -- Boilerplate types -- extern (C) { alias void function() CFunc; void function() testVar; } alias void function() NotCFunc; -- Usage example -- // This does NOT work and gripes about unable to convert to C testVar = cast(NotCFunc)GetSomeCFuncPointer(); // This DOES work testVar = cast(CFunc)GetSomeCFuncPointer(); -- End of Example -- From my observations the alias command will capture the external state as part of the alias, which means this is the only way to cast to C function pointers at this time. -Joshua Cearley BlackCrystal Software
Mar 22 2006