gpt4 book ai didi

c++ - 将数据从 C# 传递到非托管 C++(使用 COM Interop)

转载 作者:行者123 更新时间:2023-11-28 07:23:23 25 4
gpt4 key购买 nike

我正在使用 Com Interop 方法与非托管 C++ 和 C# 进行通信。

我需要从 C# 向非托管 C++ 发送数据。

我已经从 C# 发送“bool”值并通过 C++ 中的“VARIANT_BOOL*”访问它。

我需要从 C# 发送一个整数。我如何在非托管 C++ 端访问该整数值?

例如:

C#

 public int myValue()
{
return 5;
}

非托管 C++

CoInitialize(NULL);
MyNSpace::MyClassPtr IMyPointer;

HRESULT hRes = IMyPointer.CreateInstance(MyNSpace::CLSID_MyClass);

if (hRes == S_OK)
{
//// ??? define x type

IMyPointer->myValue(x);

}

最佳答案

COM 允许使用普通( native )整数类型,例如 LONG。 COM LONG 在 C++ 中代表 32 位有符号整数。例如,

HRESULT myValue([out, retval] LONG* nOutVal);

在客户端 (c++) 代码中,您只需声明一个 ordinal int 变量:

if (hRes == S_OK)
{
int x;
hRes = IMyPointer->myValue(x);

}

关于c++ - 将数据从 C# 传递到非托管 C++(使用 COM Interop),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19091217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com