Interop with another function in a different class - c#

I would have to call C++ code from .Net code via interop.
I just wonder whether is there anyway to interop with another function in a different class? For example, in C++, I have the following utility class:
class ConvertUtility
{
public:
static void Convert(PointList &ptList, const list<pts> &pts);
};
I wish to call it directly from .Net via interop, any idea how to do this?
Note: here's a related question asking about how to use namespace to distinguish between different method. But this time, I want nothing to do with namespace, only a class with static function.
Edit: Given that there are already too many functions in the C wrapper ( e.g, static extern "C" function that are callable from .Net, without class or namespace), I won't want to introduce an extra layer of wrapping, if I can help it.

In the related question you linked to, Ben Voigt says in a comment to the suggestion to write a C++/CLI wrapper:
This IS the correct answer. P/Invoke should only be used to call
functions with a "C" interface, which means extern "C" to prevent name
mangling, and also restrictions on parameter and return types.
Since the method is static, I see two options:
Write a simple C wrapper function that can be called with P/Invoke.
Write a C++/CLI wrapper that can be called directly from C#.

Related

Interop C++ to C#, passing callback to c++ functions

I am trying to use a native dll using DllImport in C#. Most of the methods are working fine but I am stuck in registering to callbacks. here is the sample:-
The function used for callback is static int SetReceiver(IMessageReceiver* pReceiver);
Definition for IMessageReceiver is
class IMessageReceiver
{
public:
virtual void OnMessage(unsigned char ucCommand, const void* pData, size_t size) = 0;
};
In C++ it is done like this:
class A : IMessageReceiver{ implementation of virtual function}
SetReceiver(this); // this is object of Class A.
I want to achieve same in C#. What i did is created one Interface IMessageReceiver and implemented in class. I am creating the object of that class and passing it as parameter but getting exception.
[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetMessageReceiver(IMessageReceiver pReceiver);
SetMessageReceiver(pReceiver);
You are not going to be able to solve this from your C# code. You need to provide the implementation of a C++ class. And you cannot make one of those in C#.
The only way to solve this is with C++ code. I can see a couple of obvious solutions:
Use a mixed-mode C++/CLI assembly. That can implement the unmanaged C++ class and provide a bridge between the unmanaged C++ code and the managed C# code.
In your C++ code add a concrete implementation of IMessageReceiver that implements OnMessage by forwarding to a function pointer. The function pointer can be provided by the C# code. You'll need to wrap this up in a C style function so that you can p/invoke it.

Wrapping C++ for use in C#

Ok, basically there is a large C++ project (Recast) that I want to wrap so that I can use it in my C# project.
I've been trying to do this for a while now, and this is what I have so far. I'm using C++/CLI to wrap the classes that I need so that I can use them in C#.
However, there are a ton of structs and enums that I will also need in my C# project. So how do I wrap these?
The basic method I'm using right now is adding dllexport calls to native c++ code, compiling to a dll/lib, adding this lib to my C++/CLI project and importing the c++ headers, then compiling the CLI project into a dll, finally adding this dll as a reference to my C# project. I appreciate any help.
Here is some code..I need manageable way of doing this since the C++ project is so large.
//**Native unmanaged C++ code
//**Recast.h
enum rcTimerLabel
{
A,
B,
C
};
extern "C" {
class __declspec(dllexport) rcContext
{
public:
inline rcContect(bool state);
virtual ~rcContect() {}
inline void resetLog() { if(m_logEnabled) doResetLog(); }
protected:
bool m_logEnabled;
}
struct rcConfig
{
int width;
int height;
}
} // end of extern
// **Managed CLI code
// **MyWrappers.h
#include "Recast.h"
namespace Wrappers
{
public ref class MyWrapper
{
private:
rcContect* _NativeClass;
public:
MyWrapper(bool state);
~MyWrapper();
void resetLog();
void enableLog(bool state) {_NativeClass->enableLog(state); }
};
}
//**MyWrapper.cpp
#include "MyWrappers.h"
namespace Wrappers
{
MyWrapper::MyWrapper(bool state)
{
_NativeClass = new rcContext(state);
}
MyWrapper::~MyWrapper()
{
delete _NativeClass;
}
void MyWrapper::resetLog()
{
_NativeClass->resetLog();
}
}
// **C# code
// **Program.cs
namespace recast_cs_test
{
public class Program
{
static void Main()
{
MyWrapper myWrapperTest = new MyWrapper(true);
myWrapperTest.resetLog();
myWrapperTest.enableLog(true);
}
}
}
As a rule, the C/C++ structs are used for communicating with the native code, while you create CLI classes for communicating with the .NET code. C structs are "dumb" in that they can only store data. .NET programmers, on the other hand, expect their data-structures to be "smart". For example:
If I change the "height" parameter in a struct, I know that the height of the object won't actually change until I pass that struct to an update function. However, in C#, the common idiom is that values are represented as Properties, and updating the property will immediately make those changes "live".
That way I can do things like: myshape.dimensions.height = 15 and just expect it to "work".
To a certain extent, the structures you expose to the .NET developer (as classes) actually ARE the API, with the behaviors being mapped to properties and methods on those classes. While in C, the structures are simply used as variables passed to and from the functions that do the work. In other words, .NET is usually an object-oriented paradigm, while C is not. And a lot of C++ code is actually C with a few fancy bits thrown in for spice.
If you're writing translation layer between C and .NET, then a big part of your job is to devise the objects that will make up your new API and provide the translation to your underlying functionality. The structs in the C code aren't necessarily part of your new object hierarchy; they're just part of the C API.
edit to add:
Also to Consider
Also, you may want to re-consider your choice to use C++/CLI and consider C# and p/invoke instead. For various reasons, I once wrote a wrapper for OpenSSL using C++/CLI, and while it was impressive how easy it was to build and how seamless it worked, there were a few annoyances. Specifically, the bindings were tight, so every time the the parent project (OpenSSL) revved their library, I had to re-compile my wrapper to match. Also, my wrapper was forever tied to a specific architecture (either 64-bit or 32-bit) which also had to match the build architecture of the underlying library. You still get architecture issues with p/invoke, but they're a bit easier to handle. Also, C++/CLI doesn't play well with introspection tools like Reflector. And finally, the library you build isn't portable to Mono. I didn't think that would end up being an issue. But in the end, I had to start over from scratch and re-do the entire project in C# using p/invoke instead.
On the one hand, I'm glad I did the C++/CLI project because I learned a lot about working with managed and unmanaged code and memory all in one project. But on the other hand, it sure was a lot of time I could have spent on other things.
I would look at creating a COM server using ATL. It won't be a simple port, though. You'll have to create COM compatible interfaces that expose the functionality of the library you're trying to wrap. In the end, you will have more control and a fully supported COM Interop interface.
If you are prepared to use P/Invoke, the SWIG software could maybe help you out: http://www.swig.org/

How to amend DLLs

We are using an external Dlls as :
[DllImport("DemoExport.dll")]
public static extern string GetDBConnection(string sDBName);
[DllImport("DemoExport.dll")]
public static extern int CreateEmptyDBFromDB(string SourceDBName, string DestinationDBName);
[DllImport("DemoExport.dll")]
Now, we want to add new method in same pattern. We are looking that is there any way to implement method in DemoExport.dll?So, we can use the method say DemoMethod() like:
[DllImport("DemoExport.dll")]
public static extern void DemoMethod();
It might look like a crazy question, but we really have need to implement this method so, later on we can use the same.
Additionally, if above is not possible then how to create a new dlls or say how use like DllExport which is not available in C#. So, laterly, anyone can use the method with attribute
[DllImport("dllname.dll")]
publis statis extern void mymeth();
You can't use C# to add a method to an existing pre-compiled *.dll.
You'll have to find the source for the DLL, write your method, re-compile, and then utilize that new method from C#.
EDIT
I'm still not sure what you're asking for in your update. If you want to write a new method that can be used by other C# consumers, then you don't have to do anything special with attributes. Simply write a public method on a public class.
After that, any .NET consumer can add an assembly reference to your class and use your method.
If you want any Windows consumer to be able to use your code, you can investigate COM Interop.
If you're still trying to use a C/C++ dll, then my original answer still stands.
I am not sure that i understood you correctly, but you want to add method to existing dll. If i am correct then it is not possible. other options are:
Find sources of that dll, add method and recompile
Create new dll and implement method there and use new one's name in DllImport

What is a Managed Prototype?

I just need clarification on what a managed prototype is.
I think it is a method that uses the DLLImport attribute and has a method like so:
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
Does it always mean this i.e you must have a DLLImport attribute and then a method signiture which is a private static extern???
Cheers
A function prototype is a function that is declared solely as a way for the compiler to work out how to call some code, without necessarily having that "some code" available to look at, so without the prototype it would be flying blind related to argument types, etc.
So for P/Invoke functionality, ie. calling functions in native DLLs from a managed language like C#, yes, you need those prototypes, unless you can find an already existing class in .NET that either wraps that function, DLL, or implements similar functionality in pure managed code.
And yes, you need a [DllImport...] attribute to specify whith DLL that has the function, and it should be static and extern, but it does not necessarily have to be private, although it usually is, typically because you then wrap that function in a managed class to make it easier for the rest of your code to use it.

Using a C++ class in C#

How can I use a C++ class in C#? I know you can use DllImport and have the class as IntPtr, but then how would I call the functions inside the class? And I don't want to create a new method for each method in the class and export that, then there is not point for me to use a class then...
Without creating a COM app
C++ Class:
class MyCPPClass
{
public:
MyCPPClass();
~MyCPPClass();
void call_me();
}
extern "C"
{
extern __declspec(dllexport) MyCPPClass* __cdecl CreateClass()
{
return new MyCPPClass();
}
}
C#:
class MyCSClass
{
[DllImport("MyCPPDll.dll")]
static extern IntPtr CreateClass();
public static void Main()
{
IntPtr cc = CreateClass();
// Now I want to call cc->call_me();
}
}
If you're building your classes in C++/CLI, then you just just be able to use it as you would any regular .NET class.
If the DLL in question is accessible via COM, then use the COM interop to talk to it from .NET.
If OTOH you're looking at non-COM, plain vanilla C++, you will have to ensure that all classes and their members are exported...
My suggestion would be that if you want to use a plain vanilla C++ DLL, write a C++/CLI wrapper around it that can "talk .NET" to your C# code and "real C++" to the C++ DLL. You'll still have to export all the C++ classes and functions, but at least that way you're working in the same language before you transition to C#.
To use c++ class, C++\CLI is a better option
As others have stated, C++ CLI is probably the easiest option.
The other standard option which you've started doing is to keep building on that C style facade you started and expose another method something like this:
extern "C" {
extern __declspec(dllexport) MyCPPClass* __cdecl CallMe(void* data)
{
MyCppClass* instance = (MyCppClass*) data;
instance->call_me();
}
}
then you can go through the tedium of creating a C# class that mirrors the functionality of your C++ class and hides the fact that the implementation is in another dll.
Or you could use C++ / CLI.

Categories

Resources