node-activex icon indicating copy to clipboard operation
node-activex copied to clipboard

js Callback Function causes Access Violation

Open jobe3774 opened this issue 6 years ago • 4 comments

Hi durs,

first of all, thanks for implementing 'winax'.

I currently use it to migrate software from IE to electron. Unfortunately I experience access violations when setting a javascript function as a callback for a C# object.

Example:

C# Site:

public interface IMyInterface { [DispId(0x1234567)] object OnChange { set; } }

JS Site:

function SetCallbackToInterface(callback) { myInterface.OnChange = function (eventName, eventData, eventTime) { if (eventName === "specialEvent") callback(eventName, eventData, eventTime); else __default(eventName, eventData, eventTime); }; }

Setting myInterface.OnChange to the Function above works, but when it is invoked from C# site it causes an access violation.

You already have a VarArguments implementation with *VarArguments(Isolate isolate, const FunctionCallbackInfo<Value> &args) but I am not sure how to use it correctly. When bool DispObject::set is called for the example above it should figure out if value is a function and then use the constructor using FunctionCallbackInfo. Could you help me please.

jobe3774 avatar Aug 17 '18 14:08 jobe3774

Hi durs,

I think the problem is, that Isolate::GetCurrent() returns NULL in DispObjectImpl::Invoke. Then the call to obj.Get(isolate) causes the Access Violation. Is it possible that GetCurrent() returns NULL because it's another thread?

Best regards, jobe

jobe3774 avatar Aug 20 '18 12:08 jobe3774

node-activex does not seem to work in an multithreaded environment.

jobe3774 avatar Aug 21 '18 08:08 jobe3774

yes, what is multithreaded environment in nodejs, for example?

durs avatar Aug 22 '18 07:08 durs

I use node-activex in an electron project. I modified DllMain of your module to call CoInitializeEx instead of CoInitialize to make COM use a multithreaded apartment. Each object of IMyInterface (see example above) spawns a worker thread and from within this worker thread it calls a callback set by OnChange (see above) and unfortunately this leads to a crash because Isolate::GetCurrent returns NULL. I also modified DispObjectImpl that it stores the Isolate on which it was created, but this also leads to access violations. Do you have an idea how to cope with this?

Best regards, jobe

jobe3774 avatar Aug 27 '18 07:08 jobe3774