error C2065: 'CComQIPtr' : undeclared identifier - visual-c++

I'm still feeling my way around C++, and am a complete ATL newbie, so I apologize if this is a basic question. I'm starting with an existing VC++ executable project that has functionality I'd like to expose as an ActiveX object (while sharing as much of the source as possible between the two projects).
I've approached this by adding an ATL project to the solution in question, and in that project have referenced all the .h and .cpp files from the executable project, added all the appropriate references, and defined all the preprocessor macros. So far so good. But I'm getting a compiler error in one file (HideDesktop.cpp). The relevant parts look like this:
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinInet.h> // Shell object uses INTERNET_MAX_URL_LENGTH (go figure)
#if _MSC_VER < 1400
#define _WIN32_IE 0x0400
#endif
#include <atlbase.h> // ATL smart pointers
#include <shlguid.h> // shell GUIDs
#include <shlobj.h> // IActiveDesktop
#include "stdhdrs.h"
struct __declspec(uuid("F490EB00-1240-11D1-9888-006097DEACF9")) IActiveDesktop;
#define PACKVERSION(major,minor) MAKELONG(minor,major)
static HRESULT EnableActiveDesktop(bool enable)
{
CoInitialize(NULL);
HRESULT hr;
CComQIPtr<IActiveDesktop, &IID_IActiveDesktop> pIActiveDesktop; // <- Problematic line (throws errors 2065 and 2275)
hr = pIActiveDesktop.CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER);
if (!SUCCEEDED(hr))
{
return hr;
}
COMPONENTSOPT opt;
opt.dwSize = sizeof(opt);
opt.fActiveDesktop = opt.fEnableComponents = enable;
hr = pIActiveDesktop->SetDesktopItemOptions(&opt, 0);
if (!SUCCEEDED(hr))
{
CoUninitialize();
// pIActiveDesktop->Release();
return hr;
}
hr = pIActiveDesktop->ApplyChanges(AD_APPLY_REFRESH);
CoUninitialize();
// pIActiveDesktop->Release();
return hr;
}
This code is throwing the following compiler errors:
error C2065: 'CComQIPtr' : undeclared identifier
error C2275: 'IActiveDesktop' : illegal use of this type as an expression
error C2065: 'pIActiveDesktop' : undeclared identifier
The two weird bits: (1) CComQIPtr is defined in atlcomcli.h, which is included in atlbase.h, which is included in HideDesktop.cpp; and (2) this file is only throwing these errors when it's referenced in my new ATL/AX project: it's not throwing them in the original executable project, even though they have basically the same preprocessor definitions. (The ATL AX project, naturally enough, defines _ATL_DLL, but I can't see where that would make a difference.)
My current workaround is to use a normal "dumb" pointer, like so:
IActiveDesktop *pIActiveDesktop;
HRESULT hr = ::CoCreateInstance(CLSID_ActiveDesktop,
NULL, // no outer unknown
CLSCTX_INPROC_SERVER,
IID_IActiveDesktop,
(void**)&pIActiveDesktop);
And that works, provided I remember to release it. But I'd rather be using the ATL smart stuff.
Any thoughts?

You may have forgotten the namespace ATL
ATL::CComQIPtr

Related

using a globals struct in several .cpp files, and initializing it in the constructor

I know similar questions have been asked, but none address this issue. I want to create a globals struct and initialize it with default values. I implemented it as below, but the project won't build.
I've tried everything I can think of, most notably moving the "extern" declaration of *gxg in and out of the header guard and changing the struct to a class, but get the same results: the project won't build because of duplicate symbols for the globals constructor. It builds if I don't use it in more than one .cpp file, or if I don't include a constructor or destructor in the struct's implementation file.
// globals.hpp
#ifndef globals_hpp
#define globals_hpp
struct gxGlobals{
double radius;
bool easement;
gxGlobals(); // constructor
} ;
extern "C" gxGlobals *gxg;
#endif /* globals_hpp */
—————————————
// globals.cpp
#include "globals.hpp"
gxGlobals::gxGlobals():
radius(24),
easement(false)
{};
———————————
// main_file.cpp
#include "globals.hpp"
gxGlobals *gxg = new gxGlobals();
———————————
// other_file.cpp
#include "globals.hpp"
// ERROR: Duplicate symbol gxGlobals::gxGlobals()
I can include globals.h in one file, but not in two or more. It also works if I remove the self-initialization in the .cpp file.
There are too many members in the actual struct to make an initializer list practical, so my last option is a function that runs on startup that plugs all of the default values in. Am I mistaken that this should work?

How to save the contents of a COM object to a file Visual C++

I'm working my way through a DirectX11 tutorial and one of the "extra credit" exercises is to compile a shader and save the compiled code to a .shader file.
The program I ended up writing is super simple so I'll just list it here instead of explaining it in English.
The main program:
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
ID3D10Blob *VS, *Errors;
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, &Errors, 0);
if(Errors)
MessageBox(NULL, L"The vertex shader failed to compile.", L"Error", MB_OK);
else if(!Errors)
MessageBox(NULL, L"The vertex shader compiled successfully.", L"Message", MB_OK);
HANDLE sHandle = CreateFile(L"compiledShader.shader",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(WriteFile(sHandle, VS->GetBufferPointer(), VS->GetBufferSize(), NULL, NULL)) // <--- break point
MessageBox(NULL, L"The compiled shader was saved successfully to 'compiledSahder.shader'", L"Message", MB_OK);
else
MessageBox(NULL, L"The vertex shader was not saved successfull.", L"Error", MB_OK);
return 0;
}
And the shader:
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
Since the contents of the VS Blob is where the compiled code is my first instinct is that I need to save the contents of that Blob to a .shader file. The only file writing I've worked with before is using fstream in basic C++ in linux so I'm unsure how to approach this. I've dug through the MSDN library for various file writing functions but I couldn't find anything that would let me write the content of a COM object to a file.
Let me know if you need any more information, and thanks in advanced for your help.
EDIT: the code has been updated per the suggestions of the current answer. I'm not 100% sure if I'm using these functions properly as I had to intuit how to by reading their descriptions on MSDN, but I think I'm on the right track. It creates the file, but when I try to write to it I'm getting the following error:
First-chance exception at 0x755cdeef in shader compiler.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x755cdeef in shader compiler.exe: 0xC0000005: Access violation writing location 0x00000000.
The program '[5604] shader compiler.exe: Native' has exited with code -1073741819 (0xc0000005).
I feel like it's a simple mistake that is causing this problem. Any ideas?
EDIT #2: Here is what gets written to the file "compiledShader.shader"
Compiled shader code?
Does this look correct? If so I'll close the question as answered and worry about whatever is causing the generic access error on my own.
Look at the ID3D10Blob interface for the content, and check the CreateFile, WriteFile and CloseHandle Win32 functions.

Warning C4278: 'GetCurrentDirectory': identifier in type library 'GCRComp.tlb' is already a macro; use the 'rename' qualifier

I'm migrating a VC++ 6.0 application to Visual studio 2008. I've fixed all the migration errors, now I'm fixing the warnings. The following warning occurs in almost 40 instances, Even after so much trial and errors and research in google, I'm not able to fix this warning.
Please find below an instance of the C42778 Error, If I get some help to fix the one below, I'll follow the same approach to fix the remaning 39 warnings.
Warning C4278: 'GetCurrentDirectory': identifier in type library 'GCRComp.tlb' is already a macro; use the 'rename' qualifier
------Code snippet from ZipFile1.h -------
#import "GCRCOmp.tlb" rename_namespace("GCRTools") // C42778
------Code snippet from gcrcomp.tlh -------
Virtual HRESULT __stdcall raw_GetCurrentDirectory {
/*[out]*/ BSTR * dirname,
/*[out, retval]*/ VARIANT_BOOL * okStatus)=0;
virtual HRESULT __stdcall get_currentDirectory {
/*[out, retval]*/ BSTR * pVal)=0;
__declspec(property(get=GetcurrentDirectory))
_bstr_t currentDirectory;
VARIANT_BOOL GetCurrentDirectory (
BSTR * dirname);
_bstr_t GetcurrentDirectory ();
------Code snippet from gcrcomp.tli-------
inline VARIANT_BOOL IFtp1::GetCurrentDirectory(BSTR * dirname){
VARIANT_BOOL _result = 0;
HRESULT _hr = raw_GetCurrentDirectory(dirname, &_result);
if(FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}
inline _bstr_t IFtp1::GetCurrentDirectory(){
BSTR _result = 0;
HRESULT _hr = get_currentDirectory(&_result);
if (FAILED(_hr) _com_issue_errorex(_hr, this, __uuidof(this));
return _bstr_t(result, false);
}
Any help to fix this warning is greatly appreciated. Thanks a lot in advance!
It because GetCurrentDirectory is already defined in Windows SDK. It is a part of ANSI/Unicode API conversion. The way to fix is undefined it before import GCRCOmp.tlb. Try this:
#pragma push_macro("GetCurrentDirectory")
#undef GetCurrentDirectory
#import "GCRCOmp.tlb" rename_namespace("GCRTools")
#pragma pop_macro("GetCurrentDirectory")

visual studio 2008 error C2371: 'int8_t' : redefinition; different basic types (http_parser.h)

i try to compile simple c/c++ app that is using http_parser from node.js
i also using libuv , and basically trying to compile this example in windows.
using visual studio 2008
but i getting this compilation error :
>d:\dev\cpp\servers\libuv\libuv_http_server\http_parser.h(35) : error C2371: 'int8_t' : redefinition; different basic types
1> d:\dev\cpp\servers\libuv\libuv-master\libuv-master\include\uv-private\stdint-msvc2008.h(82) : see declaration of 'int8_t'
the code in the http_parser.h file looks like this:
#include <sys/types.h>
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
#include <BaseTsd.h>
#include <stddef.h>
//#undef __int8
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
as you can see i tryed to undef it but it didnt worked.
what can i do so it will pass compilation .
if i just remove it im getting this error :
http_parser.c(180) : error C2061: syntax error : identifier 'unhex'
on this code section :
static const int8_t unhex[256] =
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
and many other sections that using int8_t
As it's a typedef, you can't use #ifdef or #undef etc, because these only work with symbols that have been #define'ed.
The best you can do is to make sure that the two typedef's agree, the there should be no problem.
Looking at stdint-msvc2008.h, it might be easier to modify http_parser.h to this:
typedef signed __int8 int8_t;
Any good?
Try #define HAVE_STDINT_H 1 at the top of your code to avoid redefinition of int8_t.

#pragma section and attributes

Just trying to make a new section and setting up his attributes with #pragma return this warning:
warning C4330: attribute 'write' for section '.mysec' ignored
Simple code:
#include <windows.h>
#include <stdio.h>
#pragma section(".mysec",execute,read,write)
__declspec(allocate(".mysec")) UCHAR var[] = {0xDE, 0xAD, 0xBE, 0xEF};
void main() { return; }
linker options: /DYNAMICBASE:NO, /FIXED, /NXCOMPAT:NO, /OPT:NOREF
OS/tools: Win x64 / msvc++ 110
I read some articles on MSDN and in particulary this http://msdn.microsoft.com/en-us/library/50bewfwa(v=vs.110).aspx but didn't found answer.
Thanks.
I think that this due to the execute flag. I don't think you can have a section that contains writeable code in Windows.
I might be remembering this wrong but it would a security issue and thus not supported.

Resources