Example how to convert a BSTR to UTF-8 such that the string in XML would not be corrupted
- Author
- Evers
#include <Base/Common/XMLIO.hh>
#include <libxml/encoding.h>
#ifdef WIN32
#endif
using namespace BIAS;
using namespace std;
#ifdef WIN32
string ConvertBSTRToString(BSTR pSrc)
{
DWORD cb,cwch = ::SysStringLen(pSrc);
char *szOut = NULL;
string result;
cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch + 1, NULL, 0, 0, 0);
if(cb) {
szOut = new char[cb];
if(szOut) {
szOut[cb - 1] = '\0';
if(!::WideCharToMultiByte(CP_ACP, 0,pSrc, cwch + 1, szOut, cb, 0, 0))
delete []szOut;
else {
result = szOut;
delete[] szOut;
}
}
}
return result;
}
#endif
int main (int , char** )
{
string MyAnsiString,MyUTF8String;
#ifdef WIN32
BSTR MyBstr;
MyBstr = ::SysAllocStringLen(NULL,14);
MyBstr[0] = 85;
MyBstr[1] = 83;
MyBstr[2] = 66;
MyBstr[3] = 45;
MyBstr[4] = 86;
MyBstr[5] = 105;
MyBstr[6] = 100;
MyBstr[7] = 101;
MyBstr[8] = 111;
MyBstr[9] = 103;
MyBstr[10] = 101;
MyBstr[11] = 114;
MyBstr[12] = 228;
MyBstr[13] = 116;
MyAnsiString = ConvertBSTRToString(MyBstr);
#endif
xmlNodePtr rootNode;
xmlNodePtr currentNode2;
rootNode = myXML.
create(
"XMLTest");
string MyFilename = "Test-"+MyAnsiString+".xml";
if (myXML.
write(MyFilename) <0) exit(1);
return 0;
}