c ++用于VB6库的BSTR转换 [英] c++ BSTR conversion for VB6 library

查看:109
本文介绍了c ++用于VB6库的BSTR转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到的问题的一个例子。



C ++ Lib

Here is an example of a problem I am having.

C++ Lib

BSTR testlib::f4str(const long fPtr)
{
_variant_t retVal = dataRS->Fields->Item[_variant_t(fPtr)]->Value;
BSTR retStr = SysAllocStringLen(retVal.bstrVal,dataRS->Fields->Item[_variant_t(fPtr)]->DefinedSize);
    return retStr;
}





VB6函数



VB6 Function

Declare Function f4str$ Lib "testlib.dll" (ByVal fPtr&)





所以当我运行这个函数时,我会收回字符串,但每个字符都是字符串由SOH字符(标题的开头)分隔我将其视为无法呈现的小方框字符。我知道它是一个SOH,因为我把它放到一个为我呈现空白的编辑器中。



到目前为止我得到的结果是C ++中的BSTR是< b> 与VB6的BSTR不一样所以当我把它寄回去时我得到了。



应该注意的是,如果我只输入一个字符串输入并发回它工作正常..所以它只发生在我从_variant_t转换为BSTR。



提前感谢



So when i run this function i get back the string but every character of the string is seperated by a SOH character (start of header) i see it as a small box charcter that cannot be rendered. I know its an SOH because I put it into an editor that renders whitespace for me.

What I have sorta worked out so far is that the BSTR in C++ is not the same as the BSTR for VB6 so when I send it back I get that.

It should be noted that If I just type a string in and send it back it works fine.. so it only happens when I convert from _variant_t to BSTR.

thanks in advance

推荐答案

Libtestlib.dll(ByVal fPtr&)
Lib "testlib.dll" (ByVal fPtr&)





所以当我运行这个功能的时候我获取字符串,但字符串的每个字符都由SOH字符(标题的开头)分隔,我将其视为无法呈现的小方框字符。我知道它是一个SOH,因为我把它放到一个为我呈现空白的编辑器中。



到目前为止我得到的结果是C ++中的BSTR是< b> 与VB6的BSTR不一样所以当我把它寄回去时我得到了。



应该注意的是,如果我只输入一个字符串输入并发回它工作正常..所以它只发生在我从_variant_t转换为BSTR。



提前感谢



So when i run this function i get back the string but every character of the string is seperated by a SOH character (start of header) i see it as a small box charcter that cannot be rendered. I know its an SOH because I put it into an editor that renders whitespace for me.

What I have sorta worked out so far is that the BSTR in C++ is not the same as the BSTR for VB6 so when I send it back I get that.

It should be noted that If I just type a string in and send it back it works fine.. so it only happens when I convert from _variant_t to BSTR.

thanks in advance


将您的字符串接口更改为BSTR或char *类型,并始终避免将非平凡对象作为字符串(不是char的数组)进行交换。



VB 6是一个痛苦,但如果你坚持使用大代码库,我想知道为什么没有一些工作函数。



永远不要使用它们之间的对象C ++和VB6。这个功能只适用于VB.net:mad:



几乎是什么dataRS?
change your string interface to BSTR or char* type and always avoid to interchange non-trivial objects as strings (which arent arrays of char).

VB 6 is a pain, but if you are to stuck on it with the big code base, I wonder why there isnt some working functions for that.

Never ever use objects betweens C++ and VB6. This features only works with VB.net :mad:

What the heck is dataRS?


从一个字符串返回一个字符串dll到VB6。



在VB6中声明

To return a String from a dll to VB6.

Declare in VB6
Public Declare Function QueryString Lib "lihelper" Alias "_QueryString@4" (ByVal key As String) As String



在VB6中调用


Call in VB6

Dim s as String : s = QueryString("find me")





在C ++ DLL中定义



Define in C++ DLL

extern "C" __declspec(dllexport) BSTR __stdcall QueryString(LPCSTR key{
  const std::string response(key? odbc::sql::query(key) : std::string(""));
  return SysAllocStringByteLen(response.c_str(), response.size());
}



Vb6将始终从DLL转换返回的字符串,就好像它是多字节的,这就是为什么你的Unicode字符串只包含它们的第一个字符(除非它是> 0xFF当然)。

通常我会分配一个合理预定义大小的缓冲区,并查看返回的数据是否合适。返回所需大小的功能。如果需求更大,我会放弃并重新分配......


Vb6 will always convert a returning string from a DLL as if it's multibyte, that's why your Unicode-strings will only contain their first character (unless it's >0xFF of course).
Normally I allocates a buffer of reasonable predefined size and sees if the returning data fits. The function the returning the needed size. If the needs are greater, I'll give in and reallocates ...


这篇关于c ++用于VB6库的BSTR转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆