I have written the following code that uses .net4 to run. Since I am using this code in Unity3D that doesn't support .net4 I had to convert it to native code. I am using "Unmanaged Exports" to export it to a native library.
I can call this function from Unity3D and it works fine.
Now I want to add events to my library and then access these events from another C# code in Unity3D.
My question is: How to create a callback event and register a function in my library to call when it is done? I know how to create an event in a Managed c# code. But note that here I am exporting it to native code and I need to use the event in the native code. So, My library does something as a native code and then I want it to fire a callback function to tell Unity3D that it is done.
Here is my library code:
[DllExport("SayText", CallingConvention = CallingConvention.Cdecl)]
public static void SayText()
{
var task = new Task(() =>
{
var synth = new SpeechSynthesizer();
synth.SetOutputToNull();
synth.SetOutputToWaveFile("D:\\test3.wav");
synth.SpeakCompleted += SynthOnSpeakCompleted;
synth.SpeakAsync("This is a test text");
});
task.Start();
}
private static void SynthOnSpeakCompleted(object sender, SpeakCompletedEventArgs speakCompletedEventArgs)
{
var synth = (SpeechSynthesizer)sender;
synth.SpeakCompleted -= SynthOnSpeakCompleted;
synth.SetOutputToDefaultAudioDevice();
// I want to trigger an event here so the application knows that the file has been created.
}
And here is how I'm calling this function from Unity3D:
[DllImport("mylib", EntryPoint = "SayText")]
public static extern void SayText();
I can't be sure it will solve your issue but I tried something similar with ios plugin.
Here is the C# side of the plugin:
public class CSharpWrapper
{
// Create delegate type
public delegate void TestDelegate();
// Connection with the native side
[DllImport("__Internal")]
private static extern void externMethod( TestDelegate onCompletion);
private static Action callback = null;
// This is most likely the part you are looking for.
// That method is marshalled so it will pass its address to the native side
// When native calls it we can add any code within
[MonoPInvokeCallback(typeof(TestDelegate))]
private static void ManagedTest()
{
if(callback != null) { callback(); }
callback = null;
}
#endif
public static void CallMethod(Action<string> onCompletion)
{
callback = onCompletion;
// Here we pass our own method that is marshalled for native side
externMethod(ManagedTest);
}
}
Then I have a C section in the .m file.
extern "C"
{
typedef void (*TestCallback)();
void externMethod(TestCallback testCallback)
{
// Here communication with Objective-C code
}
}
Maybe this will get you somewhere near completion. It is also possible to add parameters, just list them as usual and the marshalling attribute will do it all for you.
Related
I am trying to marshal a callback via PInvoke from a managed C# program to a native C++ DLL. These callbacks may be invoked after large delays on a separate thread. My current design is finding that the callback is invoked correct with the right data if called immediately at the native layer, but is failing if called at a delay on a separate thread. Here is a simple example of the setup:
C# Interface
public class Example
{
private delegate void NativeLoginCallback(IntPtr bytes);
[DllImport ("NativeExample")]
private static extern void Example_loginAsync(NativeLoginCallback callback);
private delegate void LoginCallback(string data);
public void LoginAsync(LoginCallback callback)
{
NativeLoginCallback nativeCallback = (IntPtr bytes) =>
{
string data = MarshalUtility.Deserialize<string>(bytes);
try
{
callback(data);
}
catch (Exception ex)
{
Debug.Log(ex);
}
}
Example_loginAsync(nativeCallback);
}
}
C Interface
extern "C" __declspec(dllimport) void Example_loginAsync(void* callback)
{
typedef void(*CSharpLoginCallbackType)(void* data);
CSharpLoginCallbackType csharpCallback = (CSharpLoginCallbackType) callback;
auto cppLoginCallback = [csharpCallback](std::string data)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
unsigned char* dataBytes;
MarshalUtility::Serialize(data, dataBytes);
csharpCallback(dataBytes);
};
std::string testData = "This one is called immediately on same thread.";
unsigned char* testDataBytes;
MarshalUtility::Serialize(testData, testDataBytes);
csharpCallback(testDataBytes);
std::thread t(cppLoginCallback, "This one is called after 5 seconds on seperate thread.");
t.detach();
}
C# Caller
public class ExampleIntegrator
{
Example example = new Example();
public void Test()
{
example.Login((string data) =>
{
Debug.Log(data);
};
}
};
The output I see is:
This one is called immediately on same thread.
NullReferenceException
The output I expect is:
This one is called immediately on same thread.
This one is called after 5 seconds on seperate thread.
Upon inspection, I have discovered that the data is correct at the scope of NativeLoginCallback i.e. after the line string data = MarshalUtility.Deserialize<string>(bytes);, the debugger correctly shows This one is called after 5 seconds on seperate thread., however it finds that callback is a NullReference. How do I ensure callback does not become null?
I'm trying to use an dll on my project which have a method that receives a function as parameter, but I don't know how to do that call using C#.
Here is the example that the dll manual provides.
do_pkd(void(*put-byte) (char));
I think it's an example writed in C and I don't know how to perform the same call on C#
Here is the way that I'm trying to do now
public static void Main(string[] args)
{
Console.WriteLine(do_pkd(put_byte));
}
[DllImport(#"C:\pkwin32.dll", CallingConvention = CallingConvention.Cdecl)]
static unsafe extern char do_pkd(Action<char> put_byte);
private static void put_byte(char s)
{
//TO-DO
Console.WriteLine(s);
}
I'm receiving the following error:
Can not perform marshalling of 'parameter # 1': Can not perform marshalling of generic types.
I used to register a delegate from C# to a callback function pointer like this (without using Marshal.GetFunctionPointerForDelegate):
C++ (Test.dll)
typedef void (__stdcall* Callback)(int num);
Callback pCallback = 0;
void __stdcall SetCallback(Callback callback)
{
pCallback = callback;
}
void __stdcall ActionCpp()
{
if(pCallback)
{
pCallback();
}
}
C#
class Program
{
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void SetCallBack(Callback callback);
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void ActionCpp();
public delegate void Callback();
static void Action()
{
Console.WriteLine("Callback succeeded.");
}
static void Main(string[] args)
{
Callback myAction = new Callback(Action);
SetCallback(myAction);
ActionCpp(); //Suppose to do the same thing as Action();
}
}
It seems that this way works fine. However, I found that we can do the same thing by using Marshal.GetFunctionPointerForDelegate and register the IntPtr of the delegate to the function pointer in C++.
I would like to know what the difference is and which practice is better? (And also Why? Thanks in advance.)
Here is the C# code when using Marshal.GetFunctionPointerForDelegate. (Nothing changes in C++ code.)
C# (With Marshal.GetFunctionPointerForDelegate)
class Program
{
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void SetCallBack(IntPtr pCallBack); //change to type "IntPtr" this time.
[DllImport("Test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void ActionCpp();
public delegate void Callback();
static void Action()
{
Console.WriteLine("Callback succeeded.");
}
static void Main(string[] args)
{
Callback myAction = new Callback(Action);
//GCHandle gcHandle = GCHandle.Alloc(myAction); <-- Is this necessary?
IntPtr pMyAction = Marshal.GetFunctionPointerForDelegate(myAction);
SetCallback(pMyAction);
ActionCpp();
//gcHandle.Free();
}
}
I have another further but related question about whether it is necessary to use "GCHandle.Alloc" (as in my comments in the above code) to avoid any GC action as long as my Callback myAction is still alive?
I am a newbie on C# and C++ callbacks, please let me know if I have naive mistakes made.
A callback requires the start address of the method that handles the event. So your two methods are both getting the same start address. The first method is clear since you are using the name of the method. 2nd method you have more statements that accomplishes the same as first method.
c# is managed language while c++ is un-managed. Which basically means that extra protection was added to c# to prevent PC from going blue screen. So when calling un-managed c++ from c# rules must be followed to prevent blue screen. So any pointer variables passed to a c++ method must be allocated in c# as un-managed using GCHandle.Alloc or by using other Marshal methods. Marshal.StrToPtr will also allocate the un-managed memory.
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);
}
}
}
In a c# assembly I've got a global prism CompositeCommand to subscribe to like this:
private static readonly CompositeCommand __myCommand = new CompositeCommand();
public static CompositeCommand MyCommand
{
get { return _myCommand; }
}
From c# I can subscribe to this command using:
[...].MyCommand.RegisterCommand(new DelegateCommand<MyClass>((c) => c.Something()));
My problem: I need to subscribe to the command from managed c++, and I got no idea how the function signature needs to be to be used in DelegateCommand. Most of the time I get errors like:
error C2664: 'Microsoft::Practices::Prism::Commands::DelegateCommand::DelegateCommand(System::Action ^)': conversion of parameter 1 from 'void (__clrcall *)(MyNameSpace::MyClass ^)' in 'System::Action ^' not possible.
How do I subscribe to a c# command? Or is there some other way to listen to an event (I can replace the CompositeCommand with something different).
Thanks!
I'm going to assume that you're using managed C++ - anything else, and there's bigger issues to worry about.
It looks like you're getting linking errors to your C# type. As such, I don't think the issue is related to any problems with Prism. In order for the C++ managed compiler to link to your C# assembly, you need to produce the C# assembly with an XML documentation file (see the Build tab in your properties). Is that enabled in your project?
I used the following as a very simple proof of concept, where TestObject is defined in the C# assembly referenced by the C++ DLL. This compiled without any issues.
Header file:
void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param);
public ref class ManagedModule : IModule
{
public:
ManagedModule();
virtual void __clrcall Initialize();
private:
};
Implementation:
ManagedDLL::ManagedModule::ManagedModule()
{
}
void __clrcall ManagedDLL::ManagedModule::Initialize()
{
Action<Project::Infrastructure::TestObject^>^ newAction =
gcnew Action<Project::Infrastructure::TestObject^>(&CommandCallback);
DelegateCommand<Project::Infrastructure::TestObject^>^ newCommand =
gcnew DelegateCommand<Project::Infrastructure::TestObject^>(newAction);
Project::Infrastructure::Commands::ApplicationExitCommand->RegisterCommand(newCommand);
return;
}
void __clrcall CommandCallback(Project::Infrastructure::TestObject^ param)
{
return;
}