
Issues with static members/methods in COM/ATL?
Quote:
> Are you saying to use an out-of-process COM object because if it were
> in-process, each process would have its own classinstance, and thus,
> different static members?
Yes. One way to share common data between class instances is to share them
in process of common COM server - that is out-of-process that has been
registered for multiple use. Other way would be to create system service (or
other kind of local server) allowing other processes to share the same data
(through file mapping, named pipes or some other means).
If you need to share data between in-process COM servers, you may try to use
something similar to:
CommonDataServer(void *)
{
HANDLE hFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, MAX_SIZE_HIGH, MAX_SIZE_LOW, "MyAppMappedFile");
// ... some synchronization to keep our function running as long as we
need it
// ...
CloseHandle(hFile);
Quote:
}
HANDLE hFile = NULL;
LPVOID lpData = NULL;
HANDLE hSem = CreateSemaphore(NULL, MAX_THREADS, MAX_THREADS,
"Global\MyAppSemaphore");
if (hSem != NULL)
{
// ok, we have access to common data
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
// we are first to use this semaphore - thus we need to initialize
mapped file
CreateThread (NULL,NULL,CommonDataServer,(void *) &lock,0,NULL);
Sleep(20);
}
hFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"MyAppMappedFile");
lpData = MapViewOfFile (hFile,FILE_MAP_ALL_ACESS,0,0,0);
// ... here you can use common data
UnmapViewOfFile(lpData);
CloseHandle(hFile);
ReleaseSemaphore(hSem,1,NULL);
Quote:
}
... BUT if first COM client process exits, CommonDataServer(void *) will be
terminated also. You will lose access to common data, and next COM server
instances will need to re-create it - so the code above if far incomplete.
Quote:
> That would truly be a problem. However, what do I give up by leaving the
> in-process model? I was under the impression that in my case, imlementing
a
> BHO, it needs to be an in-process object.
You may easilly implement both: create very simple in-process COM object
with the basic functionality you need, which will in turn call other,
out-of-process object (use #import "... .tlb" directive in your in-process
COM server to make it simple). There are special considerations when passing
interfaces between apartments.
Regards
B.