ActiveX和C#的类型转换问题

barrytam 2006-09-05 09:29:17
最近我写一个ActiveX控件,要被ASP.Net程序调用.但我发现将要传给ActiveX控件的字符数组转成String类型,再在ActiveX里(接口是CString类型)转换回字符数组.大小和数值都和原来不同了.请问高手这是为什么?还有什么好方法可以传递字符数组吗?
...全文
177 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2006-09-05
  • 打赏
  • 举报
回复
'CString' to 'BSTR':

Use the AllocSysString member function of the CString:


Code:

CString cs("Hello");
BSTR bstr = cs.AllocSysString();
If you pass the 'BSTR' to some OLE function, this will normally free the 'BSTR' memory when done with it.

If you use the 'BSTR' by yourself, dont forget to call '::SysFreeString()' when you're done with it.


Code:

::SysFreeString(bstr);
'BSTR' to 'CString':

You will mostly need this when you have some OLE function that returns a 'BSTR'. Such an OLE Function will basically do something like this:


Code:

HRESULT SomeOLEFunction(BSTR& bstr)
{
bstr = ::SysAllocString(L"Hello");
return S_OK;
}

Code:

#include <comutil.h>

BSTR bstr;
SomeOLEFunction(bstr);
CString cs(com_util::ConvertBSTRToString(bstr));
AfxMessageBox(cs, MB_OK, 0);
jixingzhong 2006-09-05
  • 打赏
  • 举报
回复
CString 操作指南

http://www.vckbase.com/document/viewdoc/?id=1094
jixingzhong 2006-09-05
  • 打赏
  • 举报
回复
CString 型转化成 BSTR 型

  当我们使用 ActiveX 控件编程时,经常需要用到将某个值表示成 BSTR 类型。BSTR 是一种记数字符串,Intel平台上的宽字符串(Unicode),并且 可以包含嵌入的 NULL 字符。

你可以调用 CString 对象的 AllocSysString 方法将 CString 转化成 BSTR:

CString s;
s = ... ; // whatever
BSTR b = s.AllocSysString();
  现在指针 b 指向的就是一个新分配的 BSTR 对象,该对象是 CString 的一个拷贝,包含终结 NULL字符。现在你可以将它传递给任何需要 BSTR 的接口。通常,BSTR 由接收它的组件来释放,如果你需要自己释放 BSTR 的话,可以这么做:

::SysFreeString(b);

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧