问题
I am able to use a FileSaveDialog (Common Item Dialog) in a VC++ 2010 app like this:
IFileDialog *pFileDialog;
HRESULT hr = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
but when I put this code into my project that has been converted from VC++ 6.0 to VC++ 2010 I get the following error:
"error C2787: 'IFileDialog' : no GUID has been associated with this object"
I also get a red squiggle under the IID_PPV_ARGS
macro and the float-over error:
"operand of _uuidof must have a class or enum type for which _declspec(uuid('...')) has been specified"
I am NOT using the Common Language Runtime Support (/clr) in either project.
How do I associate a GUID with my object?
回答1:
The problem was that I had set a compiler flag targeting the Win XP OS. That's why a feature introduced in Vista wasn't defined.
I had _WIN32_WINNT = 0x0501
(WinXP). When I changed it to 0x0600
(Vista) the IFileDialog was defined.
Mark, your suggestion about looking into the definition of IFileDialog lead me to the cause. It led me to the ShObjIdl.h
file but the section where IFileDialog was defined was greyed out, leading me up to the #if (NTDDI_VERSION >= NTDDI_VISTA)
conditional.
Thanks!
回答2:
My VC++ 6.0 doesn't define IFileDialog, not in the base package and not in the Windows SDK. Did you back port it from somewhere?
I would look at the definition of IFileDialog in VC++ 10. I'm guessing it's defined with the benefit of some macro, and that macro includes or excludes the _declspec(uuid('...'))
depending on some compile-time constant which is set wrong.
Edit: In VC++ 10 IFileDialog is defined with the help of the MIDL_INTERFACE macro, in ShObjIdl.h. The MIDL_INTERFACE macro is defined in 3 different files, so it's hard to tell which definition you're picking up; they're all different. However I don't see any way in which the definition wouldn't be associated with a GUID.
Is it possible you're doing a forward definition of IFileDialog on your own that does not include the GUID?
来源:https://stackoverflow.com/questions/5346451/how-do-i-use-ifiledialog-in-a-vc-2010-project-converted-from-vc-6-0