C++将文本发送回 C# 的回调

发布于 2024-10-18 01:27:02 字数 303 浏览 0 评论 0原文

我是 C++ 新手。我被告知使用 C++ 的“回调”是最好的解决方案。这是我的情况。

我有一个用 C++ 编写的 DLL
这个 DLL 有一个方法来启动通过 C# 代码运行的服务(这工作正常)
当 DLL 中的服务运行时,我希望 DLL 将文本传递回 C# 代码,这只是进度代码,例如“第一阶段开始”和“第一阶段完成”


我环顾四周,被告知实现这一目标的最佳方法是使用回调,我真的不知道如何实现这一点。有人有什么建议或文章可供我查看吗?请包括 C++,因为我对 C++ 的经验为零。


干杯

I'm new to C++. I have been told using a "callback" with C++ is the best solution for this. Here is my situation.

I have a DLL written in C++
this DLL has a method to start the service which is run via the C# code (this works fine)
when the service in the DLL runs I want the DLL to pass back text to the C# code, this is just progress code such as "stage one starting " and "stage one completed"

I have looked around and been told that the best way to achieve this is to use callbacks, I don't really have a clue how to implement this. Does anyone have any suggestions or articles out there I can check out? Please include C++ as I have zero experience in C++.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烏雲後面有陽光 2024-10-25 01:27:03

回调只是委托的一种特殊用法。普通模型看起来像这样:

public class MyCaller
{
   public OtherClass otherClassInstance;

   public void CallbackMethod() {...}

   public void UsesTheCallback()
   {
      //The callback method itself is being passed
      otherClassInstance.MethodWithCallback(CallbackMethod);
   }
}

public class OtherClass
{
   public delegate void CallbackDelegate()
   public void MethodWithCallback(CallbackDelegate callback)
   {
      //do some work, then...
      callback(); //invoke the delegate, "calling back" to MyCaller.CallbackMethod()
   }
}

该模型的优点在于任何具有定义的“签名”(参数和返回类型)的方法都可以用作回调。这是一种“松散耦合”的形式,其中代码不依赖于知道对象是什么,只依赖于它做什么,因此可以将该对象交换为另一个对象,而代码不知道其中的差异。

上面的例子都是C#的。当您使用 C++ DLL 中的 extern 函数时,您可能会处理 IntPtr 类型,它是指向该方法的简单指针。在定义该 extern 方法的 C# 接口时,您应该能够将此指针“编组”为委托,因此该方法看起来像普通的 C# 方法。

A callback is simply a particular use of a delegate. The normal model looks something like this:

public class MyCaller
{
   public OtherClass otherClassInstance;

   public void CallbackMethod() {...}

   public void UsesTheCallback()
   {
      //The callback method itself is being passed
      otherClassInstance.MethodWithCallback(CallbackMethod);
   }
}

public class OtherClass
{
   public delegate void CallbackDelegate()
   public void MethodWithCallback(CallbackDelegate callback)
   {
      //do some work, then...
      callback(); //invoke the delegate, "calling back" to MyCaller.CallbackMethod()
   }
}

The beauty of this model is that any method with the defined "signature" (parameters and return type) can be used as the callback. It's a form of "loose coupling", where code isn't dependent on knowing what an object IS, only what it DOES, so that object can be swapped out for another object without the code knowing the difference.

The above example is all in C#. When you're using extern functions from a C++ DLL, you'll probably be dealing with an IntPtr type that is a simple pointer to the method. You should be able to "marshal" this pointer as a delegate when defining the C# interface to that extern method, so the method will look like a normal C# method.

许你一世情深 2024-10-25 01:27:02

可能有更干净的方法,但以下是我用来使其发挥作用的一些步骤。

定义委托和将其传递给 DLL 的函数。这些参数将被发送回 C# 委托:

  public delegate uint CallbackFn( uint param1, uint param2 );

  [DllImport("yourdll.dll",  CallingConvention=CallingConvention.Winapi, EntryPoint="RegisterTheCallback" )]
  private static extern uint RegisterTheCallback( CallbackFn pfn );

创建一个变量来存储委托。确保这不会超出范围。在我的测试中,我发现 GC 会回收它(它没有“意识到”我的 DLL 仍在使用它):

  CallbackFn mCmdCallback = null;

然后在某处初始化它:

  mCmdCallback = new CallbackFn( YourCallback );

然后将它传递给您的 DLL:

RegisterTheCallback( mCmdCallback );

并定义将接收的实际方法调用:

  private uint YourCallback( uint param1, uint param2 )
  {
    // report progress etc.
  }

DLL 中的代码可能如下所示:

DWORD _declspec( dllexport ) WINAPI RegisterTheCallback
(
   DWORD (WINAPI *lpfnCallback)( DWORD param1, DWORD param2 )
)
{
// Store lpfnCallback somewhere so that it can be called later
...
}

然后 DLL 中的代码可以在需要时使用适当的数据调用它:

ret = (lpfnCallback)( 234, 456 );

There may be cleaner ways, but here are some of the steps I used to make it work.

Define the delegate and the function to pass it to your DLL. The parameters are what will be sent back to the C# delegate:

  public delegate uint CallbackFn( uint param1, uint param2 );

  [DllImport("yourdll.dll",  CallingConvention=CallingConvention.Winapi, EntryPoint="RegisterTheCallback" )]
  private static extern uint RegisterTheCallback( CallbackFn pfn );

Create a variable to store the delegate. Make sure this doesn't go out of scope. In my testing, I found that the GC would reclaim it (it wasn't "aware" that my DLL was still using it):

  CallbackFn mCmdCallback = null;

Then initialize it somewhere:

  mCmdCallback = new CallbackFn( YourCallback );

And then pass it to your DLL:

RegisterTheCallback( mCmdCallback );

And define the actual method that will receive the call:

  private uint YourCallback( uint param1, uint param2 )
  {
    // report progress etc.
  }

The code in the DLL might look like this:

DWORD _declspec( dllexport ) WINAPI RegisterTheCallback
(
   DWORD (WINAPI *lpfnCallback)( DWORD param1, DWORD param2 )
)
{
// Store lpfnCallback somewhere so that it can be called later
...
}

And then the code in your DLL can call it with the appropriate data when it needs:

ret = (lpfnCallback)( 234, 456 );
相思故 2024-10-25 01:27:02

您可以简单地将 C# 字符串传递回 C++,并将 C++ 字符串传递回 C#。要求是字符串是unicode并且分配方法是SysAllocString而不是malloc。任何需要转换为 unicode 的 ASCII 字符串。

const wchar_t* theString = L"hello";
BSTR bstr = SysAllocString(theString);
DoSomething(bstr);
SysFreeString(bstr);

注册 C# dll

Assembly asm = Assembly.LoadFile (@"c:\temp\ImageConverter.dll");
RegistrationServices regAsm = new RegistrationServices();
bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);

并将 Unicode 转换为 ASCII,反之亦然。

inline BSTR Cstring2VBstring(char *szString)
{
    WCHAR* res = NULL;
    BSTR bs;
    DWORD n;
    char *sz = NULL;
    if (*szString && szString)
    {
        sz = strdup(szString);
        n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, NULL, 0);

        if (n)
        {
            res = (WCHAR*) malloc(n * sizeof(char) );
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
        }

    }

    bs = SysAllocString( (const OLECHAR*) res);
    free(sz);
    return bs;
}



// C String to BSTR conversion (2)
BSTR Cstringn2VBstring(char *szString, int dwSize)
{
    WCHAR* res = NULL;
    BSTR bs;
    DWORD n = (DWORD) dwSize;
    char *sz = NULL;
    if (*szString)
    {
        sz = (char*) malloc(dwSize);
        memcpy(sz, szString, dwSize);
        n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, n, NULL, 0);
        if(n)
        {
            res = (WCHAR*) malloc(n * sizeof(char) );
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
        }
    }
    bs = SysAllocStringLen( (const OLECHAR*) res, n);

    free(sz);
    return bs;
}

.NET 代码:

Namespace TestLibrary2
    ' Interface declaration. '
    Public Interface ICalculator
        Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer
        Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long
        Function ReturnValue() As String
        Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String

        Sub Concat2(ByVal Number1 As String, ByVal Number2 As String)

        Function isTrue(ByVal bInputvalue As Boolean) As Boolean
        Function isTrue2(ByRef bInputvalue As Boolean) As Boolean
    End Interface



    ' Interface implementation. '
    Public Class ManagedClass
        Implements ICalculator


        Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer Implements ICalculator.Add
            Return Number1 + Number2
        End Function


        Public Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long Implements ICalculator.Subtract
            Try
                System.IO.File.WriteAllText("c:\temp\subtract.txt", "Subtracted: ")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Return Number1 - Number2
        End Function


        Public Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String Implements ICalculator.Concat
            Try
                System.IO.File.WriteAllText("c:\temp\Concat.txt", "Nummer1: " + Number1 + vbCrLf + "Nummer2:" + Number2)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Dim strReturnValue As String = Number1 + Number2
            Return strReturnValue
        End Function


        Public Sub Concat2(ByVal Number1 As String, ByVal Number2 As String) Implements ICalculator.Concat2
            Console.WriteLine("moo")
        End Sub


        Public Function ReturnValue() As String Implements ICalculator.ReturnValue
            Dim x As String = "moooooo"
            Return x
        End Function


        Public Function isTrue(ByVal bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue
            If bInputvalue = True Then
                Return True
            End If
            Return False
        End Function


        Public Function isTrue2(ByRef bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue2
            If bInputvalue = True Then
                Return True
            End If
            Return False
        End Function

    End Class


End Namespace

编辑:
请参阅此处了解更详细的信息:

http://support.microsoft.com/kb/828736
http://msdn.microsoft.com/en-us/library/ms734686.aspx

You can simply pass a C# string back to C++ and a C++ string to C#. The requirement is that the string is unicode and the allocation method is SysAllocString and not malloc. Any ASCII string you need to convert to unicode.

const wchar_t* theString = L"hello";
BSTR bstr = SysAllocString(theString);
DoSomething(bstr);
SysFreeString(bstr);

And this to register the C# dll

Assembly asm = Assembly.LoadFile (@"c:\temp\ImageConverter.dll");
RegistrationServices regAsm = new RegistrationServices();
bool bResult = regAsm.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase);

And this to convert Unicode to ASCII and vice-versa.

inline BSTR Cstring2VBstring(char *szString)
{
    WCHAR* res = NULL;
    BSTR bs;
    DWORD n;
    char *sz = NULL;
    if (*szString && szString)
    {
        sz = strdup(szString);
        n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, NULL, 0);

        if (n)
        {
            res = (WCHAR*) malloc(n * sizeof(char) );
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
        }

    }

    bs = SysAllocString( (const OLECHAR*) res);
    free(sz);
    return bs;
}



// C String to BSTR conversion (2)
BSTR Cstringn2VBstring(char *szString, int dwSize)
{
    WCHAR* res = NULL;
    BSTR bs;
    DWORD n = (DWORD) dwSize;
    char *sz = NULL;
    if (*szString)
    {
        sz = (char*) malloc(dwSize);
        memcpy(sz, szString, dwSize);
        n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, n, NULL, 0);
        if(n)
        {
            res = (WCHAR*) malloc(n * sizeof(char) );
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, res, n);
        }
    }
    bs = SysAllocStringLen( (const OLECHAR*) res, n);

    free(sz);
    return bs;
}

And the .NET code:

Namespace TestLibrary2
    ' Interface declaration. '
    Public Interface ICalculator
        Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer
        Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long
        Function ReturnValue() As String
        Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String

        Sub Concat2(ByVal Number1 As String, ByVal Number2 As String)

        Function isTrue(ByVal bInputvalue As Boolean) As Boolean
        Function isTrue2(ByRef bInputvalue As Boolean) As Boolean
    End Interface



    ' Interface implementation. '
    Public Class ManagedClass
        Implements ICalculator


        Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer Implements ICalculator.Add
            Return Number1 + Number2
        End Function


        Public Function Subtract(ByVal Number1 As Long, ByVal Number2 As Long) As Long Implements ICalculator.Subtract
            Try
                System.IO.File.WriteAllText("c:\temp\subtract.txt", "Subtracted: ")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Return Number1 - Number2
        End Function


        Public Function Concat(ByVal Number1 As String, ByVal Number2 As String) As String Implements ICalculator.Concat
            Try
                System.IO.File.WriteAllText("c:\temp\Concat.txt", "Nummer1: " + Number1 + vbCrLf + "Nummer2:" + Number2)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Dim strReturnValue As String = Number1 + Number2
            Return strReturnValue
        End Function


        Public Sub Concat2(ByVal Number1 As String, ByVal Number2 As String) Implements ICalculator.Concat2
            Console.WriteLine("moo")
        End Sub


        Public Function ReturnValue() As String Implements ICalculator.ReturnValue
            Dim x As String = "moooooo"
            Return x
        End Function


        Public Function isTrue(ByVal bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue
            If bInputvalue = True Then
                Return True
            End If
            Return False
        End Function


        Public Function isTrue2(ByRef bInputvalue As Boolean) As Boolean Implements ICalculator.isTrue2
            If bInputvalue = True Then
                Return True
            End If
            Return False
        End Function

    End Class


End Namespace

Edit:
See here for closer information:

http://support.microsoft.com/kb/828736
http://msdn.microsoft.com/en-us/library/ms734686.aspx

誰認得朕 2024-10-25 01:27:02

这很棘手,但这是代码——我花了一段时间才弄清楚——感谢支持。
你能从 C DLL 调用 C# DLL 吗?< /a>

此示例是非托管 C++ 到 C#。

It's tricky but here's the code -- took me a while to figure out -- upvotes appreciated.
Can you call a C# DLL from a C DLL?

This example is unmanaged c++ to C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文