Link to home
Start Free TrialLog in
Avatar of mcsilver
mcsilverFlag for Canada

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Wyn
Wyn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wyn
Wyn

if you want to use a .dll com object which is bound with a type library .
You can use #import in VC++.
Avatar of mcsilver

ASKER

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?
Here it is:
#include "objbase.h"
#include "wininet.h"
#include "shlobj.h"
On this points,you should use .h.
on the point,.h is standard to shell-extention interface.Standard head file is supported.
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!
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
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?  
Even I have the similar problem. So if any one knows the solution , please let me know.

Thanks

Siva
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.
I got the solutino for SDK.

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

Siva