[英]C++ Callback Function to C#
I want to rewrite a Function from C++ to C#. I haven't any experience with C++ but that's what I already have written.我想将 Function 从 C++ 重写为 C#。我对 C++ 没有任何经验,但这就是我已经写过的。 The DLL Import with some functions already works.But this callback things struggles me.具有某些功能的 DLL Import 已经可以使用。但是这个回调让我很困扰。 Pls help me:)请帮助我:)
c++ code: c++ 代码:
/* Structure for PBORCA_CompileEntryImport callback function. */
typedef struct pborca_comperr
{
INT iLevel;
LPTSTR lpszMessageNumber;
LPTSTR lpszMessageText;
UINT iColumnNumber;
UINT iLineNumber;
} PBORCA_COMPERR, FAR *PPBORCA_COMPERR;
/* Prototype for PBORCA_CompileEntryImport callback function. */
typedef PBCALLBACK(void, *PBORCA_ERRPROC) ( PPBORCA_COMPERR, LPVOID );
my c# code:我的 c# 代码:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct PBORCA_COMPERR
{
public int iLevel;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszMessageNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszMessageText;
public int iColumnNumber;
public int iLineNumber;
}
private delegate void PBORCA_CALLBACK(IntPtr pDirEntry, IntPtr lpUserData);
From what I understand there are a few possible ways you can do it.据我了解,有几种可能的方法可以做到这一点。 It will depend on what you require.这将取决于您的需求。
Option 1) Using IntPtr
and Marshal.GetFunctionPointerForDelegate
选项 1) 使用IntPtr
和Marshal.GetFunctionPointerForDelegate
// A template of the function you want other code to call
// Your calling convention may be different
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] // note calling convention
delegate void PBORCA_CALLBACK(IntPtr pDirEntry, IntPtr lpUserData);
class Orca
{
[DllImport("LibraryName.dll")]
private static extern int PBORCA_CompileEntryImport(
IntPtr hORCASession,
...
long lEntrySyntaxBuffSize,
IntPtr pCompErrorProc, // note the IntPtr
IntPtr pUserData);
// An implementation of the function delegate (this will be called)
private static void StaticImpOfTheCallback(IntPtr pDirEntry, IntPtr lpUserData)
{
// write code here (Notice: I'm a static function)
}
// An implementation of the function delegate (this will be called)
private void NonStaticImpOfTheCallback(IntPtr pDirEntry, IntPtr lpUserData)
{
// write code here (Notice: I'm not static)
}
// Create a delegate object which will link the callback function
private PBORCA_CALLBACK staticCallbackDel = StaticImpOfTheCallback;
public void SomeCallingFunction()
{
PBORCA_CompileEntryImport(
0,
... ,
128,
Marshal.GetFunctionPointerForDelegate(staticCallbackDel), // Delegate to function ptr
0);
PBORCA_CompileEntryImport(
0,
... ,
128,
Marshal.GetFunctionPointerForDelegate(
new PBORCA_CALLBACK(NonStaticImpOfTheCallback) // create a delegate instance
), // Delegate to function ptr
0);
}
}
Option 2 Using delegate
directly in the definition方案2 直接在定义中使用delegate
// A template of the function you want other code to call
// Your calling convention may be different
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] // note calling convention
delegate void PBORCA_CALLBACK(IntPtr pDirEntry, IntPtr lpUserData);
class Orca
{
[DllImport("LibraryName.dll")]
private static extern int PBORCA_CompileEntryImport(
IntPtr hORCASession,
...
long lEntrySyntaxBuffSize,
[MarshalAs(UnmanagedType.FunctionPtr)] // May or may not be needed
PBORCA_CALLBACK pCompErrorProc, // note the use of the delegate
IntPtr pUserData);
// An implementation of the function delegate (this will be called)
private static void StaticImpOfTheCallback(IntPtr pDirEntry, IntPtr lpUserData)
{
// write code here (Notice: I'm a static function)
}
// An implementation of the function delegate (this will be called)
private static void NonStaticImpOfTheCallback(IntPtr pDirEntry, IntPtr lpUserData)
{
// write code here (Notice: I'm a static function)
}
// Create a delegate object which will link the callback function
private PBORCA_CALLBACK staticCallbackDel = StaticImpOfTheCallback;
public void SomeCallingFunction()
{
PBORCA_CompileEntryImport(
0,
... ,
128,
staticCallbackDel, // no need to convert the delegate
0);
PBORCA_CompileEntryImport(
0,
... ,
128,
StaticImpOfTheCallback, // could call directly too.
0);
PBORCA_CompileEntryImport(
0,
... ,
128,
NonStaticImpOfTheCallback, // could call directly too.
0);
// same as above (I think)
PBORCA_CompileEntryImport(
0,
... ,
128,
new PBORCA_CALLBACK(NonStaticImpOfTheCallback), // more explicit
0);
}
}
These are most of the variations I can think of.这些是我能想到的大部分变化。 One of them should suit you.其中之一应该适合你。 If anything above is unclear I suggest you read some documentation on how delegates work in normal C# and experiment there before trying to use them in conjunction with C++ code.如果以上任何内容不清楚,我建议您阅读一些有关委托如何在正常 C# 中工作的文档,并在尝试将它们与 C++ 代码结合使用之前在那里进行试验。 There are plenty of tutorial available for delegates online one of them should hopefully get the penny to drop if it hasn't already.网上有很多可供代表使用的教程,如果还没有的话,其中之一应该有望让一分钱掉下来。
Good Luck祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.