Search code examples
c++commfcjscriptwsh

Can I have a COM interface in a service to be called from Windows Script Host?


My goal is to create a COM interface in my Windows local service, written with C++/MFC, whose methods could be called from a Windows Script Host JScript code running under a logged in interactive user account.

For instance, if I have a test.js that can be started with credentials of a logged in user, that does the following:

var Obj = new ActiveXObject("myservice.somename");
var Result = Obj.MyMethod("some data");

and then have MyMethod function processed in my service & return a value.

I know it's a general concept. At this stage I'm curious if such is possible (from a Windows security stand-point, i.e. calling system service from a user process) and if so, if there's any sample code that I can use as a basis for this?

I'm assuming that it must be some COM interface, right? But usually those go into a DLL. I've never tried to put them in a service.

Thank you!


Solution

  • I'm posting it for my own future reference, in despite of the treatment I got in the comments to my original post. It would've saved me a day of search if someone pointed me to this article...

    This CodeGuru article, "COM in plain C, Part 2" explains how to create a COM interface that can be called from the Windows Script Host. His IExample2 project shows how to create an in-proc DLL that hosts the COM interface that can be called from a VBScript included in the same project. Then regiexample2 and unregiexample2 projects also show how to register/unregister the COM interface. The VBScript can be easily adjusted to work with JScript in my OP.

    One word of caution though, that project is intended to be used for installation on a 32-bit OS. For 64-bit OS, you will need to build and register a 64-bit version of the in-proc COM Dll. The registration part from a 32-bit process is similar to the one shown, except that one needs to include the KEY_WOW64_64KEY flag when opening/creating registry keys.