I have a .NET DLL that exposes a method that looks like this in C#:
void TestMethod(ref TestClass[] parameter)
Where TestClass is a regular C# class that has a COM-visible interface, and I'm supposed to call the method with an array with one element. I can't figure out how to invoke this via COM from a C++ program.
That parameter becomes a SAFEARRAY**. Currently, I'm doing this, where m_Object is a smart pointer to the class that has TestMethod:
VARIANT* current = NULL;
HRESULT hrx = SafeArrayAccessData(ret, (LPVOID*)¤t);
ITestClassPtr item;
item.CreateInstance(__uuidof(TestClass));
current->vt = VT_UNKNOWN;
current->punkVal = item.GetInterfacePtr();
SafeArrayUnaccessData(ret);
HRESULT hr = (*m_Object)->TestMethod(&ret);
This results in 0x80131533 "A mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata".
I have also tried:
ITestClassPtr item;
item.CreateInstance(__uuidof(TestClass));
long i = 0;
SafeArrayPutElement(ret, &i, item);
HRESULT hr = (*m_Object)->TestMethod(&ret);
Which results in an unhelpful 0x80131600.
I've also tried using a CComSafeArray, but I'm getting the same results (it's just a wrapper around SAFEARRAY anyway).
I do not have sources for the DLL, and I can't debug into it. I've searched around the web, and I can find lots of examples for how to invoke a C# DLL via COM, but none that covers specifically how to pass ref arrays of classes from C++ to C#.
Any ideas?
Let's suppose you have these C# classes:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class TestClass1
{
public void SayHello()
{
Console.WriteLine("Hello 1");
}
public void TestMethod(ref TestClass2[] parameter)
{
parameter[0].SayHello();
}
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class TestClass2
{
public void SayHello()
{
Console.WriteLine("Hello 2");
}
}
You can register it and create the type library with a command line like this:
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe ClassLibrary1.dll /codebase /tlb
If you look at the typelib created using OleView from Windows SDK, TestMethod is declared like this:
[id(0x60020005)]
HRESULT TestMethod([in, out] SAFEARRAY(_TestClass2*)* parameter);
So its a SAFEARRAY of _TestClass2 pointers (IUnknown-derived), and you can create it and use it like this (assuming Visual Studio support):
...
#import "c:\mypath\ClassLibrary1.tlb" raw_interfaces_only
using namespace ClassLibrary1;
int main()
{
CoInitialize(NULL);
{
// warning: all error checks omitted
CComPtr<_TestClass1> tc1;
tc1.CoCreateInstance(__uuidof(TestClass1));
tc1.SayHello(); // outputs "Hello 1"
CComPtr<_TestClass2> tc2;
tc2.CoCreateInstance(__uuidof(TestClass2));
SAFEARRAY* psa = SafeArrayCreateVector(VT_UNKNOWN, 0, 1);
LONG index = 0;
// note this will call AddRef on tc2 so it's safe
SafeArrayPutElement(psa, &index, (_TestClass2*)tc2);
tc1->TestMethod(&psa); // Outputs "Hello 2"
SafeArrayDestroy(psa);
}
CoUninitialize();
return 0;
}
Related
I have a C# dll ( for which register for COM interop option is set).
This C# dll has the below interface and class
interface IMyInterface
{
bool IsNameExists(string name);
List<string> GetNameList();
}
public class MyClass : IMyInterface
{
public bool IsNameExists(string name)
{
//DO Something
}
public List<string> GetNameList()
{
// DO something
}
}
I need to call the methods IsNameExists and GetNameList from unmanaged C++.
#import "..\..\ProdCon\bin\ProdCon.tlb" raw_interfaces_only
void main()
{
HRESULT hr =::CoInitialize(NULL);
IMyInterface pIMyInterface(__uuidof(MyClass));
VARIANT_BOOL bRet = FALSE;
BSTR bstrName = ::SysAllocString(_T("RAJESH"));
hr = pIMyInterface->IsNameExists(bstrName,&bRet);
}
So I created COM object as above and called the IsNameExists mehtod without any issue.
Since GetNameList method returns list, I am getting the below warning
'MyDll.IMyInterface.GetNameList(#0), MyDll'. Warning: Type library
exporter encountered a generic type instance in a signature. Generic
code may not be exported to COM.
Please help me how to return C# list to unmanaged C++ code. So that unmanaged C++ code can use this list.
'Generic code may not be exported to COM.' => hence its the type parameter string in public List GetNameList(). Thus essentially you need access to a non-generic c# method to get the data.
If you have control of the MyClass codebase you could (for example) add a:
public string[] GetNameArray()
{
return GetNameList.ToArray();
}
If not then you'll need to write a proxy/wrapper class to do something similar to the above and expose that via COM, either as a one off or a 'general' methodology using reflection say.
See for example http://weblog.west-wind.com/posts/2007/Jul/10/Generics-and-COM-Interop-dont-mix
I have a C++ project containing a nonmanaged class method used to display string in a user interface :
void MyProject::displayIHM(std::string mystring);
This project is compiled with /clr because it calls another one made with C#/.NET 4.0. The goal of the .NET project is to make heavy computation tasks. During computation, we want to get back from it some information to the user interface.
My idea was to create two new methods in the C++-cli project :
void MyProject::displayFromDotNet(String^ mystring)
{
displayIHM(ToStdString(mystring));
}
string ToStdString ( String ^ s)
{
const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
string os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
return os;
}
Until now, everything is ok but now the difficult part : how to provide displayFromDotNet to the .NET project. My idea was to provide a function pointer in the constructor of the .NET class and then to launch the process :
void (MyProject::*pointeurFunc)(String^) = &MyProject::displayFromDotNet;
ComputationProject^ kernel = gcnew ComputationProject((this->*pointeurFunc));
kernel->Compute();
The second line does not work. The constructor of ComputationProject has a IntPtr parameter but I do not know if I can convert a function pointer to an IntPtr^ in C++. I also made some attempts using Marshal::GetDelegateForFunctionPointer but it could not compile.
I do not know what to do, any help would be appreciated!
EDIT : yes ComputationProject is my C#/.NET project. The error with line 2 is "cannot convert parameter 1 from 'overloaded function type' to 'System::IntPtr'".
I finally find a (ugly) way.
My main problem was I could not pass a method pointer to C# because it is not a real function pointer (so I cannot cast it to IntPtr).
I decided to create a second class containing a static MyProject object and a static method calling displayIHM on the static object :
class StaticMyProject
{
public :
static MyProject staticObject;
static void DisplayInIHM(char *);
};
In cpp :
MyProject StaticMyProject::objetStatique;
void StaticMyProject::DisplayInIHM(char *message)
{
std::string message2(message);
staticObject.displayIHM(message2);
}
Now for calling Compute method of ComputationProject, I modified the code like this :
StaticMyProject::objetStatique = *this;
void (*funcPointer)(char*) = StaticMyProject::DisplayInIHM;
ComputationProject^ kernel = gcnew ComputationProject((IntPtr)funcPointer);
kernel->Compute();
And in my ComputationProject.cs :
public class ComputationProject
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FunctionPointer([MarshalAs(UnmanagedType.LPStr)]string message);
public readonly FunctionPointer DisplayMethod;
public ComputationProject(IntPtr ptr)
{
this.DisplayMethod = (FunctionPointer)Marshal.GetDelegateForFunctionPointer(ptr, typeof(FunctionPointer));
}
public int Compute()
{
this.DisplayMethod("Beginning computation...");
...
}
}
I've got a C++/CLI project that wraps a large number of classes - and the classes have their own meta-data system. So I'd like to return a dynamic object to make some use cases easy in C#. But I don't see how to do this in C++.
In C# I can write the following:
dynamic TestThisOut()
{
return null;
}
void mork()
{
var d = TestThisOut();
d.Fork();
}
I would like to write TestThisOut() in C++/CLI, so that I can use it exactly the same way as I do in the "mork" function above (i.e. not having to type the dynamic keyword). Is this possible?
You can create a dynamic object in c++/cli, you just can't consume it there as you would in C# project. However you can consume it from C#. Here is how you do it:
In C++/CLI create a property or a method that returns Object^. Mark this property or method with System::Runtime::CompilerServices::DynamicAttribute. You are good to go.
Here is a quick sample I created:
namespace ClassLibrary1
{
using namespace System;
using namespace System::Dynamic;
public ref class Class1 : public DynamicObject
{
public:
[System::Runtime::CompilerServices::Dynamic]
static property Object^ Global
{
public:
Object^ get()
{
return gcnew Class1();
}
}
public:
String^ Test()
{
return "Test";
}
};
}
And my C# consumer:
Console.WriteLine( ClassLibrary1.Class1.Global.Test() );
dyanmic is C# 4.0 keyword. It is not supported in C++ CLI (and also in VB.NET).
If you want to consume dynamic object in C++ CLI, then you can use impromptu-interface library. List of supported options. Information is from this question
I have a cpp project, a cpp cli project and a c# win forms project. I want to fire a method from my native cpp code and catch it in c# project. How can i do this?
There can be multiple approaches to answer this question, because the dependency requirement between those projects is important. I will try to answer for the most common (i guess) case: in which you already have a native C++ library and you want to use that library in a C# application. In that scenario the C# project depends on the native library project. In such a case you can utilize a gateway cli/c++ library to transform native c++ events to .NET events.
Here is a complete code sample, but before that, please note:
It may not be the shortest solution, but it works fine. Also it can provide more control on transforming native data to .net types.
I used this approach in VS 2005. I dont know if there is a better instrument in newer versions of VS for that specific interoperability purpose.
If your native event is triggered from a thread other than the GUI thread, then beware of that.
The Native Library:
#ifndef _NATIVE_CODE_H_
#define _NATIVE_CODE_H_
//NativeCode.h
//A simple native library which emits only one event.
#include <stdlib.h>
#include <iostream>
using namespace std;
#define NATIVELIBRARY_API __declspec(dllexport)
//An argument class to wrap event parameters
class NativeEventArgs{
public:
//a 32bit integer argument
//any other primitives can be here, just be careful about the byte size
int argInt32;
//null terminated ascii string
const char* argString;
//null terminated wide/unicode string
const wchar_t* argWString;
};
//A simple mechanism to fire an event from native code.
//Your library may have a DIFFERENT triggering mechanism (e.g. function pointers)
class INativeListener
{
public:
virtual void OnEvent(const NativeEventArgs& args)=0;
};
//The actual native library code, source of native events
class NATIVELIBRARY_API NativeCode
{
public:
NativeCode()
:theListener_(NULL)
{}
//Listener registration method
void registerListener(INativeListener* listener) {
theListener_ = listener;
}
//this is the very first source of the event
//native code emits the event via the listener mechanism
void eventSourceMethod() {
//... other stuff
//fire the native event to be catched
if(theListener_){
//prepare event parameters
NativeEventArgs args;
wstring wstr(L"A wide string");
string str("A regular string");
//build-up the argument object
args.argInt32 = 15;
args.argString = str.c_str();
args.argWString = wstr.c_str();
//fire the event using argument
theListener_->OnEvent( args );
}
}
private:
//native code uses a listener object to emit events
INativeListener* theListener_;
};
#endif
Gateway Library Sample:
//GatewayCode.h
//GatewayLibrary is the tricky part,
//Here we listen events from the native library
//and propagate them to .net/clr world
#ifndef _GATEWAY_CODE_H_
#define _GATEWAY_CODE_H_
#include "../NativeLibrary/NativeCode.h" //include native library
#include <vcclr.h> //required for gcroot
using namespace System;
using namespace System::Runtime::InteropServices;
namespace GatewayLibrary{
//.net equvelant of the argument class
public ref class DotNetEventArg{
internal:
//contructor takes native version of argument to transform
DotNetEventArg(const NativeEventArgs& args) {
//assign primitives naturally
argInt32 = args.argInt32;
//convert wide string to CLR string
argWString = Marshal::PtrToStringUni( IntPtr((void*)args.argWString) );
//convert 8-bit native string to CLR string
argString = Marshal::PtrToStringAnsi( IntPtr( (void*)args.argString) );
//see Marshal class for rich set of conversion methods (e.g. buffers)
}
private:
String^ argString;
String^ argWString;
Int32 argInt32;
public:
//define properties
property String^ ArgString {
String^ get() {
return argString;
}
}
property String^ ArgWString {
String^ get() {
return argWString;
}
}
property Int32 ArgInt32 {
Int32 get() {
return argInt32;
}
}
};
//EventGateway fires .net event when a native event happens.
//It is the actual gateway class between Native C++ and .NET world.
//In other words, It RECEIVES NATIVE events, TRANSFORMS/SENDS them into CLR.
public ref class EventGateway {
public:
//ctor, its implementation placed below
EventGateway();
//required to clean native objects
~EventGateway();
!EventGateway();
//the SENDER part
//.net event stuff defined here
delegate void DotNetEventHandler(DotNetEventArg^ arg);
event DotNetEventHandler^ OnEvent;
private:
//our native library code
//notice you can have pointers to native objects in ref classes.
NativeCode* nativeCode_;
//the required device to listen events from the native library
INativeListener* nativeListener_;
internal: //hide from .net assembly
//the RECEIVER part, called when a native event received
void OnNativeEvent(const NativeEventArgs& args){
//you can make necessary transformation between native types and .net types
//create .net argument using native argument
//required conversion is done by DotNetEventArg class
DotNetEventArg^ dotNetArgs = gcnew DotNetEventArg(args);
//fire .net event
OnEvent( dotNetArgs );
}
};
}
//A concrete listener class. we need this class to register native library events.
//Its our second gateway class which connects Native C++ and CLI/C++
//It basically gets events from NativeLibary and sends them to EventGateway
class NativeListenerImp : public INativeListener {
public:
NativeListenerImp(gcroot<GatewayLibrary::EventGateway^> gatewayObj ){
dotNetGateway_ = gatewayObj;
}
//this is the first place we know that a native event has happened
virtual void OnEvent(const NativeEventArgs& args) {
//inform the .net gateway which is responsible of transforming native event to .net event
dotNetGateway_->OnNativeEvent(args);
}
private:
//class member to trigger .net gateway.
//gcroot is required to declare a CLR type as a member of native class.
gcroot<GatewayLibrary::EventGateway^> dotNetGateway_;
};
////ctor and dtors of EventGateway class
GatewayLibrary::EventGateway::EventGateway()
{
nativeCode_ = new NativeCode();
//note; using 'this' in ctor is not a good practice
nativeListener_ = new NativeListenerImp(this);
//register native listener
nativeCode_->registerListener(nativeListener_);
}
GatewayLibrary::EventGateway::~EventGateway()
{
//call the non-deterministic destructor
this->!EventGateway();
}
GatewayLibrary::EventGateway::!EventGateway()
{
//clean up native objects
delete nativeCode_;
delete nativeListener_;
}
#endif
And the final application in C# (or in any other .net language):
//Program.cs
//C# the final evet consumer application
using System;
using System.Collections.Generic;
using System.Text;
using GatewayLibrary;
namespace SharpClient
{
class Program
{
static void Main(string[] args)
{
//create the gateway
EventGateway gateway = new EventGateway();
//listen on .net events using the gateway
gateway.OnEvent += new EventGateway.DotNetEventHandler(gateway_OnEvent);
}
static void gateway_OnEvent( DotNetEventArg args )
{
//use the argument class
Console.WriteLine("On Native Event");
Console.WriteLine(args.ArgInt32);
Console.WriteLine(args.ArgString);
Console.WriteLine(args.ArgWString);
}
}
}
I have an unmanaged dll with a class "MyClass" in it.
Now is there a way to create an instance of this class in C# code? To call its constructor? I tried but the visual studio reports an error with a message that this memory area is corrupted or something.
Thanks in advance
C# cannot create class instance exported from native Dll. You have two options:
Create C++/CLI wrapper. This is .NET Class Library which can be added as Reference to any other .NET project. Internally, C++/CLI class works with unmanaged class, linking to native Dll by standard C++ rules. For .NET client, this C++/CLI class looks like .NET class.
Write C wrapper for C++ class, which can be used by .NET client with PInvoke. For example, over-simplified C++ class:
class MyClass()
{
public:
MyClass(int n){data=n;}
~MyClass(){}
int GetData(){return data;}
private:
int data;
};
C API wrapper for this class:
void* CreateInstance()
{
MyClass* p = new MyClass();
return p;
}
void ReleaseInstance(void* pInstance)
{
MyClass* p = (MyClass*)pInstance;
delete p;
}
int GetData(void* pInstance)
{
MyClass* p = (MyClass*)pInstance;
return p->GetData();
}
// Write wrapper function for every MyClass public method.
// First parameter of every wrapper function should be class instance.
CreateInstance, ReleaseInstance and GetData may be declared in C# client using PInvoke, and called directly. void* parameter should be declared as IntPtr in PInvoke declaration.
The solution is create C++/CLI wrapper like:
#include "DllExportClass.h"
public ref class ManagedOperationHelper
{
public:
double Sum(double add1, double add2)
{
CDllExportClass obj;
double ret=obj.Sum(add1, add2);
return ret;
}
double Mult(double mult1, double mult2)
{
CDllExportClass obj;
double ret=obj.Mult(mult1, mult2);
return ret;
}
};
where CDllExportClass is the class exported from native code. Above is the .h of the C++/CLI. Take care to let find the lib to this dll. Put the dll and the lib in the same directory and compile the C++/CLI code.In the managed code directory put the native dll and the C++/CLI dll. In the managed project put the reference of the C++/CLI project. Instanciate in the maged code the C++/CLI class like:
ManagedOperationHelper obj = new ManagedOperationHelper();
double ret=obj.Sum(10, 20);
It's all.
You can not use unmanged C++ code directly in C#. The interoperability can be done using PInvoke. There are a lot of issues related to this topic, especially when calling functions which have pointers as arguments.
The basic procedure goes like this:
C# part
namespace MyNamespace {
public class Test {
[DllImport("TheNameOfThe.dll")]
public static extern void CreateMyClassInstance();
public void CallIt() {
CreateMyClassInstance(); // calls the unmanged function via PInvoke
}
}
}
C++ part
class MyClass {
public: MyClass() { /** Constructor */ }
};
MyClass* staticObject;
extern "C" void CreateMyObjectInstance() {
staticObject = new MyClass(); // constructor is called
}