InvalidCastException implementing .net events in native c++ with managed wrapper - c#

I am attempting to invoke managed events from native c++, by using a managed wrapper for a native class, and then using the managed wrapper via dll from C#. However, I receive a InvalidCastException runtime error that I don't know how to resolve, or even what it means. My implementation is as follows:
The header for ManagedClass is
#pragma once
#include "NativeClass.h"
using namespace System;
using namespace System::Runtime::InteropServices;
namespace NativeLibrary
{
public delegate void ReportProgressDelegate(Double ^completionPercentage);
public ref class ManagedClass
{
public:
ManagedClass();
event ReportProgressDelegate ^OnReportProgress;
void RaiseReportProgress(Double ^completionPercentage);
void CallTestFunction();
private:
NativeClass *_NativeClass;
ReportProgressDelegate ^ReportProgress;
};
}
while ManagedClass source file is
#include "stdafx.h"
#include "ManagedClass.h"
#include "TestFunction.h"
using namespace NativeLibrary;
ManagedClass::ManagedClass()
{
_NativeClass = new NativeClass();
ReportProgress = gcnew ReportProgressDelegate(this, &ManagedClass::RaiseReportProgress);
_NativeClass->ProgressFunction = (void (*)(double))Marshal::GetFunctionPointerForDelegate(ReportProgress).ToPointer();
void ManagedClass::RaiseReportProgress(System::Double ^completionPercentage)
{
OnReportProgress(completionPercentage);
}
void ManagedClass::CallTestFunction()
{
TestFunction(*_NativeClass);
}
}
Here NativeClass is defined in its own header file
#pragma once
class NativeClass
{
public:
NativeClass() {};
void (*ProgressFunction)(double);
};
as is TestFunction
#pragma once
#include "NativeClass.h"
void TestFunction(NativeClass& nativeClass)
{
nativeClass.ProgressFunction(50.0);
return;
}
I set up a C# program to test the event. My C# program consists of
NativeLibrary.ManagedClass managedClass = new NativeLibrary.ManagedClass();
managedClass.OnReportProgress += new NativeLibrary.ReportProgressDelegate(managedClass_OnReportProgress);
managedClass.CallTestFunction();
where the managedClass_OnReportProgress event is defined as
void managedClass_OnReportProgress(ValueType completionPercentage)
{
MessageBox.Show("report progress event triggered");
}
The problem occurs when I call the function CallTestFunction. When I step through using the debugger, it successfully makes it inside the native TestFunction. However, when it reaches the closing brace of TestFunction, I get the runtime error:
"InvalidCastException was unhandled. No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))"
I'm really clueless as to how to resolve this problem, or what the source of it is. Suggestions?

public delegate void ReportProgressDelegate(Double ^completionPercentage);
Double is a value type. You should only use the ^ for reference types. I'm guessing it bombs when it tries to convert a boxed Double (an Object) to a native double. Delete the ^

Related

Invoke .NET method via COM, with a ref array

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*)&current);
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;
}

Call C# delegate from CLI class that wraps native C++ class

I have a CLI wrapper to a C++ class. The C++ class has a callback that passes
a char* array, and the length of that array.
void (CPP_CALLBACK*)(char* data, unsigned int dataLength)
In C# land, I have this callback
private delegate void AppPacketReceivedDelegate(byte[] data);
My CLI wrapper receives the CPP_CALLBACK callback, and must then somehow call the C# delegate.
Any ideas on how to do this ? I tried
System::Action<cli::array<char>^>
but I am not sure how to match the delegate to this Action.
Update:
I convert the delegate into a function pointer, and then I can call the function pointer from CLI using this syntax:
typedef void(__stdcall * WRAPPER_APP_PACKET_CALLBACK) (cli::array<unsigned char>^);
But, when I call this, the array is always of size 1 !
Here a simple example, not exactly matching your situation, but it could be quickly adapted.
CppClass.h
namespace MyCppNamespace
{
typedef void (__stdcall *callback_function)(int, const char*);
class CppClass
{
private:
callback_function _callbackFunc;
public:
void DoSomething(callback_function callbackFunc);
}
}
DotNetClass.cs
namespace MyManagedNamespace
{
public delegate void ManagedCallback(int size, string message);
}
CliWrapperClass.h
#include "CppClass.h"
using namespace MyManagedNamespace;
namespace MyCliNamespace
{
public ref class CliWrapperClass
{
private:
CppClass *_cppClass;
public:
void DoSomething(ManagedCallback ^ callback);
}
}
CliWrapperClass.cpp
#include "CliWrapperClass.h"
namespace MyCliNamespace
{
void CliWrapperClass::DoSomething(ManagedCallback ^ callback)
{
System::IntPtr callbackPtr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(callback);
_cppClass->DoSomething(static_cast<callback_function>(callbackPtr.ToPointer()));
}
}
In the .Net class, you can create an instance of CliWrapperClass and call its DoSomething(...) function in this way:
namespace MyManagedNamespace
{
public class ManagedClass
{
private ManagedCallback _callback = MyCallback;
private void MyCallback(int size, string message)
{
// Do what you want ...
}
public void MyFunction()
{
CliWrapperClass wrapper = new CliWrapperClass();
wrapper.DoSomething(_callback);
}
}
}
In the end, I marked the C# class and the corresponding delegate as unsafe, and
then I was able to call the delegate from C++ and pass over the C-style array. On the C# side, I created a managed array and copied the bytes over using Marshal.Copy

Passing a C# delegate to a C++/CLI wrapper

I was following this example to use a c# delegate as a c++ callback.
My wrapper looks like this:
// GLFWWrapper.h
#pragma once
using namespace System;
namespace GLFWWrapper {
typedef void(__stdcall *KEY_CALLBACK)(int, int, int, int);
public ref class Window {
public:
void setKeyCallback(KEY_CALLBACK fp);
static KEY_CALLBACK csharpKeyCallback;
...
};
}
// GLFWWrapper.cpp
#include "stdafx.h"
#include "GLFWWrapper.h"
namespace GLFWWrapper {
void cppKeyCallback(GLFWwindow * window, int key, int scancode, int action, int mods) {
if (Window::csharpKeyCallback) {
Window::csharpKeyCallback(key, scancode, action, mods);
}
}
void Window::setKeyCallback(KEY_CALLBACK fp) {
csharpKeyCallback = fp;
}
...
}
The cppKeyCallback function is a callback I set in another function (glfwSetKeyCallback(m_ptr, cppKeyCallback);). The csharpKeyCallback is supposed to be the delegate from my C# project. In the example I linked to above it is only explained how I can set a delegate from inside my wrapper. But how can I set the delegate from my C# project? When I try to call setKeyCallback from my C# project I get an error Window.setKeyCallback(?) is not supported by the language.
You seem to be missing the managed part of the instruction:
#pragma managed
public delegate void keyCallback(int, int, int, int);
void Window::setKeyCallback(keyCallback^ fp) {
csharpKeyCallback = fp;
}
In order to pass a reference to a function instance (delegate) from C# to C++\CLI you need to declare it i.e. declare a managed delegate. This can be done either in C# or in C++\CLI.
In your code the KEY_CALLBACK is an unmanaged function declaration. Thus it is "Not supported by the language". C# side cannot use it as a delegate declaration.
In order to avoid such a mess I myself always keep my C++\CLI projects clear of any managed declarations and provide those in an additional C# class library that is referenced from both the C++\CLI wrapper and the C# side.
Edit: Do not forget to put the ^ sign after the managed reference type in the managed method declaration or the compiler is going to bust you with an error 'A delegate type is not allowed here'.

Wrapping "C++ dll" in "C++/CLI" when function returns pointer to abstract class

I have c++ dll:
UnmanagedCode.h looks like:
#include "stdafx.h"
struct Class1
{
public: virtual void method() = 0;
};
extern "C" __declspec(dllimport) Class1* Create_function();
UnmanagedCode.cpp looks like:
#include "stdafx.h"
#include "UnmanagedCode.h"
class Class2 : Class1
{
public: void method(){}
};
Class1* Create_function()
{
Class2* c2 = new Class2(); // I don't care about free memory for this example
Class1* c1 = (Class1*)c2;
return c1;
};
I have c++/cli managed class:
#include "Library.h"
#include "UnmanagedCode.h"
typedef Class1* (*Createfunction)();
namespace CLIWrapper
{
public ref class ManagedClI
{
private:
Class1* cl1;
public:
ManagedClI(){}
void Create()
{
//some usual routine for loading c++ library with Library class
String ^path = "path to the library.dll"; // just to show
using System::Runtime::InteropServices::Marshal;
const char* cpath = (const char*)(Marshal::StringToHGlobalAnsi(path)).ToPointer();
Library loader;
loader.load((string)cpath, false);
Marshal::FreeHGlobal(System::IntPtr((void*)cpath));
Createfunction hDet = (Createfunction)loader.getProcAddress("Create_function");
cl1 = hDet();
cl1->method(); // if I call cl1->method() here it works perfect!!
}
void SomeFunction()
{
cl1->method(); //but if I call cl1->method() here it throws an error!!!
}
};
}
I use ManagedClI class in my c# application, Something like:
CLIWrapper.ManagedClI object = new CLIWrapper.ManagedClI();
object.Create();
object.SomeFunction(); // <- this causes an error
object.SomeFunction() causes the error: attempted to read or write protected memory when it calls cl1->method().
But in the same time cl1->method() is normally working in Object.Create().
I think that I'm doing something wrong when wrapping Create_function().
Can anyone suggest something?
Your Library loader is more than just a loader. It is the CLI object that keeps the DLL loaded. When the last reference to it goes away and it is cleaned up, the DLL is unloaded.
The virtual function table of your abstract class lives in that DLL, as does the functions that it points to. When the DLL is unloaded, that virtual function table becomes invalid, and attempting to call the method is going to be undefined behavior.
To fix this in a first approximation, store the Library object in your ManagedClI class instead of as a local variable.

Cpp / Cli Fire an Event

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);
}
}
}

Categories

Resources