C ++回拨发送短信回C# [英] C++ Callback to send text back to C#

查看:264
本文介绍了C ++回拨发送短信回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.

我用C ++编写

该DLL中的DLL已经开始这是通过C#的运行服务的方法代码(这工作正常)

时,在DLL运行服务我想要的DLL传回文字的C#代码,这只是进度代码,如第一阶段起点和第一阶段完成了

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"



我已经环顾四周和被告知,要实现这一目标的最好办法就是使用回调,我真的没有线索如何实现这一点。没有人有任何建议或物品在那里我可以检查出来吗?请包括C ++,因为我有在C ++中零的经验。


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++.


干杯

推荐答案

您可以简单地传递一个C#字符串返回到C ++和C ++字符串到C#。的要求是,该字符串是Unicode和分配方法SysAllocString和不malloc的。 。任何ASCII字符串,你需要转换为Unicode

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);

和这个注册C#DLL

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);

这为Unicode转换为ASCII,反之亦然。

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;
}



和.NET代码:

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

编辑:

见这里更详细的资料:


See here for closer information:

http://support.microsoft.com/kb/828736

http://msdn.microsoft .COM / EN-US /库/ ms734686.aspx

这篇关于C ++回拨发送短信回C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆