• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1126
  • Last Modified:

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?
0
shala
Asked:
shala
  • 4
  • 3
  • 2
  • +2
1 Solution
 
WxWCommented:
Include ole2.h or windows.h or richole.h
0
 
nietodCommented:
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.
0
 
shalaAuthor Commented:
I try,but still fail.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
You tried what?   Did you set the target version number, like I suggested?
0
 
mflamCommented:
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
CoInitialize(NULL);

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

Or:
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?
0
 
shalaAuthor Commented:
I tried:
# include <ole2.h> or <windows.h> or <richole.h>

or

#define _WIN32_WINNT 0x04000
 
but lost.

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

AND take a look at the example at
http://journal.iftech.com/articles/dcom_3/
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
0
 
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?
0
 
shalaAuthor Commented:
I want to see other answers.
0
 
mflamCommented:
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
0
 
ajitmCommented:
Pl. check the following:
1. At the start of the code use
CoInitialize(NULL);
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.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now