Re: Newbie: TCHAR to BSTR conversion
- From: mfurnari@xxxxxxxxx
- Date: 2 Nov 2005 15:41:51 -0800
The easiest way to convert between the two is to use a _bstr_t:
TCHAR buffer[MAX_PATH];
....
_bstr_t bstrResult = _bstr_t(buffer);
I couldn't import wshom.ocx, but this code below accomplishes the task
you're trying to do. You can get the MSScript control here:
http://www.microsoft.com/downloads/details.aspx?FamilyId=D7E31492-2595-49E6-8C02-1426FEC693AC&displaylang=en
Source:
#include "StdAfx.h"
#import "C:\Program Files\Microsoft Windows Script\Windows Script
Control\msscript.ocx" rename_namespace("VBS")
#include <tchar.h>
#include <atlbase.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
string getFile(const string &filename)
{
string strContents;
ifstream file(filename.c_str(), ios::in);
while (file && file.peek() != char_traits<char>::eof())
strContents.append(1, file.get());
file.close();
return strContents;
}
string itos(int i, unsigned int base = 10)
{
char buffer[20];
string strResult = "";
strResult = _itoa(i, buffer, base);
return strResult;
}
string bstos(_bstr_t bstrSource)
//Convert a bstr to a string
{
string Result = "";
if ((BSTR) bstrSource != NULL)
Result = bstrSource;
return Result;
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);
VBS::IScriptControlPtr pScript(__uuidof(VBS::ScriptControl));
try
{
pScript->Language = "VBScript";
TCHAR buffer[MAX_PATH];
::GetModuleFileName(NULL,buffer,sizeof(buffer));
if (buffer!=NULL)
printf ("Current path is: %s",buffer);
// Strip off the EXE's file name...
LPTSTR pEnd = _tcsrchr( buffer, L'\\' );
//...and replace it with the name of the file
lstrcpy( pEnd + 1, "test.vbs" );
string strContents = getFile(buffer);
pScript->ExecuteStatement(strContents.c_str());
}
catch (...)
{
VBS::IScriptErrorPtr pError = pScript->Error;
string strError;
strError += "Description: " + bstos(pError->Description) + "\n";
strError += "Text: " + bstos(pError->Text) + "\n";
strError += "Source: " + bstos(pError->Source) + "\n";
strError += "Number: " + itos(pError->Number) + "\n";
strError += "Line: " + itos(pError->Line) + "\n";
strError += "Column: " + itos(pError->Column) + "\n";
cout << strError;
}
CoUninitialize();
return 1;
}
Best of luck,
Matt Furnari
.
- Follow-Ups:
- Re: Newbie: TCHAR to BSTR conversion
- From: Paul
- Re: Newbie: TCHAR to BSTR conversion
- Prev by Date: Re: Casting int to pointer in x64
- Next by Date: Re: Newbie: TCHAR to BSTR conversion
- Previous by thread: Casting int to pointer in x64
- Next by thread: Re: Newbie: TCHAR to BSTR conversion
- Index(es):
Relevant Pages
|