neutralinojs / neutralinojs

Portable and lightweight cross-platform desktop application development framework
https://neutralino.js.org
Other
7.49k stars 378 forks source link

Unpopular request: Access to WSH and WMI using COM/AX on windows. #1337

Open squadjot opened 3 days ago

squadjot commented 3 days ago

Hiiiii ❤️

So, Windows Script Host : The scripting environment on windows that let's you do almost everything related to computer management. You can write vb and/or javascript files , and run them in WSH to get things done on a windows machine.

In cscript, wscript, mshta, ie you can do things like:

var objShell = new ActiveXObject("Shell.Application"); And get an instance of shell COM object and do stuff with it.

Could also be: new ActiveXObject("WIA.ImageProcess"); , and scan some drawings on your scanner (lol , maybe not very a realistic use case, but still.. )

Or get access to WMI

var locator = new ActiveXObject("WbemScripting.SWbemLocator");
// Connect to the WMI service
var service = locator.ConnectServer(".", "root\\cimv2");
// Execute the WMI query to retrieve DPI information
var colItems = service.ExecQuery("SELECT * FROM Win32_DesktopMonitor");

There's plenty of useful things to do with it, and it's not going away any time soon.. it's the backbone of windows management.

In a windows script (javascript/jscript), All this is implemented with one single function: new ActiveXObject... (ActiveX??? yuck..yea i know) - with that, you have access to a complete javascript interface / interop for controlling windows, just waiting to be utilized.

And! It's definitely technically possible to implement in a modern browser. Some argue that it only works in legacy IE browsers and WSH, but that's not true. I recently made it work in a WebView2 edge component ( c# Winform app ); I just made my own "ActiveXObject" method. and could use it, just like i was in a HTA or WSH. And it's full interop, not some json API.

In C# it was just a matter of doing:

[ClassInterface(ClassInterfaceType.AutoDual)] 
[ComVisible(true)]
...
webView.CoreWebView2.AddHostObjectToScript("external", interop); // interop
C# Interop having:

        public Object NewActiveXObject(String progId)
        {
            return (Activator.CreateInstance(Type.GetTypeFromProgID(progId)));
        }

And in the browser document you can get access to the object by doing something like:

var external = window.chrome.webview.hostObjects.sync.external;
var externalASync = window.chrome.webview.hostObjects.external;

function ActiveXObject(){
 return external.NewActiveXObject.apply(null,arguments)
}

window.ActiveXObject = ActiveXObject;
var fso = new ActiveXObject("Scripting.FileSystem");

I'm not all sure how this would be done in C , probably not as easy. But.. maybe not too hard? And if it's "easy" to implement, then why not?

squadjot commented 3 days ago

image

benjammin4dayz commented 2 days ago

Check out WelsonJS, it seems to fit the bill. https://github.com/gnh1201/welsonjs

Shell.Application https://github.com/gnh1201/welsonjs/blob/2ba05aa633dad23fc31508bca027466146f45366/lib/shell.js#L143-L152

WbemScripting.SWbemLocator https://github.com/gnh1201/welsonjs/blob/2ba05aa633dad23fc31508bca027466146f45366/lib/wmi.js#L15-L27

squadjot commented 2 days ago

WelsonJS uses mshta? ( IE11 at best ) I'm coming from using HTA's. I want to make my apps in a modern webbrowser with the v8 engine. Also.. WelsonJS seems like a bit like a frankensteins monster