C++/CX Header file can't find Microsoft namespace

5.5k views Asked by At

I have a header file with the following code:

  Microsoft::WRL:ComPtr<ID3D11Device2> m_device;

inside a class definition. Visual Studio 2013 is saying that Microsoft is not a namespace, if I take the code and cut it out and put it in another class in another file unchanged it works just fine!

Any ideas?

Philip

EDIT: All of a sudden (without me having changed anything) Intelissense now accepts Microsoft::WRL::ComPtry as valid but when I compile it still gives me errors that it does not exists.

1

There are 1 answers

0
Chuck Walbourn On BEST ANSWER

You need to

#include <wrl.h>

or

#include <wrl/client.h>

To get Microsoft::WRL::ComPtr in your module.

When you say "Visual Studio 2013 is saying that Microsoft is not a namespace" do you mean you get a compiler error or is just Intellisense? When dealing with headers, Intellisense can get a bit out of sync until you build again. For example:

//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };

//Test.cpp
#include <wrl/client.h>
#include "Test.h"

If you just added the #include <wrl/client.h> to the Test.cpp, Intellisense might not know yet it is in scope for the header. It's perfectly valid C++ already, but a better practice is to include in your headers the ones it needs like:

//Test.h
#pragma once
#include <wrl/client.h>
class A { Microsoft::WRL::ComPtr<T> a; };

The other way this sync issue can manifest itself is if you are doing:

//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };

//Test.cpp
#include "pch.h"
#include "Test.h"

//pch.h
#include <wrl/client.h>

Again, fully valid C++ that will build. Intellisense knows it works when you build, but might not until then.

Note: WRL is traditional C++ and is not using C++/CX language extensions. They both exist to make it easier to consume WinRT APIs from C++, and you will see the Microsoft::WRL::ComPtr used inside C++/CX applications when dealing with non-WinRT COM APIs like Direct3D. And you can mix C++/CX with WRL in the same application taking advantage of the fact that you can use reinterpret_cast<> between C++/CX ref ^ and ABI COM pointers. You can use Microsoft::WRL::ComPtr in old-school Windows desktop apps on Windows 7 or Windows Vista too.

With all that said, WRL and C++/CX are two distinct things.

Update: For consuming Windows Runtime APIs, you can also use C++/WinRT which is also 'standard' C++ without any need for the C++/CX extensions. See Microsoft Docs. You can use Microsoft::WRL::ComPtr for C++/WinRT applications, or you can use their variant wrl::com_ptr