Discussion:
VC++ 8.0- CoCreateInstance Failed with error 1008
(too old to reply)
JoDeGr8
2009-08-15 18:34:41 UTC
Permalink
Hi,
My application i calls a dll[Dll1.dll] which installs an keyboard
hook. Whenever user presses some key the hook function will execute
from another dll[Dll2.dll]. The hook function will try to create a COM
object using CoCreateInstance.I have given CoInitialize on top. The
problem is that the CoCreateInstance fails with error 1008. Please
tell me what is happening.

The same code is running perfectly when i use it in different exe or
dl..

Thanks,
J
Tim Roberts
2009-08-16 05:08:10 UTC
Permalink
Post by JoDeGr8
My application i calls a dll[Dll1.dll] which installs an keyboard
hook. Whenever user presses some key the hook function will execute
from another dll[Dll2.dll]. The hook function will try to create a COM
object using CoCreateInstance.I have given CoInitialize on top.
Where do you call CoInitialize? I don't think the system makes any
promises about which thread is used to call your hook callback.
Post by JoDeGr8
The problem is that the CoCreateInstance fails with error 1008.
Please tell me what is happening.
That would be ERROR_NO_TOKEN, but that's not the usual format of a COM
error. What object are you trying to create? Can you show us the code you
used to create the object, and the code that displays the error number?
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
JoDeGr8
2009-08-16 05:57:19 UTC
Permalink
Thanks, I am using the following code


CoInitialize(NULL);
IShellWindows *psw;
//AfxMessageBox(L"Reached here1");
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL,
IID_IShellWindows, (void**)&psw)))

AfxMessageBox(L"Pass");
else{

CString str;
str.Format(L"%d",GetLastError());
AfxMessageBox(str);
}
Carl Daniel [VC++ MVP]
2009-08-16 06:09:04 UTC
Permalink
Post by JoDeGr8
Thanks, I am using the following code
CoInitialize(NULL);
IShellWindows *psw;
//AfxMessageBox(L"Reached here1");
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL,
IID_IShellWindows, (void**)&psw)))
AfxMessageBox(L"Pass");
else{
CString str;
str.Format(L"%d",GetLastError());
AfxMessageBox(str);
}
Well one problem that you definitely have is calling GetLastError following
CoCreateInstsance - there's absolutely no guarantee that GetLastError
returns anything even remotely related to the COM error. Instead, you need
to hold onto the HRESULT returned by CoCreateInstance and examine that to
determine why the call is failing.

-cd
JoDeGr8
2009-08-16 06:54:58 UTC
Permalink
On Aug 16, 11:09 am, "Carl Daniel [VC++ MVP]"
Post by Carl Daniel [VC++ MVP]
Post by JoDeGr8
Thanks, I am using the following code
CoInitialize(NULL);
IShellWindows *psw;
//AfxMessageBox(L"Reached here1");
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL,
                               IID_IShellWindows, (void**)&psw)))
AfxMessageBox(L"Pass");
else{
CString str;
str.Format(L"%d",GetLastError());
AfxMessageBox(str);
}
Well one problem that you definitely have is calling GetLastError following
CoCreateInstsance - there's absolutely no guarantee that GetLastError
returns anything even remotely related to the COM error.  Instead, you need
to hold onto the HRESULT returned by CoCreateInstance and examine that to
determine why the call is failing.
-cd
Thanks,
The value in the HRESULT is - "Failed to allocate necessary memory"

This problem only occurs within the hook function. Any other function
in the same dll i put this code it is works just fine.

Any security reasons??

Regards,
J
Ben Voigt [C++ MVP]
2009-08-16 12:47:11 UTC
Permalink
Post by JoDeGr8
Thanks,
The value in the HRESULT is - "Failed to allocate necessary memory"
This problem only occurs within the hook function. Any other function
in the same dll i put this code it is works just fine.
Any security reasons??
Well, it's a very bad idea (i.e. will fail or cause other failures randomly)
to call CoInitialize from a hook function. Maybe that thread was using COM
with different settings. If the application already called CoInitialize,
your options will be ignored and if they chose a different apartment model
your CoCreateInstance call will fail. If your call to CoInitialize comes
first, then the application settings will be ignored and the app may be
unable to use its own COM objects. A very bad situation either way.

Instead, use one of the many IPC methods to move the data to a thread you
control and do the COM interaction there.
Post by JoDeGr8
Regards,
J
JoDeGr8
2009-08-16 15:19:07 UTC
Permalink
Post by Ben Voigt [C++ MVP]
Post by JoDeGr8
Thanks,
The value in the HRESULT is - "Failed to allocate necessary memory"
This problem only occurs within the hook function. Any other function
in the same dll i put this code it is works just fine.
Any security reasons??
Well, it's a very bad idea (i.e. will fail or cause other failures randomly)
to call CoInitialize from a hook function.  Maybe that thread was using COM
with different settings.  If the application already called CoInitialize,
your options will be ignored and if they chose a different apartment model
your CoCreateInstance call will fail.  If your call to CoInitialize comes
first, then the application settings will be ignored and the app may be
unable to use its own COM objects.  A very bad situation either way.
Instead, use one of the many IPC methods to move the data to a thread you
control and do the COM interaction there.
Post by JoDeGr8
Regards,
J
I found that the problem occurs only when i try to create an object of
IShellWindows all other interfaces are working perfectly.
Chizl
2009-08-21 14:10:28 UTC
Permalink
Never call CoInitialize within a function that is called over and over..
This will result in a memory leak.
Post by JoDeGr8
Hi,
My application i calls a dll[Dll1.dll] which installs an keyboard
hook. Whenever user presses some key the hook function will execute
from another dll[Dll2.dll]. The hook function will try to create a COM
object using CoCreateInstance.I have given CoInitialize on top. The
problem is that the CoCreateInstance fails with error 1008. Please
tell me what is happening.
The same code is running perfectly when i use it in different exe or
dl..
Thanks,
J
Carl Daniel [VC++ MVP]
2009-08-21 14:22:57 UTC
Permalink
Post by Chizl
Never call CoInitialize within a function that is called over and
over.. This will result in a memory leak.
No, it won't. You can call CoInitialize as many times as you want from a
thread - only the first such call has any effect other than to increase the
success count. If you've called CoInitialize 'n' times, you must also call
CoUninitialize 'n' times.

http://msdn.microsoft.com/en-us/library/ms678543(VS.85).aspx

-cd
Chizl
2009-08-21 17:56:38 UTC
Permalink
Unless they have changed something in VC8/9, in VC6 we see over time a
memory leak when calling CoInitialize each time a method was called, even
with a CoUninitialize for each call.. We chased a memory leak for weeks
before we found this was our issue. Once we moved the method out and to the
top of our thread, the leak went away. Since that time, I've made it a
good practice to always follow this rule.
Post by Carl Daniel [VC++ MVP]
Post by Chizl
Never call CoInitialize within a function that is called over and
over.. This will result in a memory leak.
No, it won't. You can call CoInitialize as many times as you want from a
thread - only the first such call has any effect other than to increase
the success count. If you've called CoInitialize 'n' times, you must also
call CoUninitialize 'n' times.
http://msdn.microsoft.com/en-us/library/ms678543(VS.85).aspx
-cd
Chizl
2009-08-21 18:06:26 UTC
Permalink
Since that was like 8 years ago, I started looking and I remember a little
more about what happened.. My project was an ATL Service and we used
CoInitializeEx with multithreaded params.. I don't have any way of going
back and seeing out it was implemented, so I can't verify everything was in
place. I do remember looking up CoInitalizedEx at the time and Microsoft
was pretty clear about this call should only be called one time per thread,
however now the wording doesn't appear quite so blunt. Then again, I'm
getting old, I may be delusional.

http://msdn.microsoft.com/en-us/library/ms695279(VS.85).aspx
CoInitializeEx must be called at least once, and is usually called only
once, for each thread that uses the COM library.
Post by Carl Daniel [VC++ MVP]
Post by Chizl
Never call CoInitialize within a function that is called over and
over.. This will result in a memory leak.
No, it won't. You can call CoInitialize as many times as you want from a
thread - only the first such call has any effect other than to increase
the success count. If you've called CoInitialize 'n' times, you must also
call CoUninitialize 'n' times.
http://msdn.microsoft.com/en-us/library/ms678543(VS.85).aspx
-cd
Loading...