Sign in to follow this  
Liquid-Snake

[.net] Importable COM DLL written in C++ for C#?

Recommended Posts

Liquid-Snake    122
Hey guys, I want to create a COM DLL written in C++ so I can import it easily in my C# application. More specifically, I would like to create a COM DLL so that I could just add it under "References" in my C# application and be able to use it ala .NET framework/classes style. (i.e. be able to import the namespace and use it like everyhing else in the .NET framework) Is this possible, and how would I go about doing this? Thanks guys.

Share this post


Link to post
Share on other sites
Demirug    884
If you only want to import some native C++ code in your C# program I recommend the usage of the CLR/C++ compiler. You get what you want and don’t need to mess around with COM.

Share this post


Link to post
Share on other sites
Liquid-Snake    122
Thanks for the response guys.

Bob Janova: I have an engine written in C++ that I want to expose to C#. Basically I'm writing the Level Editor/GUI in C# (since it's a nice language to do it in).

How do I create a COM object from my C++ code? Do you guys know of any tutorials available, or perhaps some sample code?

THANKS alot!

Share this post


Link to post
Share on other sites
Liquid-Snake    122
Long story short, I have a game completely written in non-managed C++, and I want to write a level editor in C#. Therefore I need to expose my C++ engine to my C# application.

I've done this before using P/Invoke, i.e. importing the DLL symbols via attributes/DLL imports, but I'd hate to write all these wrappers for my whole engine (and I would have to do it in 2 places, DLL exporting my interfaces in C++ and importing those interfaces in my C# app).

It's pretty nice what Tom Miller and the rest of the gang at the MDX team did. I like how you get the whole DX interface just by importing the namespace and using it.

I want that very same thing :)

Share this post


Link to post
Share on other sites
Demirug    884
If you want to do the same thing you should use CLR/C++ to build a small wrapper around your unmanaged C++ objects. This is how MDX and many other assemblies build. I have done the same for the RawInput API and started to build a managed Direct3D 10 layer.

Share this post


Link to post
Share on other sites
Liquid-Snake    122
Excellent, thanks for the reply Demirug!

Do you know of any examples or samples in which I can see this? I have my code base in (.h) headers and .cpp files, and I'd hate to copy and paste this stuff into managed C++ with unsafe { } blocks to get it to compile into managed-c++/clr.

Share this post


Link to post
Share on other sites
Demirug    884
You don’t need unsafe blocks in CLR/C++ as you can mix managed and unmanaged classes in one DLL. The compiler will do all the dirty work behind. If your Engine is already in a DLL I recommend that you create a new CLR/C++ project that includes this DLL like a normal C++ Application. Than you can create a managed class for every unmanaged class you want to export to C#. This managed class can than contains pointers to your unmanaged classes.
Maybe this small example from my managed D3D10 layer can help you:

pragma once

#include "ShaderByteCode.h"
#include "DeviceChild.h"

using namespace System::Runtime::InteropServices;

namespace DirectX
{
namespace Direct3D10
{
public ref class PixelShader : DeviceChild
{
public:
PixelShader(DirectX::Direct3D10::Device^ device, ShaderByteCode^ bytecode)
{
CreateInternal (device, bytecode->Code);
}

property ShaderByteCode^ ByteCode
{
ShaderByteCode^ get ()
{
DirectX::Direct3D10::Device^ device = Device;

if (device->NativeStateMirror == NULL)
throw gcnew InvalidCallException ();

D3D10_PIXEL_SHADER_DESC Desc;

HRESULT hr = device->NativeStateMirror->GetPixelShaderDesc (shader, &Desc);

Direct3D10Exception::CheckResult (hr);

return gcnew ShaderByteCodePointer ((void*)Desc.pFunction, Desc.SizeInBytes);
}
}

internal:
PixelShader(ID3D10PixelShader* shader)
: shader (shader)
{
Register ();
}

property ID3D10PixelShader* Shader
{
ID3D10PixelShader* get ()
{
return shader;
}
}
virtual property ID3D10DeviceChild* NativeChild
{
ID3D10DeviceChild* get () override
{
return shader;
}
}

virtual void ClearNativeChild () override
{
shader = NULL;
}

protected:
ID3D10PixelShader* shader;

void CreateInternal (DirectX::Direct3D10::Device^ device, void* pcode)
{
ID3D10PixelShader* pShader;

HRESULT hr = device->NativeDevice->CreatePixelShader (pcode, &pShader);

Direct3D10Exception::CheckResult (hr);

shader = pShader;

Register ();
}
};
}
}

Share this post


Link to post
Share on other sites
Liquid-Snake    122
Awesome, thanks Demirug.

I need to pick up a managed-c++ book, or read about it online -- I'm only versed in C#! Any recommendations?

This looks like exactly what I need. Thanks again.

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