如何在 C++ 中创建和初始化双精度型的 SAFEARRAY传递给 C#
我的 C# 方法需要从 C++ 调用
最初我的 C# 方法采用 double[] 类型的参数,但是当从 C++ 调用时,它变成了 SAFEARRAY
在 C++ 中,我需要从双精度数组中获取数据,并填充 SAFEARRAY。我还没有找到任何示例代码来执行此操作。
任何帮助表示赞赏
My C# method needs to be invoked from C++
Originally my C# method takes a parameter of type double[], but when calling from C++ it becomes a SAFEARRAY
In C++ I need to take data from an array of doubles, and populate a SAFEARRAY. I have not found any sample code to do this.
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是在 C++ 中创建安全数组的代码。
完成后释放指针,如以下代码 -
如果您使用 C++ 的 ATL,则最好使用“atlsafe.h”中声明的 CComSafeArray。这是 SAFEARRAY 的包装。 CComSafeArray 类
Following is the code to create a safearray in C++.
Free the pointer when you are finished like the following code-
If you use ATL for C++, then better use CComSafeArray declared in "atlsafe.h". This is wrapper for SAFEARRAY. CComSafeArray Class
继续@Liton的回答,我想强调他的最后一句话,即ATL的
CComSafeArray
。它确实可以节省您大量的打字时间。CComSafeArray
具有 C++ 构造函数、析构函数、运算符重载,其中包括一个 [ ] 运算符重载,它为您提供对SAFEARRAY
中任何元素的读/写引用。简而言之,您可以真正专注于业务逻辑,而不必担心SAFEARRAY
管道:至少,即使您不打算使用
CComSafeArray
值得在
中解构其源代码,让您更好地了解SAFEARRAY
函数的内容、时间、原因和方式。Continuing on @Liton's answer, I want to stress his last sentence, i.e. ATL's
CComSafeArray
. It really can save you a lot of typing.CComSafeArray
has C++ constructors, destructors, operator overloads including one for [ ] that gives you an read / write reference to any element in theSAFEARRAY
. In short, you can really focus on your business logic and needn't worry about theSAFEARRAY
plumbing:At the very least, even if you're not going to use
CComSafeArray
it's worthwhile to deconstruct its source code in<atlsafe.h>
giving you better insight on the what, when, why and how onSAFEARRAY
functions.不建议传递 SAFEARRAY。建议将 SAFEARRAY 放入 VARIANT 中。此外,SAFEARRAY 应保存 VARIANT 数据。这提供了所有世界中最好的,并且使得传递 VARIANT 的 VARIANT SAFEARRAY 对其他语言更有用。例如,C++ 到 VB / C#(请注意,由调用者决定释放/销毁 SAFEARRAY)
基于之前的代码构建
Passing SAFEARRAYs is not recommended. It is recommended to place the SAFEARRAY into a VARIANT. Further, the SAFEARRAY should hold VARIANT data. This gives the best of all worlds and makes passing VARIANT SAFEARRAY of VARIANTs more useful to other languages. E.g. C++ to VB / C# (Note it is up to the caller to free/destroy the SAFEARRAY)
Building on the previous code