Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VC++ 8.0- CoCreateInstance Failed with error 1008

570 views
Skip to first unread message

JoDeGr8

unread,
Aug 15, 2009, 2:34:41 PM8/15/09
to
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

unread,
Aug 16, 2009, 1:08:10 AM8/16/09
to
JoDeGr8 <johne...@gmail.com> wrote:
>
>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.

>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, ti...@probo.com
Providenza & Boekelheide, Inc.

JoDeGr8

unread,
Aug 16, 2009, 1:57:19 AM8/16/09
to
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]

unread,
Aug 16, 2009, 2:09:04 AM8/16/09
to

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

unread,
Aug 16, 2009, 2:54:58 AM8/16/09
to
On Aug 16, 11:09 am, "Carl Daniel [VC++ MVP]"

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]

unread,
Aug 16, 2009, 8:47:11 AM8/16/09
to

> 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.

>
> Regards,
> J
>
>

JoDeGr8

unread,
Aug 16, 2009, 11:19:07 AM8/16/09
to
On Aug 16, 5:47 pm, "Ben Voigt [C++ MVP]" <bvo...@newsgroup.nospam>
wrote:

I found that the problem occurs only when i try to create an object of
IShellWindows all other interfaces are working perfectly.

Chizl

unread,
Aug 21, 2009, 10:10:28 AM8/21/09
to
Never call CoInitialize within a function that is called over and over..
This will result in a memory leak.

"JoDeGr8" <johne...@gmail.com> wrote in message
news:eae0d1cc-8e21-43c5...@v20g2000yqm.googlegroups.com...

Carl Daniel [VC++ MVP]

unread,
Aug 21, 2009, 10:22:57 AM8/21/09
to
Chizl wrote:
> 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

unread,
Aug 21, 2009, 1:56:38 PM8/21/09
to
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.


"Carl Daniel [VC++ MVP]" <cpdaniel_remove...@mvps.org.nospam>
wrote in message news:uJcUirmI...@TK2MSFTNGP04.phx.gbl...

Chizl

unread,
Aug 21, 2009, 2:06:26 PM8/21/09
to
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.


"Carl Daniel [VC++ MVP]" <cpdaniel_remove...@mvps.org.nospam>
wrote in message news:uJcUirmI...@TK2MSFTNGP04.phx.gbl...

0 new messages