Link to home
Start Free TrialLog in
Avatar of leobaz2
leobaz2

asked on

Calling a dll from javascript

Hello all, what I am trying to do here is to call a dll from javascript.  The javascript will send 2 parameters (both strings) to a function in the dll.  The dll will return a string array to the javascript where the javascript will then output the results.

The dll was created using MSVC++ 6.0.  The project type of this dll is Win32 Dynamic-Link Lirary.  It is NOT an ATL COM AppWizard.

Can someone tell me the steps I need to take in order to be able to do this?  Do I need to register the dll, set the PATH environment variable, etc.  Any help would be great since I am having troubles finding example on the web.

- Leobaz2
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Are you talking about server-side JScript?

Fritz the Blank
Avatar of leobaz2
leobaz2

ASKER

No, this is all on the client side.  What I want is for the client to take a file and extract data from it.  Such data would be the last saved time, author, title, number of words, number of lines, etc.  This extraction is all done in dlls.  

The client would use javascript to call a dll.  The javascript would send to the dll the absolute path to the filename.  The dll would extract metadata about the file specified by the path.  This data will be return to the javascript by a string array.
So, are you saying then that the .dll would be on the client machine?

If not, then there is no way the client-side script can call a .dll on your server. On the other hand, how do you know that the .dll exists on the client machine? Even if it did, the security model would not allow that kind of behaviour without the use of ActiveX, which most people have disabled because of security concerns.

Fritz the Blank
Avatar of leobaz2

ASKER

The dlls would be on the client machine.  The client would recieve these dll files from the installation.

But say that ActiveX is enabled.  Would I have to make a dll using the ATL COM AppWizard in MSVC++, or can I just use a win32 dynamic link library?
Avatar of leobaz2

ASKER

The dlls would be on the client machine.  The client would recieve these dll files from the installation.

But say that ActiveX is enabled.  Would I have to make a dll using the ATL COM AppWizard in MSVC++, or can I just use a win32 dynamic link library?
I am not sure about that. I have never used this model as none of my clients have ever allowed it.

Fritz the Blank
Avatar of leobaz2

ASKER

Is there another way I can do this then?  My original though was to create a broswer plugin.  I read somewhere on the web that the easiest way to make a browser plugin for IE would be to use activeX.  Is there another way I can create a browser plugin with IE?
You need to create a new com dll component. add the
rutines you wish to call the other dll to this component and the create wrapper functions in the com component.

Compile the dll and sign it. When you install the com wrapper by copying it to hdd and registering it. you will be able to create an instance of it in explorer or any other com compatible application.  Simply call the wrapper rutines from your javascript. (use the clsid of the wrapper)

"If you signe and install a dll component on a client machine you should not be queryed to its safty"
I created a zlib com wrapper for exactly the same reasons and have used it succsessfully to compress and deompress data even from a web page withought trigering ms security warnings.

You only need to crerate an activex dll wich in vb terms i belive translated to com atl in c/c++
oh did i forget to mention the rundll.exe on windows wich will alow you to call procedures. use wsh etc to get at the command line

Doing so will still not do exactly what you want unless you wrap it in com as their is not other way to call dlls from web browsers without some extension.
Avatar of leobaz2

ASKER

OK, thanks for the advice gmsolutions, but I am now getting another error.  I have posted this error in another question.  Can you please help me on that one too?

https://www.experts-exchange.com/questions/20566857/Automation-server-can't-create-object.html

In summary, this question is about an error I am getting in my javascript.  When I try to create the activeX object, I get an "automation server can't create object" error on this line:

x=new ActiveXObject("WebPlugin.dll");
It sounds like it may be a permissions error...

Fritz the Blank
Avatar of leobaz2

ASKER

I have changed all of the permissions for all of my zones to the lowest possible.  It still doesn't work.  Any other ideas?
Avatar of leobaz2

ASKER

I found my problem.  I was using WebPlugin.dll which isn't the progID.  The progID was WebPlugin.Plugins.  Thanks anyway.

I will not award any points however since no one figured out the problem.  Im sorry.  However, I have yet another problem.

Using the activeXobject I am creating using javascript, I am calling a function in the dll.  This function is just a test function, its only purpose is to add two number which are passed in.

I am passing a results variable along with the 2 number to add in the parameters.  The c++ will get a pointer to that variable and then store the result in the parameter pointer.  When I print out this result in the javascript, it says undefined.  Is there something I am doing wrong?  Is there a certain way to return values from COM to Javascript?

Here is the C++ code:

// Plugins.cpp : Implementation of CPlugins
#include "stdafx.h"
#include "WebPlugin.h"
#include "Plugins.h"

/////////////////////////////////////////////////////////////////////////////
// CPlugins


STDMETHODIMP CPlugins::AddNumbers(long Num1, long Num2, long* ReturnVal)
{
     // TODO: Add your implementation code here
     *ReturnVal = Num1 + Num2;

     return S_OK;
}


Here is the javascript code:

var x = new ActiveXObject("WebPlugin.Plugins");
var result;
x.AddNumbers(4,8,result);
pluginForm.metadata.value = "result = " + result + "\n";

The output in the pluginForm.metadata textbox is:
result = undefined

NOTE:
When declaring result, if I do var result = 5 instead of just var result, I get the value of result when I output it as 5.  So it looks like there is nothing happening to result when I pass to the function.

Any help would be great.
Thanks.
Unsubscribing....

Fritz the Blank
Ahh good yes you call your objects by "component.class" or "{cls-id-xxx-xxx}" figuring things out like that on your own ensures you dont continue make the same mistake.

As to your other question. My experiance in windows c++ programming is very limited so i cannot answer your question here. The only way i could help here is to write a simple wrapper for all your functions in visual basic 6 and post you back the source code and possibly a binnary.

In my opinion you arent geting/seting the referance to result.

give it a value and then call AddNumbers to see if it still returns undef.

i am curiose if *ReturnVal = Num1 + Num2; actually changes the pointer and not the values.


if your intrested i can code up a wrapper for u in vb6 but sorry i cant help you further in c++



 










Avatar of leobaz2

ASKER

Im sorry but I have to do this in C++.  Can you provide me with a little VB that will do the same thing?  Thank would be helpful.  Also can you write the javascript also if it is different from mine?  Thanks
Avatar of leobaz2

ASKER

I figured out the problem.  In my IDL file, I declared the last parameter, which is my return value, to be [out] when it should have been [out, retval].

Thanks for everyones help.
hehe by the time i read both your last posts i had allready written a vb com wrapper. all you have to do is add the declares and wrapp them in the class.

it functions as a service and creating any instance of a plugin class from the web page will also create a referance to it in a collection held in plugins.

This class can be created by an application or webpage and will allow them to call the wrapper functions independantly as well as comununicate with each other including between multiple web pages in seperate windows.
It does this by including a third class wich sinks its events in the plugins class. This allows the plugin class to manage a collection of plugin classes.

Only one function to enumerate plugin classes created and to one calculate 1 addition is provided.
Also an example wrapper for getticks api and sleep.
id presume you know how to pass variables etc to dll's thru vb

if you are intrested ill post it to u.
Avatar of leobaz2

ASKER

Thanks for taking the time and writing the VB code.  Because I'm such a nice guy, I will give you the points just for helping me out.  I just have one more question.

My method in the COM dll has to return an array or strings.  It doesnt have to be strings, it could be char*, BSTR, whatever it takes to send an array of text back to the javascript.  I am trying to mess around with SAFEARRAY in the COM and creating an array in JavaScript using VBArray(array), but this isn't working.  I think my problem is in the COM.  For some reason, the array isn't being returned.  I got the code for creating the SAFEARRAY from another question.  Here is my COM code:

STDMETHODIMP CPlugins::ReturnArray(VARIANT *Array)
{
     // TODO: Add your implementation code here    
     VariantInit (Array);
     Array->vt = VT_ARRAY | VT_I4; // Array of integers

     SAFEARRAY* psa;
     // 10 elements numbered 0-9
     SAFEARRAYBOUND bound = { 10, 0 };
     psa = SafeArrayCreate (VT_I4, 1, &bound);

     if (psa == NULL)
         return E_OUTOFMEMORY;

     for (long i=0; i<10; i++)
         SafeArrayPutElement (psa, &i, &i);

     Array->parray = psa;

     return S_OK;
}

Do you see anything wrong?  Thanks
ASKER CERTIFIED SOLUTION
Avatar of gmsolutions
gmsolutions

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leobaz2

ASKER

Thanks gmsolutions.