Using interfaces in VC

This has to be an easy question.  I've never been able to figure out how to use interfaces in Visual C++.  I have some sample code that uses the IActiveDesktop interface (the one I want to use) but I don't know what .h files etc. to include to make it compile.  Here's the sample code:

      HRESULT hr;
      IActiveDesktop *pActiveDesktop;

      //Create an instance of the Active Desktop
      hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
                        IID_IActiveDesktop, (void**)&pActiveDesktop);

      //Insert code to call the IActiveDesktop methods
      char sWallpaper[256];
      pActiveDesktop->GetWallpaper(sWallpaper, 256, 0);


      // Call the Release method
      pActiveDesktop->Release();

I simply stuck the code in the OnInitDialog() of my dialog-based AppWizard app.  

Any help is much appreciated!  TIA.
LVL 1
mcsilverAsked:
Who is Participating?
 
WynConnect With a Mentor Commented:
You should include:

#include "objbase.h"
#include "shlobj.h"
0
 
WynCommented:
if you want to use a .dll com object which is bound with a type library .
You can use #import in VC++.
0
 
mcsilverAuthor Commented:
I added the two include statements, but I still get these compile errors:

P:\Moon\Test\TestDlg.cpp(132) : error C2027: use of undefined type 'IActiveDesktop'
        c:\program files\microsoft visual studio\vc98\include\comdef.h(507) : see declaration of 'IActiveDesktop'
P:\Moon\Test\TestDlg.cpp(132) : error C2227: left of '->GetWallpaper' must point to class/struct/union
P:\Moon\Test\TestDlg.cpp(136) : error C2027: use of undefined type 'IActiveDesktop'
        c:\program files\microsoft visual studio\vc98\include\comdef.h(507) : see declaration of 'IActiveDesktop'
P:\Moon\Test\TestDlg.cpp(136) : error C2227: left of '->Release' must point to class/struct/union

I suspect that I have to use the #import statement, as you suggested, but I don't know where to add it, or what file to import for the IActiveDesktop interface.  Suggestions?
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.

 
WynCommented:
Here it is:
#include "objbase.h"
#include "wininet.h"
#include "shlobj.h"
0
 
WynCommented:
On this points,you should use .h.
0
 
WynCommented:
on the point,.h is standard to shell-extention interface.Standard head file is supported.
0
 
mcsilverAuthor Commented:
Thank you for your answer, but I'm having trouble with your english.  Your last two comments didn't mean anything to me.  However, I did include the three .h files that you listed

#include "objbase.h"
#include "wininet.h"
#include "shlobj.h"

and I still get the compile errors.  I put the #include's into stdafx.h - hope that's alright.  Any other suggestions?  Can you clarify the following:

"on the point,.h is standard to shell-extention interface.Standard head file is supported."

Thanks!
0
 
WynCommented:
If you include  three head files above,it should work .I'v tried it.Here is the workable code:
(try to open a empty console project and add a new .cpp .Then copy below to it )

#include "objbase.h"
#include "wininet.h"
#include "shlobj.h"
int main(int argc, char* argv[])
{

HRESULT hr;
IActiveDesktop *pActiveDesktop;

//Create an instance of the Active Desktop
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);

//Insert code to call the IActiveDesktop methods
USHORT sWallpaper[256];
pActiveDesktop->GetWallpaper(sWallpaper, 256, 0);


// Call the Release method
pActiveDesktop->Release();
      return 0;
}


If you get error.Let me know.

>>"on the point,.h is standard to shell-extention interface.Standard head file is supported."

Sorry for my bad English.
I mean if you use interfaces like IActiveDesktop(extensions for shell) .They are standard for VC and have their own header files.
If you use some Activex or so,sometimes You'v got to use import if .h is unavailable for you.But most times this work will be done by VC unless you write your application by pain c++.

Best Regards
Wyn
0
 
mcsilverAuthor Commented:
Hi, I compiled your code in a console project with no problems.  But it still doesn't work in an MFC Windows project (dialog-based, or SDI).  Any ideas?  Could it be the project settings?  
0
 
nsivatgCommented:
Even I have the similar problem. So if any one knows the solution , please let me know.

Thanks

Siva
0
 
mcsilverAuthor Commented:
I haven't found the solution yet, but then I hadn't been inquiring in the meantime.  If you haven't tried microsoft.public.vc.mfc yet then post the question there, or maybe I will if you don't.
0
 
nsivatgCommented:
I got the solutino for SDK.

We have to include the file #include "wininet.h"  before we include any other file.

Siva
0
All Courses

From novice to tech pro — start learning today.