Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load DLL file from Jscript file?

Tags:

dll

jscript

So I'm writing a standalone JScript file to be executed by Windows Script Host (this file is not going to be used as a web application).

My goal is to load a dll file. Just like using LoadLibrary function in a C++ application.

I tried researching the subject but I didn't come up with anything useful. I'm so lost I don't have any piece of code to share. I understand using ActiveXObject may come to my rescue. if so, any idea how to use it?

Update:

If we all agree that loading is impossible, I'll settle for validity check. Meaning, don't try to load but check if it is loaded and functional.

like image 242
idanshmu Avatar asked Nov 30 '14 13:11

idanshmu


2 Answers

You can export a specific function for this purpose. Then, from your JScript, execute rundll32.exe and check that the function ran as expected.

like image 121
GoodLife Avatar answered Sep 29 '22 01:09

GoodLife


You might also give Gilles Laurent's DynaWrap ocx a chance.

This kind of dll needs to be registered on the target system like regsvr32 /s DynaWrap.dll.

It is restricted to 32-bit DLLs, and this might be inconvenient for you to use, but it works on a 64bit Windows. You can't access function exported by ordinal number and you can't directly handle 64bit or greater values/pointers.

Here's a sample to call MessageBoxA from JScript:

var oDynaWrap = new ActiveXObject( "DynamicWrapper" )

// to call MessageBoxA(), first register the API function
oDynaWrap.Register( "USER32.DLL", "MessageBoxA", "I=HsSu", "f=s", "R=l" )

// now call the function
oDynaWrap.MessageBoxA( null, "MessageBoxA()", "A messagebox from JScript...", 3 )

And here from VBScript:

Option Explicit
Dim oDynaWrap
Set oDynaWrap = CreateObject( "DynamicWrapper" )

' to call MessageBoxA(), first register the API function
UserWrap.Register "USER32.DLL", "MessageBoxA", "I=HsSu", "f=s", "R=l"

' now call the function
UserWrap.MessageBoxA Null, "MessageBoxA()", "A messagebox from VBScript...", 3

To use a function you need to "register" the exported function of your DLL. To do this you need to call the register method with a first parameter containing a string object to the complete path of the DLL, a second parameter for the exported name of the function to use, and following three paremeters describing the functions declartion in a somehow obscure syntax.

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

f= describes the type of call: _stdcall or _cdecl. Default to _stdcall.

r= describes the return values data type.

The supported data types are:

Code  Variant      Description

a     VT_DISPATCH  IDispatch*
b     VT_BOOL      BOOL
c     VT_I4        unsigned char
d     VT_R8        8 byte real
f     VT_R4        4 byte real
h     VT_I4        HANDLE
k     VT_UNKNOWN   IUnknown*
l     VT_I4        LONG
p     VT_PTR       pointer
r     VT_LPSTR     string by reference
s     VT_LPSTR     string
t     VT_I2        SHORT
u     VT_UINT      UINT
w     VT_LPWSTR    wide string

Thus the Register method call used in the examples describes MessageBoxA like this:

_stdcall LONG MessageBoxA( HANDLE, LPSTR, LPSTR, UINT );

For a explanation of MessageBoxA look at the docs on MSDN.

Please read the DynaWrap docs for more sophisticated examples... But you might need Google translate, 'cos they are written in french ;-)

like image 41
rogepa Avatar answered Sep 28 '22 23:09

rogepa