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如何转化
...全文
213 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
帮你顶
回复
using CONVERTION;
BSTR bstr;
char* tstr = b2t(bstr);
bstr = T2B( tstr );
回复
发动态
发帖子
Web Services
创建于2007-09-28

1.2w+

社区成员

.NET技术 Web Services
申请成为版主
社区公告
暂无公告