#include "stdafx.h"
#import "c:\windows\system32\wshom.ocx" no_namespace
rename("FreeSpace","FSpace") rename("Unknown", "Unk")
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <atlbase.h>
int main(int argc, char* argv[])
{
USES_CONVERSION;
CoInitialize(NULL);
IWshShell2Ptr myWsh (__uuidof(IWshShell_Class));
BSTR bstrCommand;
VARIANT vtWindowStyle, vtWaitOnReturn;
VariantInit(&vtWindowStyle);
VariantInit(&vtWaitOnReturn);
vtWindowStyle.vt = VT_I2;
vtWindowStyle.iVal = 1;
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" );
// Convert from TCHAR to BSTR for COM
bstrCommand = SysAllocString(T2OLE(buffer));
HRESULT hr = myWsh->Run(bstrCommand, &vtWindowStyle, &vtWaitOnReturn);
SysFreeString(bstrCommand);
myWsh = NULL;
CoUninitialize();
return 1;
}
It compiles but the program crashes on the myWsh->Run statement. The script
file itself merely pops up a Hello World message and runs fine from windows
explorer.
Any help would be really appreciated.
Thanks
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:
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
in main()
"Paul" <pauln.o...@laberg.com.au> wrote in message
news:uvk2jxA4...@TK2MSFTNGP12.phx.gbl...
Confine the sope using braces, as in:
CoInitialize()
{
_bstr_t ...
}
CoUninitialize()
Brian
"Brian Muth" <bm...@mvps.org> wrote in message
news:u5oBUlB...@TK2MSFTNGP09.phx.gbl...