[ann] another way to call api's from script (the PB way)



I know what you're thinking. If microsoft wanted scripters to
be calling api's from script, then they would have built that
capability into the scripting engine. And besides, isn't there
already a DynaWrap utility for those who would dare to trespass
outside the boundaries of accepted scripting norms? True...

However, (for the sake of completeness?), while browsing in
the PowerBasic forum, I stumbled across a posting authored
by Charles Pegge and entitled: "How to turn a DLL into
a COM object", found here:

http://www.powerbasic.com/support/pbforums/showthread.php?s=69fab519eedc80eb6974c2c9ba8c881b&t=28421&highlight=comfordll

That sounded intriguing. And it was. What you get is some
powerbasic source code, a dll and a typelib. You register
the dll (and the typelib) with the provided bat file. Then
you run the script, which will interface with a "standard"
dll (also provided). It all works like "a charm" (any win98
users, see the postscript). The implication is that if
"COMforDLL" will work with a standard dll, then it will work
with any standard system dll as well.

What was intriguing to me was the notion that you could write
a com component in powerbasic, whereas the conventional wisdom
is that microsoft vb or c++ (and templates) are necessary.
The "basic" source code provided, (if you take the time to
read it), looks just like c++ code albeit written in basic.
In other words, it is more-or-less c++ code rendered into
the basic language. If nothing else, it is helpful to
understand if one eventually wished to go from vb to c++
in order to write some actX objects which don't render
very well into vb.

Here is the vbs test script provided by Pegge:

--- <code> ---
' VBscript .vbs, Author: Charles E V Pegge, 30 June 2006

set II = WScript.CreateObject("COMforDLL.Object","II_")

MyLibrary = II.LoadLibrary ( "ExampleHostDLL.dll" )

ReqReturnString = II.GetProcAddress ( MyLibrary, "TestFunctionReturnString" )

CallMeBack = II.GetProcAddress ( MyLibrary, "CallMeBack" )
'MsgBox CallMeBack, 0, "CallMeBack handle"

'some test data
a=1:b=2:c=3:d=4:e=5:f=6:g="seven":h=8

if ReqReturnString then
result = II.fun ( ReqReturnString, a,b,c,"four",e,f,g,h )
MsgBox result, 0, "Result"
end if

II.RequestCallback(3) 'a COmforDLL test function

if CallMeBack then
II.EnableLibraryCallbacks (MyLibrary)
II.fun CallMeBack, 2
II.DisableLibraryCallbacks (MyLibrary)
end if

FreeLibrary = II.FreeLibrary (MyLibrary)
Set II = Nothing


sub II_Callback1()
Wscript.echo "Callback 1 is called"
end sub

sub II_Callback2(v1,v2,v3,v4)
Wscript.echo "Callback 2 is called",v1,v2,v3,v4
end sub

sub II_Callback3(v1,v2,v3,v4)
Wscript.echo "Callback 3 is called",v1,v2,v3,v4
end sub
--- </code> ---

As you can tell from the code, the COMforDLL utility is
nothing more than a wrapper for the "LoadLibrary" api,
"GetProcAddress" api, some call-the-function code, and
then the "FreeLibrary" api, the basic mechanics of calling
a system api. DynaWrap does a better job of hiding the
guts of the api call, but then COMforDLL does get
the job done.

cheers, jw

p.s. Note for the win98 crowd. My first attempt to
register the COMforDLL dll and typelib with the win98
regsvr32.exe failed. How revolting! I took the package
over to a nearby winXP system and it worked perfectly.
And so, I thought that maybe I could get COMforDLL to
work with win98 if I manually copied the winXP registry
entries and took them back to win98. So I exported the
entries from winXP and loaded them into the win98
registry. VOILA! Now the COMforDLL works successfully
on win98 too. If there are any win98 aficionados left,
then the reg file I used is attached.
REGEDIT4
; COMforDLL registry entries, jw 02July08
; The "COMforDLL" utility comes with an actX dll plus a typelib,
; and a batch file to register these two entities. But alas,
; couldn't get that to work (i.e., register itself) on win98se.

; And so, found an accomodating winXP system, and VOILA! it did
; work over there. And so, exported the registry entries, and
; then attempted to load them into the win98se registry as follows:
; ------------------------------------------------

[HKEY_CLASSES_ROOT\COMforDLL.Object]
@="Intermediary between DLL host and COM client"

[HKEY_CLASSES_ROOT\COMforDLL.Object\CLSID]
@="{044A4BF7-837E-459A-A9AA-C961B8888E80}"

[HKEY_CLASSES_ROOT\CLSID\{044A4BF7-837E-459A-A9AA-C961B8888E80}]
@="Intermediary between DLL host and COM client"

[HKEY_CLASSES_ROOT\CLSID\{044A4BF7-837E-459A-A9AA-C961B8888E80}\InprocServer32]
@="C:\\Program Files\\COMforDLL\\COMforDLL.dll"
"ThreadingModel"="both"

[HKEY_CLASSES_ROOT\CLSID\{044A4BF7-837E-459A-A9AA-C961B8888E80}\ProgID]
@="COMforDLL.Object"


Relevant Pages

  • Re: Turning the Quick Launch Bar on using the Registry
    ... I need to do it as part of a script. ... >> I am trying to customise my default profile using registry hacks. ... >> whenever you turn it on or off it modifies the following key ... >> What does this DLL do? ...
    (microsoft.public.windowsxp.customize)
  • Re: Paths of all registered DLLs
    ... people who are not here anymore,so I need some script that will list all the ... DLL names and their paths that are registerd with regsvr32. ... > be used to enumerate registry keys, or you can always fall back ...
    (microsoft.public.win2000.general)
  • Re: Modify Registry on all SBS2003 clients
    ... registry setting to all workstations. ... command "REG ADD" to modify registry. ... the following command will change the registry value as your ... You may copy above command into the login script file SBS_LOGIN_SCRIPT.bat ...
    (microsoft.public.windows.server.sbs)
  • Re: Modify Registry on all SBS2003 clients
    ... logging on the client computer dont have administrative rights on the ... registry setting to all workstations. ... command "REG ADD" to modify registry. ... and put the command lines at the bottom of the script file. ...
    (microsoft.public.windows.server.sbs)
  • Re: Is there anyway to control the order of in which the [Run] entries are performed in the registry
    ... 3rd-party 'Startup Delayer' program or use a batch script, ... of these entries in the registry. ... Also, as I have also said, the 'Startup Delayer' ... run these commands from a 'Command Prompt' ...
    (microsoft.public.windowsxp.general)