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

WynCommented:
You should include:

#include "objbase.h"
#include "shlobj.h"
0

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
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
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
C++

From novice to tech pro — start learning today.