How to load a DLL file from a Jscript file?

So, I am writing a stand-alone JScript file that will be run by Windows Script Host (this file will not be used as a web application).

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

I tried to research the subject, but I did not come up with anything useful. I’ve lost so much that I don’t have a piece of code. I understand that using ActiveXObject may come to my aid. if so, any idea how to use it?

Update:

If we all agree that downloading is not possible, I will agree to a reality check. Meaning, do not try to download, but check if it is loaded and if it works.

+6
source share
3 answers

You can export a specific function for this purpose. Then, from your JScript, run rundll32.exe and verify that the function works as expected.

+1
source

You can also give Gilles Laurent DynaWrap ocx a chance.

This type of dll must be registered on the target system, for example regsvr32 /s DynaWrap.dll .

It is limited to 32-bit DLLs, and it may not be convenient for you to use, but it works on 64-bit Windows. You cannot access a function exported by serial number, and you cannot directly process 64 bits or more values ​​/ pointers.

Here is a sample for calling 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 the first parameter containing the string object, the full path to the DLL, the second parameter for the exported name of the function used and the following three parameters that describe the declaration of the functions in some way hidden syntax.

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

f= describes the type of call: _stdcall or _cdecl . The default is _stdcall .

r= describes the data type of the return value.

Supported data types:

 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 call to the Register method used in the examples describes MessageBoxA as follows:

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

For an explanation of MessageBoxA, see the docs on MSDN .

Read the DynaWrap docs for more complex examples ... But you may need to translate Google because they are written in French; -)

+1
source

To be able to use a DLL as an ActiveXObject, it must be registered as a COM object . There are some limitations to this, but if you have code for this DLL, this is certainly doable.

When you register a DLL as a COM object, it is given a name. This name is used to create the object. This example from MSDN uses excel as it is already registered if you have set up an office.

 var ExcelApp = new ActiveXObject("Excel.Application"); var ExcelSheet = new ActiveXObject("Excel.Sheet"); // Make Excel visible through the Application object. ExcelSheet.Application.Visible = true; // Place some text in the first cell of the sheet. ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1"; // Save the sheet. ExcelSheet.SaveAs("C:\\TEST.XLS"); // Close Excel with the Quit method on the Application object. ExcelSheet.Application.Quit(); 

Besides limiting the registration of dlls, using dlls is no different from using them as C ++ or C # dll. Note that C # (or other .NET DLLs) must be ComVisible , which will be used from javascript in this way.

EDIT: The only way to use the C / C ++ dll from javascript is swig . I have not used it, so I can only point in this direction.

SWIG is a software development tool that connects programs written in C and C ++ with various high-level programming languages. SWIG - This is used with various types of target languages, including common scripting languages ​​such as Javascript, Perl, PHP, Python, Tcl, and Ruby.

0
source

Source: https://habr.com/ru/post/978895/


All Articles