Wednesday 28 October 2009

Using WScript.Shell from VC++ Part - I

The WScript.Shell component provides some much sought after functionality. It provides some easy to use APIs for doing things which often require convoluted code in plain VC++. For example creating a windows shortcut is a simple matter of calling IWshShell::CreateShortcut()! Can it get any easier? (please feel free to let me know if it does :) )

But naively trying to import the WScript.Shell library into your project will expose you to a world of hurt. Here's a small preview...

wshtest.cpp(11) : warning C4278: 'ExpandEnvironmentStrings': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
wshtest.cpp(11) : warning C4278: 'AddPrinterConnection': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
wshtest.cpp(11) : warning C4278: 'SetDefaultPrinter': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
wshtest.cpp(11) : warning C4278: 'DeleteFile': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
wshtest.cpp(11) : warning C4278: 'MoveFile': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
wshtest.cpp(11) : warning C4278: 'CopyFile': identifier in type library 'Wscript.Shell' is already a macro; use the 'rename' qualifier
debug\wshom.tlh(1159) : warning C4003: not enough actual parameters for macro 'GetFreeSpace'
debug\wshom.tlh(1159) : error C2059: syntax error : 'constant'
debug\wshom.tlh(1159) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
debug\wshom.tlh(1159) : warning C4183: '_variant_t': missing return type; assumed to be a member function returning 'int'
debug\wshom.tlh(1160) : error C2146: syntax error : missing ';' before identifier 'GetTotalSize'
debug\wshom.tlh(1160) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


Ok now getting to what you may be most interested in, here's the code which manages to import the WScript.Shell library and still compile...


// WSHTest.cpp : Defines the entry point for the console application.
//

#import "progid:Wscript.Shell" no_namespace \
rename("FreeSpace", "WshFreeSpace") \
rename("ExpandEnvironmentStrings", "WshExpandEnvironmentStrings") \
rename("AddPrinterConnection", "WshAddPrinterConnection") \
rename("SetDefaultPrinter", "WshSetDefaultPrinter") \
rename("DeleteFile", "WshDeleteFile") \
rename("MoveFile", "WshMoveFile") \
rename("CopyFile", "WshCopyFile") 


int main()
{
    return 0;
}

In the next part we shall actually see how to do some interesting things with our new found powers!

ASIDE: The WScript documentation seems to be intended for script users who generally do not have to care for things like interfaces, co-classes etc (which is a good thing sometimes). But it's difficult to work with WScript in C++ if you do not know the interface, co-class names etc. But there is an easy way out of this quandary just compile the above source code and look in the output directory for a file called wshom.tlh this file contains the declarations of all the interfaces, co-classes etc within the component)

No comments:

Post a Comment