Pra*_*ell 5 c++ com flutter-desktop
我正在尝试在 flutter 桌面应用程序中调用 ac# dll 中的函数,
在我的 C# dll 中,
using System;
using System.Runtime.InteropServices;
namespace MathLibrary
{
// NOTE: I have set set [assembly: ComVisible(true)] in AssemblyInfo.cs
[ComVisible(true)]
[Guid("66DE2FB9-7A3B-4C33-AF26-9AD5EDD4C71F")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMathLibrary
{
[DispId(1)]
string multiply(int a, int b);
};
[ComVisible(true)]
[Guid("021E950E-3612-4FAD-9F15-F61632A95BD8")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MathLibrary.MathCalc")]
public class MathCalc : IMathLibrary
{
public string multiply(int a, int b)
{
return "Product is " + (a * b).ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这个存储库作为基础 flutter 应用程序。
在应用程序中,我使用平台通道在 dart 和 C++ 代码之间进行通信。在 C++ 代码中,我尝试调用 C# 函数(在文件 windows/runner/custom_channel.cpp 中)。经过一番谷歌搜索后,我想出了以下内容
首先,我向 tlb 文件添加了导入(必须向生成的 tlh 文件添加导入才能使 IntelliSense 工作)
#import "bin/MathLibrary.tlb"
using namespace MathLibrary;
Run Code Online (Sandbox Code Playgroud)
下面的函数应该调用c#函数
CoInitialize(NULL);
MathLibrary::IMathLibraryPtr IcalcPtr;
HRESULT hr = ::CoCreateInstance(__uuidof(MathLibrary::MathCalc), NULL,
CLSCTX_INPROC_SERVER,
__uuidof(MathLibrary::IMathLibrary),
(void**)&IcalcPtr);
_bstr_t calcVal;
if (FAILED(hr) || IcalcPtr == nullptr) {
// CoCreateInstance failed
// THIS CONDITION IS MET
(*resPointer)->Error("Cannot Create COM Object");
return;
}
//IcalcPtr->multiply(a, b, &calcVal);
calcVal = IcalcPtr->multiply(a, b);
// not sure how to convert bstr to std::string
const char* calcStr((const char*) calcVal.GetBSTR());
c.assign(calcStr);
CoUninitialize();
Run Code Online (Sandbox Code Playgroud)
CoCreateInstance 失败。
由于我没有c++经验,我很困惑,
IcalcPtr->multiply(a, b)很长,但我认为它会是 int我想了解一般如何从 c++ 与 c# com 接口交互以及如何使其在我的情况下工作。示例代码和文档链接会很有帮助
| 归档时间: |
|
| 查看次数: |
4736 次 |
| 最近记录: |