How to use CoCreateInstanceEx?

In vc6.0,I write a CoCreateInstanceEx call,but when I compile the project,it show:"error C2065: 'CoCreateInstanceEx' : undeclared identifier",How should I do?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Include ole2.h or windows.h or richole.h
I'm sure he is.

This function was added for win95 and NT 4, so it is not part of the original win32 API.  so it is recognized only if you have the _WIN32_WINNT #defined as >= 0x04000.  (indicating a Win NT 4.0) target.  Or if you have #defined _WIN32_DCOM

Let me know if you have any questions.
shalaAuthor Commented:
I try,but still fail.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

You tried what?   Did you set the target version number, like I suggested?
The CoCreateInstance is declared in the <objbase.h> file.
BUT: The easiest is to write it in one of the following:

In an MFC AppWizard Dialog Based Exe:
Inside the ...Dlg.cpp file, after

In an MFC AppWizard SDI/MDI based Exe:
In the ...Doc.cpp file.

In an ATL AppWizard COM Exe or DLL.

In all cases the CoCreateInstance is declared in the "stdAfx.h" file which calls the <atlbase.h> or the <afxdisp>
or the <afxcomn.h>.

You should not attempt to do these manually.
One thing that happens many times is the place you put some .h files inside your code. Play around with the order of your header files.
If it doesn't help would you like to send the code?
shalaAuthor Commented:
I tried:
# include <ole2.h> or <windows.h> or <richole.h>


#define _WIN32_WINNT 0x04000
but lost.

I just want to create a remote object(a Activex EXE on different machine), have a sample method?
also add the
#define _WIN32_DCOM

AND take a look at the example at
Scroll down to the "client code" and click: "view client code".
There's nothing special in the example, except that it's a console app, and added the _WIN32_DCOM in the stdafx.h file.

What version of VStudio are you working with?

Good luck, Moshe
shalaAuthor Commented:
Now in vc++6.0,I added the _WIN32_DCOM in the stdafx.h file,and copy the example code,but fail in:
#include "RemoteServer.h"
#include "RemoteServer_i.c"

I don't have the two files,I only have a exe file(activex exe writed by vb),how to create the two files?
shalaAuthor Commented:
I want to see other answers.
Hi sala,

You don't need the _i.c and the .h if your are using smartpointers!
The smartpointer comes from the .exe .dll or .ocx via the #import directive.

You can also use the "Insert ActiveX component" in your MFC Appwizard Exe project. Simply Right Mouse Button on the dialog you are developing (in the resource view after double clicking the resources/dialog/IDD_MYPROJ_DIALOG, and "Insert ActiveX component".

Or you can use the menu: project/add to project/components and controls
And add your item to the project.
Then use it from the toolbar. (along with buttons edit boxes and other controls, you have your new control, drag onto your dialog, and work with it.)

Good luck, Moshe
Pl. check the following:
1. At the start of the code use
2. Include header objbase.h.
3. Instead of directly using the function use with double scope resolution. i.e.
::CoCreateInstanceEx( ... );

4. Ensure the OS is NT 4.0 or later OR
Windows95 or later.

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.