开发者

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

开发者 https://www.devze.com 2023-01-29 13:14 出处:网络
I am attempting to invoke managed events from native c++, by using a managed wrapper for a nativeclass, and then using the managed wrapper via dll from C#.However, I receive a InvalidCastException run

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 ^

0

精彩评论

暂无评论...
验证码 换一张
取 消