我正在为Clarion(Clarion是C ++编程语言)程序编写C#.NET DLL。
我很好地调用了C#.NET DLL,一切都很好用。但是,我需要C#.NET DLL接收Clarion过程以进行回调,然后能够调用该过程传递三个int参数。
Clarion Procedure看起来像这样(Clarion long是C#int):
MyCallBack procedure(long p1, long p2, long p3)
... Data ...
code
... Code ...
如何将abvoe过程传递给C#.NET DLL,C#.NET DLL如何调用该过程传递三个int参数?
提前致谢。
答案 0 :(得分:2)
希望此示例为您提供了一个开始的地方,它基于SoftVelocity新闻组(缓存的纯文本版本here)中的示例。
注意:我正在使用RGiesecke DllExport包和Clarion LibMaker的修改版本来创建兼容的lib文件。你提到你已经没有问题地调用C#DLL所以我假设你正在做类似的事情。如果您有兴趣,我会在blog上进一步讨论。
PROGRAM
MAP
MODULE('ManagedCSharpDLL.dll')
CallbackProc PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue),TYPE,PASCAL,DLL(TRUE)
SetCallback PROCEDURE(*CallbackProc pCallback),NAME('SetCallback'),PASCAL,RAW,DLL(TRUE)
TestCallback PROCEDURE(*CString passedString),NAME('TestCallback'),PASCAL,RAW,DLL(TRUE)
END
Callback PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue),PASCAL
END
a CSTRING(20)
CODE
Message('Clarion: SetCallback(Callback)')
SetCallback(Callback)
a = 'Call Test Worked'
Message('Clarion: Send message: ' & a)
TestCallback(a)
Message('Clarion: Made call and got back safely')
Callback PROCEDURE(BSTRING PassedValue, *BSTRING ReturnValue)
CODE
MESSAGE('Clarion: Passed Value: ' & PassedValue)
ReturnValue = 'Done'
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RGiesecke.DllExport;
namespace ManagedCSharpDLL
{
public static class UnmanagedExports
{
private static CallbackProc _callback;
[DllExport("SetCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static void SetCallback(CallbackProc pCallback)
{
_callback = pCallback;
MessageBox.Show("C#: SetCallback Completed");
}
[DllExport("TestCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static void TestCallback(string passedString)
{
string displayValue = passedString;
string returnValue = String.Empty;
MessageBox.Show("C#: About to call the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
_callback(displayValue, ref returnValue);
MessageBox.Show("C#: Back from the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
}
public delegate void CallbackProc( [MarshalAs(UnmanagedType.BStr)] String PassedValue, [MarshalAs(UnmanagedType.BStr)] ref String ReturnValue);
}
}