I'm writing plugin for one software. This software invokes
void Init() {...}
on loading and have multithreading feature: program can run multiple threads and can call custom functions from my plugin at the same time.
In my plugin I'm using COM objects which I initialize following way:
void Init() { // "Global" initializaton
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
position.CreateInstance(__uuidof(Position));
order.CreateInstance(__uuidof(Order));
}
And next I implement plugin-based function (example):
int SendOrder(....) {
return order.SendOrder(...); // invoke COM object's method
}
Problem is that this variant not working as expected so I moved COM object instantiation directly to the function's body:
int SendOrder(....) {
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
order.CreateInstance(__uuidof(Order));
int ret = order.SendOrder(...);
CoUnitialize();
return ret;
}
Now COM object will be instantiated on every function call and this variant works as expected (every thread now have it's own apartment and object's instance), but I'm afraid that is not the best solution, because instantiation is costly operation.
Can this be done somehow better?
SendOrderfrom multiple threads at the same time? What isn't working as expected?orderinInit, what is the problem you're seeing?