BSTR是什么类型,在VC++.NET调用WEBSERVICE的方法时,string与BSTR如何转化

webjimcn 2004-10-15 04:40:44
webservice 用C#写成,client端用VC++.net写,在VC++.NET调用WEBSERVICE的方法时,string与BSTR如何转化,本来是string method(string,string),引用后,却变成了BSTR.string与BSTR如何转化
...全文
265 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2005-09-08
  • 打赏
  • 举报
回复
vc++ 2005 ???
蒋晟 2005-09-08
  • 打赏
  • 举报
回复
// MarshalBSTR1.cpp
// compile with: /clr
#define WINVER 0x0502
#define _AFXDLL
#include <afxwin.h>

#include <iostream>
using namespace std;

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

void NativeTakesAString(BSTR bstr)
{
printf_s("%S", bstr);
}

#pragma managed

int main()
{
String^ s = "test string";

IntPtr ip = Marshal::StringToBSTR(s);
BSTR bs = static_cast<BSTR>(ip.ToPointer());
pin_ptr<BSTR> b = &bs;

NativeTakesAString( bs );

Marshal::FreeBSTR(ip);

return 0;
}
// MarshalBSTR2.cpp
// compile with: /clr
#define WINVER 0x0502
#define _AFXDLL
#include <afxwin.h>

#include <iostream>
using namespace std;

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma managed

void ManagedTakesAString(BSTR bstr)
{
String^ s = Marshal::PtrToStringBSTR(static_cast<IntPtr>(bstr));
Console::WriteLine("(managed) convered BSTR to String: '{0}'", s);
}

#pragma unmanaged

void UnManagedFunc()
{
BSTR bs = SysAllocString(L"test string");
printf_s("(unmanaged) passing BSTR to managed func...\n");
ManagedTakesAString(bs);
}

#pragma managed

int main()
{
UnManagedFunc();
return 0;
}
蒋晟 2005-09-08
  • 打赏
  • 举报
回复
// MarshalBSTR1.cpp
// compile with: /clr
#define WINVER 0x0502
#define _AFXDLL
#include <afxwin.h>

#include <iostream>
using namespace std;

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

void NativeTakesAString(BSTR bstr)
{
printf_s("%S", bstr);
}

#pragma managed

int main()
{
String^ s = "test string";

IntPtr ip = Marshal::StringToBSTR(s);
BSTR bs = static_cast<BSTR>(ip.ToPointer());
pin_ptr<BSTR> b = &bs;

NativeTakesAString( bs );

Marshal::FreeBSTR(ip);

return 0;
}
// MarshalBSTR2.cpp
// compile with: /clr
#define WINVER 0x0502
#define _AFXDLL
#include <afxwin.h>

#include <iostream>
using namespace std;

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma managed

void ManagedTakesAString(BSTR bstr)
{
String^ s = Marshal::PtrToStringBSTR(static_cast<IntPtr>(bstr));
Console::WriteLine("(managed) convered BSTR to String: '{0}'", s);
}

#pragma unmanaged

void UnManagedFunc()
{
BSTR bs = SysAllocString(L"test string");
printf_s("(unmanaged) passing BSTR to managed func...\n");
ManagedTakesAString(bs);
}

#pragma managed

int main()
{
UnManagedFunc();
return 0;
}
Little_Ghost 2005-09-06
  • 打赏
  • 举报
回复
CSring str = "This is a string";
BSTR bstr = str.AllocSys...();//(具体记不清,应该是点出来的第一个函数)

CString str2 = (CString)bstr;//能够直接强制转换
Tomgus 2004-10-15
  • 打赏
  • 举报
回复
http://dev.csdn.net/article/35/35969.shtm
wangxt 2004-10-15
  • 打赏
  • 举报
回复
帮你顶
真相重于对错 2004-10-15
  • 打赏
  • 举报
回复
using CONVERTION;
BSTR bstr;
char* tstr = b2t(bstr);
bstr = T2B( tstr );

12,166

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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