c# using a c++ engine

Started by
7 comments, last by Rob Loach 18 years, 7 months ago
I have a c++ engine, and im intrested in making some sort of mesh viewer in c#, so with my engine running inside my c# application and using the c++ code. is this possible? how would i go about it? currently my engine compiles in to a lib (not a dll).
Advertisement
Use managed C++.
for my engine you mean?
to convert my managed code it is just some project options i assume?
Consider using SWIG. Your lib will be usable from Pyhton, Perl, etc as well.

hth,
CipherCraft
hrmm, looks simple enough!
do i just need to set up my project to compile as a dll and the i files i create take care of the rest?
Kinda [wink]

Trouble is templates. You can only define template instantiations and rename them for SWIG, since most other languages do not have a concept like templates:

%template(ArrayOfInts) Array<int>;


Plus operators must be renamed, which is also because of 'limitations' in other languages:

%rename(IsEqualTo)	operator ==;%rename(IsUnequalTo)	operator !=;%rename(IsLessThan)	operator <;%rename(IsMoreThan)	operator >;..etc...


Other than that it's quite a usefull tool.

cu,
CipherCraft
Quote:Original post by supagu
for my engine you mean?
to convert my managed code it is just some project options i assume?

Oh goodness no. You basically have to partially rewrite it.

Don't do it - terrible idea.

Using C# isn't a bad idea if you're calling your DLL-ified C++ engine. If you can pull it off, it'll probably work quite well.

The other alternative is to use a C++ windowing system, possibly through a language like Python (I recomend wxWidgets).
I wrote a managed C++ wrapper for my engine in a night and hooked it to a C# application, it's quite simple actually. You can either write it by hand or try using SWIG to generate the wrapper.
You can use P/Invoke to call functions in C++ DLLs, much like the Tao Framework makes class wrapper functions to OpenGL and SDL for use with .NET languages like C#. You can even make sequential class structures in C# to match up to the class interface you made in C++ held in the DLL.

To call a C++ function in a DLL file, you use the DLLImport attribute:
[DllImport("CppEngine.dll", ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]public static extern bool Initialize(string windowTitle, int width, int height);
The P/Invoke is very smart in converting various data formats back and forth. For example, your C++ function could take a char* and in C# you could pass in a string and P/Invoke will translate it for you [wink]. I've never tried SWIG before, I should try it out.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement