如何将字符串数组从 C/C++ dll 传递到 vba (Excel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/987010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:29:06  来源:igfitidea点击:

how to pass string array from C/C++ dll to vba (Excel)

arraysstringexcelvbadll

提问by Andy

how to pass string array from C/C++ dll to vba (Excel)

如何将字符串数组从 C/C++ dll 传递到 vba (Excel)

dll in Visual Studio

Visual Studio 中的 dll

dll is not managed, ATL, etc.

dll 未托管,ATL 等。

regards Andy

问候安迪

采纳答案by Suvesh Pratapa

One of the ways to handle this is to write your DLL to return a SAFEARRAY of type Byte as the function result (VB arrays are OLE SafeArrays).

处理此问题的方法之一是编写 DLL 以返回 Byte 类型的 SAFEARRAY 作为函数结果(VB 数组是 OLE SafeArrays)。

To do this you have to read up on the SafeArray APIs and structures. I don't know of it myself, but the main things you'll need are the SAFEARRAYBOUND structure and the SafeArrayCreate API. What the API returns to you, you return to VBA. And you'll be done.

为此,您必须阅读 SafeArray API 和结构。我自己也不知道,但您需要的主要内容是 SAFEARRAYBOUND 结构和 SafeArrayCreate API。API 返回给您的内容,您返回给 VBA。你会完成的。

回答by Karl E. Peterson

Microsoft wrote a whitepaper many years ago detailing some best practices for writing DLLs to use with VB5/VB6, which of course would also apply to VBA. It is reprinted with permission, and available in the original Word form, here:

微软多年前写了一份白皮书,详细介绍了编写 DLL 以与 VB5/VB6 一起使用的一些最佳实践,这些实践当然也适用于 VBA。经许可转载,并以原始 Word 形式提供,请点击此处:

Microsoft: Developing DLLs for VB5 http://vb.mvps.org/tips/vb5dll.asp

微软:为 VB5 开发 DLL http://vb.mvps.org/tips/vb5dll.asp

See section 8 ("Passing and Returning Arrays") for help with this task. There is an example that is written specifically to demonstrate passing and returning arrays of strings.

有关此任务的帮助,请参阅第 8 节(“传递和返回数组”)。有一个示例专门用于演示传递和返回字符串数组。