durs / node-activex

Node.JS Implementaion of ActiveXObject
MIT License
329 stars 62 forks source link

How to attach to an existing IDispatch pointer #122

Closed somanuell closed 1 year ago

somanuell commented 1 year ago

Hi durs, I have a situation where an Excel is not correctly saved in the ROT, but I think I can retrieve an IDispatch* by other means (using an MSAA technique). Could I then somehow "inject" the IDispatch pointer into winax?

somanuell commented 1 year ago

Ok, fund code in disp.cpp line 547:

    // Use supplied dispatch pointer
    else if (args[0]->IsUint8Array()) {
        size_t len = node::Buffer::Length(args[0]);
        void *data = node::Buffer::Data(args[0]);
        IDispatch *p = (len == sizeof(INT_PTR)) ? (IDispatch *) *(static_cast<INT_PTR*>(data)) : nullptr;
        if (!p) {
            isolate->ThrowException(InvalidArgumentsError(isolate));
            return;
        }
        disp.Attach(p);
        hrcode = S_OK;
    }

Issue now is "HOW TO" to pass the pointer from JavaScript ???

durs commented 1 year ago

Hi, found this example:

// Create COM object from JS
var obj = new winax.Object({
    test: function(v) { return v*2; }
});

// Convert COM IDispatch pointer to Variant array
var varr = new winax.Variant(obj, 'uint8[]');

// Convert Variant array to JS array address bytes
var arr = new Uint8Array(varr.valueOf());
console.log(arr);

// Create new COM object from IDispatch pointer (address bytes)
var obj2 = new winax.Object(arr);

// Call test method
console.log(obj2.test(2));
somanuell commented 1 year ago

@durs Hi. Thanks for merging the PR. Do you plan to release on npmjs.com?

durs commented 1 year ago

Ok, I'll make a release, while I don't plan to change anything else

somanuell commented 1 year ago

Hi, again. When I built winax with npm i against node 14.18.3 in january 2022, I got a node_activex.node of approx 185 kB. Today when I npm i winax against node 14.20.0 I get a node_activex.node of 560 kB! What am I missing here?

durs commented 1 year ago

I don't know, last release in my environment 240 Kb with Node 18.13 and Visual Studio 2022 (v143)

somanuell commented 1 year ago

What is your build method?

durs commented 1 year ago

npm install then tried rebuild binding.sln but got the same result

somanuell commented 1 year ago

Same here! npm install ==> node_activex.node is 544 KB rebuild binding.sln ==> node_activex.node is 544 KB.... node --version ==> v14.20.0 Visual Studio 2022 (v143) I AM LOST!

somanuell commented 1 year ago

LOL with nodejs v18.15.0 32 bits I am now at 194 KB! Something very weird is happening with the LIBs used while linking against nodejs v14.20....