Sign in to follow this  
psykr

C++ and C#

Recommended Posts

C# is going to be an important asset for programmers to know (possess?), so I'm planning on learning it with a bit of game programming. Since my game engine is almost done (in C++), I plan to learn C# by putting the engine in a DLL, then writing games in C#, importing the engine functions from the C++ DLL. However, with this design, a few problems come to mind. I don't know much about C#, so I hope someone else can fill me in. 1) I have a memory manager that tacks extra information onto allocations, for debug purposes and leak tracking. If I were to interface with C#, how would objects be passed across DLL boundaries? If I created an object (with extra padding) in the DLL, then gave it to C# to handle, would I be able to manipulate it the way C++ manipulates cross-DLL objects? 2) Does running the DLL in the context of a C# application affect its.. capabilities? If I had self-modifying code in the DLL, would it still work correctly?

Share this post


Link to post
Share on other sites
Quote:
Original post by psykr
1) I have a memory manager that tacks extra information onto allocations, for debug purposes and leak tracking. If I were to interface with C#, how would objects be passed across DLL boundaries? If I created an object (with extra padding) in the DLL, then gave it to C# to handle, would I be able to manipulate it the way C++ manipulates cross-DLL objects?
less about DLLs and more about managed. unmanaged code that allocates something is responsible for deallocating it. IIRC.
Quote:
2) Does running the DLL in the context of a C# application affect its.. capabilities? If I had self-modifying code in the DLL, would it still work correctly?
First, WHY?!? Second, it should... if it worked before.

Share this post


Link to post
Share on other sites
Quote:
Original post by sbayeta
You loose a lot of performance when invoking unmanaged code from managed code. You should try to use C# exclusively, unless you have no other choice.
Who told you that?

Share this post


Link to post
Share on other sites
As I understand it, there is a bit of a speed hit involved with switching between managed and unmanaged code, which can have be problematic if, for instance, you are using OpenGL in immediate mode by P/Invoking glVertex calls.

As long as the native stuff works on a sufficiently high conceptual level, it shouldn't be an issue.

Share this post


Link to post
Share on other sites
There is some overhead with invoking native functions from managed code, though it's not going to be a real killer (unless you do something stupid).

Another alternative (the route that I'm going): My engine is all in C++, and I'm scripting things out in C#. There's an intermediate layer of C++/CLI as well (really convenient here: I can access both native and CLI code without doing anything special. This is extremely beneficial).

All in all, C# isn't really difficult to learn if you're already a C++ programmer. Now that we finally have templates to work with, you can pretty much do everything that you could do in C++ (unless we're talking about low level memory management type stuff, of course).

Share this post


Link to post
Share on other sites
Quote:

Approximate overhead for a platform invoke call: 10 machine instructions (on an x86 processor)

Approximate overhead for a COM interop call: 50 machine instructions (on an x86 processor)


[rolleyes]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this