Born's Windows Scripting Host

The DynaCall page

Thanks to Ton Plooy and Jeff Stong and their articles published in the August and November issues of the Windows Developers Journal (see the Archive part in www.wdj.com for the articles and the original code) the WSH script programmers has now the possibility to call Win 32 API functions. There is a routine dynacall.dll that provides something like a "Declare statement" in VBScript. Basically it will allow you to call functions in other dlls (like any of the win32 API function).

The history

Months ago I noticed a posting from Michael Hines about the Dynacall.dll in the WSH newsgroup. He mentioned also that the original code comes from a Windows Developers Journal article. But my (first) attempts to find these articles and the code wasn't successful. So I asked several people to send me the parts. But then I get confused about the different versions and so on. To let the WSH scripters participate in the Dynacall.dll I decided to create this page.

Because there was a bit confusion about the different versions of the Dynacall.dll, I like to get a brief overview (as far as I can estimate it from here). The original code published by Ton Plooy was written for Windows 9x (he published the code in an article in the August issue of the Windows Developer Journal www.wdj.com, describing the technique to call an external function without an Declare statement). Then Jeff Stong published an article (An Automation Object for Dynamic DLL Calls) with the code for Dynacall.dll in the November 1998 issue of the Windows Developer Journal.

William Epp extended the component and Michael Hines undertook the task to bring the whole distribution with a few comments and modifications to the WSH newsgroups. During writing the WSH book I experimented a little bit with the dll, and I found out that the original Dynacall.dll published from Jeff Stong was written only for Windows NT. So Michael Hines was so kind to create also a version for Windows 9x. Both ZIP-Archives are offered below for download.

Using the Dynacall.dll

Download the ZIP-Archive and unpack the archive into a separate folder. The archive contains the source code, a few samples Michael Hines and other provides and the already compiled Dynacall.dll. Take care to get the Win 9x or the Win NT version. The register the DLL using the program RegSvr32.dll with the following command:

RegSvr32.exe <path>dynacall.dll

where <path> is the path to your folder containing the dll. Unregistering the dll may be done with the /u switch for RegSvr32.exe (although I got an error message for the Win 9x version during this step).

After you have registered the dll, you can use an automation object to call your external functions.

' Create the wrapper object for dynamic DLL function calling

Dim UserWrap 

Set UserWrap = CreateObject("DynamicWrapper")

' GetProcAddress for GetPrivateProfileStringA()

UserWrap.Register "kernel32.DLL", "GetPrivateProfileString", "i=ssssls", "f=s", "r=l"

The input parameters are:

i=describes the number and data type of the functions parameters

f=type of call _stdcall or _cdecl. So it can work with both MS C++ and Borland C++. Default to _stdcall. If that doesn't work use _cdecl. If that doesn't work good luck!

r=return data type.

Data types are:

const ARGTYPEINFO ArgInfo[] = 

{

{'a', sizeof(IDispatch*), VT_DISPATCH}, // a IDispatch*

{'c', sizeof(unsigned char), VT_I4}, // c signed char 

{'d', sizeof(double), VT_R8}, // d 8 byte real 

{'f', sizeof(float), VT_R4}, // f 4 byte real 

{'k', sizeof(IUnknown*), VT_UNKNOWN}, // k IUnknown* 

{'h', sizeof(long), VT_I4}, // h HANDLE 

{'l', sizeof(long), VT_I4}, // l long 

{'p', sizeof(void*), VT_PTR}, // p pointer 

{'s', sizeof(BSTR), VT_LPSTR}, // s string 

{'t', sizeof(short), VT_I2}, // t short 

{'u', sizeof(UINT), VT_UINT}, // u unsigned int 

{'w', sizeof(BSTR), VT_LPWSTR}, // w wide string 

}

William Epp added anr 'r' for VT_BYREF (pass by reference) but is for strings only. This made the GETPROFILESTRING function to work. But it didn't work for the GETPROFILESECTION.

For Windows 9x there a some specialties. If you intend to call different API functions you need to declare for each function your own object variable. Also some parameters submitted to the API must be converted from Variants to the required data type using CString or equivalent functions.

Note: Due to the difficulty of all these issues, I prefer to use an ActiveX control to pass my WSH script calls to an external API function. These technique is described in my WSH book (www.wrox.com). But for some tests, and if you haven't an ActiveX control handy, the dynacall.dll comes handy. But keep in mind that all the code comes without any support. The original articles and code may be downloaded from the archive pages of the Windows Developers Journal (www.wdj.com). Because it is sometimes difficult to get the articles, because we have now modified versions of the code, because Ton Plony and Michael Hines doesn't run own homepages, I have decided to offer the least recent ZIP archives provided from Michael Hines for download. But keep in mind that I can't support the files nor I will be responsible for any consequences resulting from the use of the code.

Back

WSH book

There are two book projects which supports the scripters:

G. Born: Inside Windows Scripting Host, Microsoft Press Germany, ISBN 3-86063-495-X.

Unfortunately my attempts to release this title also in English failed till yet (most publishers still weight the WSH in a low priority or have ideas for whom the WSH might be, which I can't accept). I have already translated the manuscript already into English. So I hope we will have a chance to release the material soon, for the broader mass of Windows users.

Back


Do you feel these samples are useful? Comments, suggestions? Post me a note.


by Günter Born