Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Mar : CoCreateInstance, multiple threads. Only original thread can use object?
| Subject: | CoCreateInstance, multiple threads. Only original thread can use object? |
| Posted by: | "Whome" (noma..@maildot.com) |
| Date: | Fri, 30 Mar 2007 17:48:09 |
I have a javaJNI .dll library and it uses CoCreateInstance method to
create one of the directshow objects (CLSID_MediaDet).
Java virtual machine loads .dll library at the first call and then it
stays there to the end of world. This is fine.
I run the following code snippet each and every time.
* calls CoCreateInstance
* uses returned dsMediaDet instance
* set null at the end
Problem:
This works only when current thread is the same as first time being
used. If I call this .dll method from the other java thread then
CoCreateInstance call fails and return -2147221008 error code.
How should I handle CoCreateInstance objects to use then as
"create-use-destroy"?
And this method should be a thread safe, so creating a new instance each
time solved threading issues, no critical block required, right?
function getMediaDetails(const fileName: WideString): String;
var
dsMediaDet : IMediaDet;
threadId : Cardinal;
begin
threadId := GetCurrentThreadID();
try
// create Media Detector instance
retval := CoCreateInstance(CLSID_MediaDet, nil,
CLSCTX_INPROC,IID_IMediaDet,dsMediaDet);
if (retval <> S_OK) then begin
Result := 'CLSID_MediaDet failed ('
+ IntToStr(retval) + '), thread: '
+ IntToStr(threadId);
Exit;
end;
...clip...
finally
dsMediaDet := nil;
end;
end;
initialization
// Initialize COM library
CoInitialize(nil);
finalization
// Uninitialize COM library
CoUninitialize();