• Advertisement
Sign in to follow this  

[.net] How to use native code in c#.net

This topic is 3762 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

I have a dll of my engine. Now i have to use it to develop a tool. I want to program this tool in c#.net but the dll was programmed in c++. How i can do this? ( sorry for bad english -_- )

Share this post


Link to post
Share on other sites
Advertisement
If you only need to a call a few functions, you can use pinvoke. If you need to access a larger collection of functionality, you will probably want to write a wrapper in C++/CLI.

Share this post


Link to post
Share on other sites
It is managed C++. You can write pure C++ in it but you can also write garbage collected managed C++ as well. Being able to combine the two (with some limitations) allows you to create a dll that is able to be used by all other .Net languages. Within that dll you can wrap the unmanaged code in a managed class.

E.g

ref class Foo
{
int WrapperFunction( int dataIn )
{
return WrappedUnmanagedCFunction( dataIn );
}
};

You will need to become good friends with google for a bit but it really isn't that hard and it can make things a LOT easier.

Share this post


Link to post
Share on other sites
Just use a CLR class library to create a wrapper around the unmanaged code and then include it as a reference into the project where you need to use it. The code I wrote below isn't 100% correct since i just wrote it off the top of my head, but see what you can figure out and let us know how it goes.

also .NET 2.0 Interoperability Recipes by Bruce Bukovics is a great book and is definitely worth checking out if your doing a lot of stuff with interop.


#pragma unmanaged
//unmanaged class
class CUnmangedClass
{
public:
CUnmanagedClass();
~CUnmanagedClass();

void DoStuff();
}

#pragma managed
// managed wrapper class
ref CManagedWrapper
{
CUnmanagedClass ^ ptr;
public:
CManagedWrapper()
{
ptr = new CUnmanagedClass();
}
~CManagedWrapper()
{
delete ptr;
}

void DoStuff()
{
ptr->DoStuff();
}
};

Share this post


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

  • Advertisement