How to load DLL file from Jscript file? - dll

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.

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

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 ;-)

To be able to use a dll as ActiveXObject, it needs to be registered as COM object. There are some restrictions on this but if you have a code for this dll, it is certainly doable.
When you register your dll as COM object, it is assigned a name. You use this name to create an object. This example from MSDN uses excel since it is already registered if you installed 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();
Apart from restriction of registering dll, using dll is no different from using it as c++ or c# dll. Note that, C# (or other .NET dlls) should be ComVisible to be used from javascript this way.
EDIT: The only other way of using C/C++ dll from javascript is swig interfaces. I have not used it, therefore I can only point you in that direction.
SWIG is a software development tool that connects programs written in
C and C++ with a variety of high-level programming languages. SWIG is
used with different types of target languages including common
scripting languages such as Javascript, Perl, PHP, Python, Tcl and
Ruby.

Related

How can I create a standard DLL in VB6?

TL:DR; How can I compile a VB6 module file into a standard DLL which I can use across multiple VB6 applications?
I am tasked with the support of multiple legacy applications written in VB6.
All of these applications make use of piece of hardware constructed by my employer. Before I came on to work for my employer, he had outsourced the work of developing a DLL for the project to a company that is no longer capable of supporting it since the individual working for THEM recently quit and no one else is capable of figuring it out.
My employer has recently upgraded our hardware, so even worse - the DLL that Company furnished us with is no longer useful either.
Further exacerbated by the fact that the company who released to us the NEW hardware did not release to us a DLL file which is capable of running in VB6.
It now falls to me to create a DLL file ( NOT a device driver ) which is capable of facilitating communications between the new ( and hopefully the old ) devices and VB6 applications.
My knowledge of VB6 is... limited, at best. I am mostly familiar with .Net and have had much success in creating DLLs in .Net, but when it comes to VB6, I know enough to get by. I'm entering into uncharted territory here.
I'm well acquainted with the HID.dll and the SetupAPI.dll P/Invokes and structs necessary to make this work, and I was even fortunate enough to stumble upon this, which had a working bit of VB6 code which facilitates read/writing to/from HIDs connected to the system. I tested this and ( with a bit of fidgeting ) it worked for our device out of the box. But that doesn't help me because I can't compile the module into a DLL file ( let alone figuring out events in VB6 and a truck load of other things, but I'm getting ahead of myself ).
I've read and tried a few different methods and while they proved promising, they didn't work.
Google has also inundated me with a lot of red herrings and been in general not very helpful.
If necessary, I would even write it in C/C++ ( though I'd rather not if there is some other way ).
So is what I am trying to do possible? Can someone direct me to some step-by-step for this sort of thing?
EDIT 1 :
To expound a bit, when I say that "they didn't work", what I mean is that in the case of the first link, the program still failed to find the function ( with an error message like "Function entry point not found" ) and in the second case I consistently and repeatedly received a memory write error when trying to call the function ( not fun ).
Here's a link to a way to do a standard DLL, that looks more straightforward than the links you've posted. I can say that if Mike Strong ("strongm") posts code, it works, too. You might want to have a look at it.
However, it's probably better to use COM if you're able: it's easier to set up (obviously) and it also has some standard capabilities for keeping track of the object's interface, that are built into VB6. For example, when you use the TypeOf keyword, VB6 actually makes an internal call to the object's QueryInterface method, which is guaranteed to exist as one of the rules of COM (and, if you use the keyword on a reference to a standard DLL object you'll get an error).
VB6 does "static" classes by setting the class's Instancing property to GlobalMultiUse. Warning: the "static" keyword has an entirely different meaning in VB6: static local variables' values persist between method calls.
1. After your trip to 1998 to get your copy of VB6, start a new ActiveX DLL project:
2. Edit Project Properties for the name of the beast.
3. Add a Class for the interface you are creating. I cleverly named the class VB6Class because the project/DLL is named VB6DLL.
4. Write code. I added some test methods to perform complex calculations:
Option Explicit
Public Function GetAString(ByVal index As Integer) As String
Dim ret As String
Select Case index
Case 0
ret = "Alpha"
Case 1
ret = "Beta"
Case Else
ret = "Omega"
End Select
GetAString = ret
End Function
Public Function DoubleMyInt(ByVal value As Integer) As Integer
DoubleMyInt = (2 * value)
End Function
Public Function DoubleMyLong(ByVal value As Long) As Long
DoubleMyLong = (2 * value)
End Function
5. Make DLL from File menu. You may need to be running As Admin so it can register the DLL.
6. In the project which uses it, add a reference to the DLL.
Test code:
Private Sub Command1_Click()
Dim vb6 As New VB6DLL.VB6Class
Dim var0 As String
Dim var1 As Integer
Dim var2 As Long
var0 = vb6.GetAString(0)
var1 = vb6.DoubleMyInt(2)
var2 = vb6.DoubleMyLong(1234)
Debug.Print "GetAString == " & var0
Debug.Print "DoubleMyInt == " & var1
Debug.Print "DoubleMyLng == " & var2
End Sub
Result:
GetAString == Alpha
DoubleMyInt == 4
DoubleMyLng == 2468
Not sure what to do about the "truck load of other things".

Create MetaTrader4 DLL with VB.Net

I'm trying to create a DLL for MetaTrader4 with VB.Net. Most of the examples are in C++, but I know it can be done in VB.Net. I know it is a bit more tricky than C++.
Can anyone point me in the right direction?
something about defining MT4_EXPFUNC __declspec(dllexport)
I finally figure out how to do it. Although this is not the ideal solution, it works. I'm documenting here for "future-generations".
Basically, I'm using a program called DLLExporter.Exe that converts a managed .Net class method into a direct unmanaged function. It at:
http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI
Sample of my VB.Net code:
Imports System.Runtime.InteropServices
Public Class myFXTrader
<DllExporter.DllExport()>
Public Shared Function storeFXRatesIntoSQL(
<MarshalAsAttribute(UnmanagedType.LPWStr)> pvsBroker As String _
, <MarshalAsAttribute(UnmanagedType.LPWStr)> pvsSymbol As String _
, <MarshalAsAttribute(UnmanagedType.LPWStr)> pvsTimeStamp As String _
, pviAsk As Double _
, pviBid As Double) As <MarshalAsAttribute(UnmanagedType.LPWStr)> String
Dim vsErrorCode As String = ""
'---------------------------------
Try
'... do some work here ...
Catch ex As Exception
End Try
'---------------------------------
Return vsErrorCode
End Function
End Class
After compilation of the above code, I just run DLLExporter myFXTrader.dll and it will do it's magic. The resulting .dll file can be copied to MT4's library folder and can be #import and used. See sample Expert Advisor code below:
#import "FXTrader.Exports.dll"
string storeFXRatesIntoSQL(string pvsBroker, string pvsSymbol, string pviTimeStamp, double pviAsk, double pviBid);
#import
int start()
{
Comment( storeFXRatesIntoSQL( AccountCompany(), Symbol(), TimeLocal(), Ask, Bid) );
int err;
err = GetLastError();
if(err>0) { Alert( err ); }
return(0);
}
Hope this will help whoever out-there who's attempting the same thing. I had to do quite a lot of reading/research to figure this out.
You will not find any official examples from metatrader. Their support is always less then helpful.
The api as you noted is indeed c++, meaning native. You are looking to write something in vb.net. .Net being managed code, whether your looking for vb or c# or even f# you will need a translation layer that will convert native api and data structures to manged.
Look into c++/cli. Cli is a framework that allows you mix native and managed code in the same assembly.
__declspec(dllexport) is an instruction that tells the linker which functions to export out of a native dll so that they are visible to the consumer of the dll and can be invoked in code. When you export a function like that it is still a native function.
I'm not sure if your trying to implement manager or server api. In case of a manager the entire api is in a class, the pointer to which is provided by the only function exported by the manager dll. In case of the server your are the one writing the dll as a plugin to the server so, in theory, you could export every function of your plugin but then the input and output types will still be native, in which case you will ether have to write converters or use pinvoke on the functions you exported. Here's an explanation of pinvoke http://www.codeproject.com/Articles/4965/Using-Platform-Invoke
In any case you still need to write native c++ code.
There is open source project, which gives you almost full access to mtmanapi: MetaTrader4.Manager.Wrapper.
It's written in C++/CLI, so it won't be a problem to use it from VB.NET

Using String variable as an expression

Passing commands as variables. I am creating a POP3 client and was crunching through some code when I though of something interesting.
Is it possible to pass strings of vb code to an object so that the object will execute it. I am relatively familiar with vb.net's source code being converted to Intermediate language and then being thrown into a JIT virtual machine, but I was hoping there was a simple way to implement this idea.
I want to be able to use strings
Dim Command as string
Command = "If a + b > 0 then c = a + b" '<----syntactical sugar!
System.Compiler.Something.execute(command)
If anyone has any direction, or any correction to any of the above. I appreciate your time.
Rah!
As a start you may want to check out the open source FileHelpers project.
The Runtime.ClassBuilder class in that project creates a .Net class from text, which can then be used as a normal class.

Pass data of arbitrary type between VBA and dll

So I am working on an Excel Project that is going to load a C++ dll using VBA. What I'd like to do is to be able to pass an Excel range with no specific type (data can be numerical or categorical) to the C++ dll (The best way I can describe my Excel range is of the type variant).
So the steps probably involve:
Load the dll in VBA
Send the excel range to dll (The range may contain columns of numbers and/or columns of strings)
manipulate the data from excel in the dll file
I am thinking of using excel variant and C++ variant. But it's not clear for me how to use the C++ variant as I couldn't find any good documentations on it.
Another suggestion I received was to ues COM programming.
My Questions:
Could a kind soul possibly provide pointers for me on how to proceed? (e.g. by providing the C++ prototype, and a simple example of how to handle the variant)
Does anyone know any good documentation/tutorial on using C++ Variants (and perhaps jointly with VBA)?
Is using COMs preferable to using VARIANTS if speed is an issue?
Is using the C API an option?
UPDATE:
The size of the ranges I need to manipulate can be large (~ 500,000 rows).
Speed is a factor, thus, I'd like to avoid unnecessary copying as much as possible.
Provided you only want to pass data to the dll (and not pointers to actual Excel objects such as Range), you have two fundamental options:
You have huge data sets and want to avoid copying as much as possible.
In this case you might want to pass that same Variant array you get by calling Range.Value. In order to do that, you will have to write a little TLB to reference from VB, in which you would describe your exported C++ function as expecting a SAFEARRAY(VARIANT)*. This is because the Declare operator will not let you actually pass a SAFEARRAY*.
The function will look like this:
LONG __stdcall ReturnArrLowerBound(SAFEARRAY** ppArr)
{
if (ppArr == NULL) return -1;
SAFEARRAY* pArr = (*ppArr);
LONG res = 0;
SafeArrayGetLBound(pArr, 1, &res);
return res;
}
And the TLB descripion will look like that:
[
uuid(A686B138-D8CE-462e-AEF2-75DA4DBF1C75)
]
library foo
{
[
dllname("TestSafearray.dll")
]
module vb
{
[entry("ReturnArrLowerBound")]
LONG __stdcall ReturnArrLowerBound(SAFEARRAY(VARIANT)* ppArr);
}
}
And your C++ project will obviously include a def file:
LIBRARY "TestSafearray"
EXPORTS
ReturnArrLowerBound
Your data sets are reasonably sized and you don't mind a little bit of copying.
Then make your C++ function to accept a mere int[] and Declare it in VB as accepting arr() as Long. On VB side, allocate an array on Longs and copy the elements into it from the Range.Value array.

Calling PythonFunction's from a VB application hosting Iron Python

I'm a C++ programming who was tapped to write a small application in Visual Basic. The application hosts an IronPython runtime and I am attempting to define some function in python and then call them from VB. I have written a simple test function in python
def test():
print "Test was Called"
Then I use the iron python to create a ScriptSource from the python file. I am able to look up the "test" variable through object operations but I can not figure out how to call the object that. For example (in VB):
pyScope = engine.CreateScope()
pySource = engine.CreateSourceFromFile("C:\Some\File\path\test.py")
pySource.Execute(pyScope)
' Now I expect the function test() to be defined in pyScope
Dim tmp as Object
pyScope.TryGetVariable("test", tmp)
At this point in my code tmp is defined as an object of type PythonFunction. I can't figure out how to call this function.
tmp()
Is not valid VB syntax. I've gotten this far, now how do I perform this seemingly simple task?
Edit: By calling
pyEngine.Operations.Invoke(tmp)
I am able to call the function and I see the expected output at stdout. I am still under the impression that there is some function-pointer-like type that I can cast objects of type PythonFunction to which will let me invoke temp directly without calling to the Python engine.
Not sure this will work, but try casting it to an Action type:
DirectCast(tmp, Action)()
Based on the comment, try this:
engine.ObjectOperations.Invoke(tmp, Nothing)
VB in .NET 4 should have the same dynamic support as C#. According to http://msdn.microsoft.com/en-us/library/ee461504.aspx#Y5108 (near the bottom), you should be able to do:
Dim tmp As Object = scope.GetVariable("test")
... which is what you're already doing, so make sure you're targeting .NET 4.
If that doesn't work you should be able to cast it with the generic version of GetVariable:
Dim tmp As Action = scope.GetVariable(Of Action)("test")
Finally, you already discovered Invoke on ObjectOperations.
(You may need to tweak the syntax, since I don't know VB.)