lenmom

博客园 首页 新随笔 联系 订阅 管理

#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;
}

posted on 2017-11-17 14:51  老董  阅读(299)  评论(0编辑  收藏  举报