Jump to content
  • Advertisement
Sign in to follow this  

Need help with SWIG and void pointers

This topic is 1749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hopefully someone's got an answer. In a nutshell, I need SWIG to generate the ability for IntPtr <-> void *, and IntPtr<-> HWND.


I'm finding either SWIG's directive handling is abysmal or something is very wrong in my interface file. Don't get me wrong I'm extremely grateful for SWIG in general, it's saved my *** by generating a very thorough API for my engine, but it dropped the ball on me when it doesn't handle void pointers or HWND types properly; and if it drops the ball on me there, then it doesn't matter how good it is -- results are what matter, and I need void pointers and HWNDs, if I don't have that then it's no good to me since so much of game programming is around dealing with handles of various sorts. I'm trying to bridge between my engine's DLL and C#. Much of DX and even OpenGL (hRCs, etc) utilize void pointers and HWNDs (which are macroized as void pointers in windef.h I think). Now I'm starting to think it's SWIGs directive handling that is the problem because I've tried 4 different solutions I've found and not one of them provides me the automatic marshaling needed for C# to pass an IntPtr or a void ptr to a SWIGTYPE_p_void or SWIGTYPE_p_HWND without an error in C#.


Here's 4 links that I've tried:





http://www.nickdarnell.com/2011/05/swig-and-a-miss/  -- point #9


with any of them, I get the error: "Error: Cannot implicitly convert type 'System.IntPtr' to 'SWIGTYPE_p_HWND'"


I know why it's saying that. It's because there's no SWIG generated (Or any generated for that matter) code to handle going from an IntPtr to an HWND datatype so it's trying to convert it without code (probably through some compiler generated automatic code) and it's unable to do that -- so it fails.


An honest look as to why I'm trying to avoid extensive learning on typemaps and C# marshaling

I suppose the answer would be for me to learn how to make my own typemaps. The way I see it, for what I'm trying to do, typemaps are a feature of SWIG that will probably not be revisited again once I have the few datatypes, I need for bridging across, set up. I am very familiar with what the argument probably will be... "You don't know what you're going to need in the future". Maybe not, but I know 90% (at the very least) of what I'm doing is being done in the C++ dll, 5% is making calls to the APIs that are exported out from that DLL and the remaining 5% is probably manipulations of the UI of the editor and just basic declarations so that I can pass objects over to the C++ dll. SWIG handles most of the other datatypes perfectly ok. And not for nothing, I have tried to look at it and understand it but got nowhere since there seems to be many different ways of achieving it (as evidenced by the 4 urls I posted), some through %cs_structs, some through %typemaps and some with indepth-marshaling (Involving %typemaps and %defines, and all sorts of nasty looking stuff).


To be brutally honest, I have no interest in learning marshaling. I only need this now because SWIG, with as good as it is, can't really handle void *, and HWND properly out of the box it seems. C++ is my choice of language, not C# and I can't afford to get bogged down in the minutiae of the "wonderful exciting world of marshaling" (or %typemaps for that matter).


Quick reason as to why I'm making the editor in C#

For those of you that followed my other post: http://www.gamedev.net/topic/650937-mfc-splitter-windows-and-cviews-oh-my -- You may find my detailing this ordeal/experience helpful if you're dealing with the same questions

I hate much of the things C# takes away (And yes I'm familiar with the "unsafe {};" constructs tongue.png -- but scoping issues occur rather easily imho). From what I've read, CLI is fine if you want to rewrite your code to be CLI compliant and are ok with it being Windows platform only. Only reason I'm writing my editor in C# is because I can throw together a UI pretty quickly and get it looking the way I want fairly quickly. And I've spent enough time with MFC to realize it works pretty good so long as you stick to it's automatic back-end macro-voodoo it does for window/view creation which is, again, pretty limiting. I guess C#'s purpose is not for language-simplification (As I always believed and managed objects was really more of a bonus), it's completely about managed objects and everything else is either an effect (Simpler language syntax imho), or a bonus (visual design of UI). The visual designing of forms was/is really a tacked on super-nice bonus. If they only did the visual-design feature but for C++.. Oh wait, Borland I think did that with VCL and their .NET implementation... hmm...


I guess an attachment of my .i file is needed? There's nothing in it really except the inclusion of the headers for my engine, and anyone of your choices for the solutions found at the URLs above, but I can certainly post it if it's really needed. Thanks in advance!


-- StakFallT

Edited by StakFallT

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!