#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
HRESULT SomeCOMFunction(BSTR *bstr)
{
*bstr = ::SysAllocString(L"你好,软件测试,lenmom");
return S_OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
BSTR unicodestr = 0;
char *ansistr=NULL;
SomeCOMFunction(&unicodestr);
int lenW = ::SysStringLen(unicodestr);
int lenA = ::WideCharToMultiByte(CP_ACP, 0, unicodestr, lenW, 0, 0, NULL, NULL);
if (lenA > 0)
{
ansistr = new char[lenA + 1]; // allocate a final null terminator as well
::WideCharToMultiByte(CP_ACP, 0, unicodestr, lenW, ansistr, lenA, NULL, NULL);
ansistr[lenA] = 0; // Set the null terminator yourself
FILE *fp;
if((fp = fopen("d:\\111.vbs", "wb+"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
else
{
fputs(ansistr, fp);
}
fclose(fp);
}
else
{
// handle the error
}
//...use the strings, then free their memory:
if(ansistr!=NULL)
delete[] ansistr;
::SysFreeString(unicodestr);
return 0;
}