将 C 类型字符串数组中的元素作为 BSTR 传递给 COM 对象? (在 C++ 中)
我正在编写一个由外部程序调用的 C++ DLL。
1.) 我将一个字符串数组(如 char *var)作为该程序的参数。
2.) 我想迭代这个数组并对字符串数组的每个元素调用 COM 函数。 COM 函数必须采用 BSTR:
DLL_EXPORT(void) runUnitModel(char *rateMaterialTypeNames) {
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
IUnitModelPtr pIUnit(__uuidof(BlastFurnaceUnitModel));
pIUnit->initialiseUnitModel();
int i;
for(i=0; i < sizeOfPortRatesArray; i++)
pIUnit->createPort(SysAllocString(BSTR((const char *)rateMaterialTypeNames[i])));
我认为 SysAllocString(BSTR((const char *)rateMaterialTypeNames[i])) 位给我带来了问题。 程序运行时出现访问冲突。
这是访问 i 处的rateMaterialTypeName 值的正确方法吗? 请注意,我期望像“IronOre”这样的内容作为 i 处的值,而不是单个字符。
I am writing a C++ DLL that is called by an external program.
1.) I take an array of strings (as char *var) as an argument from this program.
2.) I want to iterate through this array and call a COM function on each element of the string array. The COM function must take a BSTR:
DLL_EXPORT(void) runUnitModel(char *rateMaterialTypeNames) {
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
IUnitModelPtr pIUnit(__uuidof(BlastFurnaceUnitModel));
pIUnit->initialiseUnitModel();
int i;
for(i=0; i < sizeOfPortRatesArray; i++)
pIUnit->createPort(SysAllocString(BSTR((const char *)rateMaterialTypeNames[i])));
I think its the SysAllocString(BSTR((const char *)rateMaterialTypeNames[i])) bit that is giving me problems. I get an access violation when the programs runs.
Is this the right way to access the value of the rateMaterialTypeName at i? Note I am expecting something like "IronOre" as the value at i, not a single character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 Microsoft 的 ATL,则可以使用 CComBSTR 类。
它将接受 char* 并从中创建 BSTR,而且,您无需担心删除 BSTR,所有这些都发生在 CComBSTR 的 dtor 中。
另请参阅 Matthew Xaviers 回答,看起来您没有正确地将字符串数组传递到该函数中。
希望这可以帮助
If you're using Microsofts ATL, you can use the CComBSTR class.
It will accept a char* and create a BSTR from it, also, you don't need to worry about deleting the BSTR, all that happens in the dtor for CComBSTR.
Also, see Matthew Xaviers answer, it doesn't look like you're passing your array of strings into that function properly.
Hope this helps
因为保存 C 字符串的变量只是指向第一个元素(char*)的指针,所以为了传递 C 字符串数组,函数的参数应该是 char**:
这样,当您计算rateMaterialTypeNames 时[i],结果将是一个 char*,这是您需要传递给 SysAllocString() 的参数类型。
添加注释:正如 Tommy Hui 的回答所指出的那样,您还需要在某个时候将字符串转换为宽字符。
Because a variable holding a C string is just a pointer to the first element (a char*), in order to pass an array of C strings, the parameter to your function should be a char**:
This way, when you evaluate rateMaterialTypeNames[i], the result will be a char*, which is the parameter type you need to pass to SysAllocString().
Added note: you will also need to convert the strings to wide chars at some point, as Tommy Hui's answer points out.
如果函数rateMaterialTypeNames 的参数是字符串,则
rateMaterialTypeNames[i]
是字符而不是字符串。 您应该仅使用参数名称本身。
此外,演员阵容总体来说很糟糕。 转换为 BSTR 是一个很大的标志。 SysAllocString 的参数类型是
const OLECHAR*
,对于 32 位编译器来说,它是一个宽字符。 所以这肯定会失败,因为实际参数是 char*。
代码需要的是将窄字符串转换为宽字符串。
If the parameter to the function rateMaterialTypeNames is a string, then
rateMaterialTypeNames[i]
is a character and not a string. You should use just the parameter name itself.
In addition, casts in general are bad. The conversion to a BSTR is a big flag. The parameter type for SysAllocString is
const OLECHAR*
which for 32-bit compilers is a wide character. So this will definitely fail because the actual parameter is a char*.
What the code needs is a conversion of narrow string to a wide string.