C++, WinRT, VS2017, Windows10, Bluetooth LE
I have a stand-alone C++/WinRT VS2017 console app that was used for testing code to control a Bluetooth LE device. That console app works and I am now in the process of moving that code into an existing C++ MFC VS2017 app which also works.
In the existing MFC app I first installed the NuGet cppwinrt package that I used in the test console app (no problems)
I then put the various WinRt header files that were in the console app's pch.h into the MFC's stdafx.h file
#pragma comment(lib, "windowsapp")
#include <condition_variable>
#include "winrt\Windows.Foundation.h"
#include "winrt\Windows.Storage.Streams.h"
#include "winrt\Windows.Devices.Bluetooth.h"
#include "winrt\Windows.Devices.Bluetooth.Advertisement.h"
#include "winrt\Windows.Devices.Bluetooth.GenericAttributeProfile.h"
So far, so good. The MFC app still compiles without problems. However, I then put the "using namespace" entries, that were at the top of the console app's .cpp file, into the MFC's ...View.cpp and that is when I get the compile problems
using namespace winrt;
using namespace Windows::Foundation;
using namespace winrt::Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::Bluetooth::Advertisement;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
The compile error I get is:
Error C2872 'IUnknown': ambiguous symbol MyMFCApp c:\program files
(x86)\microsoft visual
studio\2017\professional\vc\tools\msvc\14.16.27023\atlmfc\include\atlcom.h 3456
I see that IUnknown seems to be fairly universal throughout Windows APIs. How should I go about clearing up this error?
#AlanBirtles answer seems to have solved the problem. In the console app I could not compile without these using namespace entries so I just assumed that I would also need them in the MFC app. As I mentioned in my comment above, all of the namespaces compiled in the MFC app except for the two Foundation namespaces.
//using namespace Windows::Foundation; // errors IUnknown
//using namespace winrt::Windows::Foundation; // errors IUnknown
I left the rest and commented these two out and then added the code to create the BluetoothLEAdvertisementWatcher, attach a callback for the watcher.Received() and let it find my device. It ran fine. I have not yet tried with the rest of the namespaces commented out but I will.
I did try #IInspectable suggestion for using the #include <unknwn.h> before all of the WinRT namespaces. I put that in at the top and then uncommented one of the two Foundation namespaces but still got the ambiguous IUnknown error.
So the answer seems to be (for my situation anyway) that the namespaces were not necessary...at least the two Foundation namespaces.
Thanks for the help. It is really appreciated.
Related
I'm trying to reproduce an issue, so I have created an empty MFC C++ application using the VS2019 wizard and a separate native Unit Test project.
Before adding the Unit Test project, the MFC application compiled and launched successfully.
The MFC application still compiles successfully, but the Unit Test project will not compile. I'm getting two errors:
E0035 #error directive: "include 'pch.h' before including this file for PCH"
C1189 #error: "include 'pch.h' before including this file for PCH"
However, the only file in the Unit Test project (UnitTest1.cpp) already includes pch.h at the top of the file:
#include "pch.h"
#include "CppUnitTest.h"
#include "../MFCApplication1/MFCApplication1.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
CMFCApplication1App app;
bool result = app.InitInstance();
Assert::IsTrue(result);
}
};
}
It seems to be telling me to do something that is already done.
What's going on here?
Try to include stdafx.h and remove pch.h, it resolves the issue
I had the same issue. I found that I could not include the MFC DLL project's default header file directly. In your example this would be #include "../MFCApplication1/MFCApplication1.h"
I eventually found this example MFC DLL project code: https://github.com/Microsoft/VCSamples/tree/master/VC2010Samples/MFC/advanced from a list of examples here.
Notice that in the example that the default header/implementation files created by the MFC DLL project creation wizard (in this example, DLLScreenCap.h) don't export or provide any additional functionality. An existing MFC DLL project that I work with did the same.
So I added a class to the MFC DLL project to be tested and exported a simple function from it, and tested this exported function from my unit test project after linking to the project under test.
Exported class and function look like this:
#pragma once
class __declspec(dllexport) MFCLibraryExports
{
public:
// tests returning 17 to test unit test library against an mfc project
int SampleExport();
};
In your example I know that you are trying to test an instanced app and my answer doesn't help with that, but I was able to confirm that I can test at least a function exported from an MFC DLL project using the MS Unit Test framework. I'm not sure if you are expected to be able to get access to the application from a unit test as in your example; I am not able to include that header directly.
With C++/WinRT Microsoft apparently made quite the effort to make their APIs standard-conformant.
And finally they've also released a machine learning API along with a code sample repo.
Unfortunately, all samples are dependent on Visual Studio. Even the simplest command line demo (CustomTensorization) requires .snl files and VisualStudio.
Is it possible to write code for this API without VisualStudio by just downloading an SDK and using a regular make file?
If so, how? Please post or point to a MCVE.
Thank you.
I don't know anything about the machine learning API, but C++/WinRT is a header-only library that you can include and build from a developer command prompt quite easily. Here's a simple example:
C:\ml>type sample.cpp
#pragma comment(lib, "windowsapp")
#include <winrt/Windows.AI.MachineLearning.h>
#include <stdio.h>
using namespace winrt;
using namespace Windows::AI::MachineLearning;
int main()
{
init_apartment();
puts("Sample");
}
C:\ml>cl /EHsc /std:c++17 /nologo sample.cpp
sample.cpp
C:\ml>sample.exe
Sample
For an actual example using the machine learning API I'd suggest you start here:
https://learn.microsoft.com/en-us/windows/ai/get-started-desktop
But again, you can follow along and substitute Visual Studio with the command line and use cmake, or any other build system, if so desired.
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.
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
I'm u unable to use the following namespace "using namespace Microsoft::Win32"; in visual studio 2010.Actually I'm trying to access the system registry using this command.Whenever,I type this command i end up getting something like "name followed by :: must be a class or namespace name" .Help me out below mentioned is my code.
#include<stdio.h>
#include<conio.h>
#include<Windows.h>
#include <tchar.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
using namespace Microsoft::Win32;
According to Microsoft examples on msdn.microsoft.com, there is such a namespace and their C++ examples use:
using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;
You might also need to set your project to include the relevant Assembly, which is mscorlib (in mscorlib.dll).
Per comment by Creris below, apparently this also requires the CLR compiler option /clr.
(Oh, and sorry for posting the C# reference to Microsoft.Win32)
I'm wondering how Visual Studio, other IDE's, and any other sort of circumstances (ie. no IDE at all) handle bringing in code from outside.
At first I thought #includes were the only way to do this, by either placing the assembly files in the designated directory for Visual Studio assembly files and then using the <> format to bring them in, or by putting the assembly files in the project directory and using the "" format to bring them in (that is, <> and "" respectively). But now I come up to the example at the end of this post with the #using directive (which, to note, is different than just the 'using' directive without the '#', for namespaces). Also I've come across adding assembly references in visual studio from within the 'configuration properties' dialogue.
So, would someone set me straight on all the in's and out's of adding assembly files and other code to a given project?
--The following is the example that has confused me-->
I have this section in my book that states:
"...The figure combines C++ 2008 code
with legacy C and native C++ code. It
also presents the two assembly
reference files you'll use most often
with C++ 2008, along with their
associated namespaces. Unlike when
you use Visual Studio to develop a
project, the assembly reference files
aren't included by default when you
code a single source file. Because of
that, you must code #using directives
for these files. ..."
#include <stdio.h>
#include <iostream>
#using <system.dll>
#using <system.windows.forms.dll>
// Associated namespace directives
using namespace std;
using namespace System;
using namespace System::Windows::Forms;
void main()
{
printf( "Hello, Earth\n"); // from stdio.h
cout << "Hello, Mars\n"; // from iostream
Console::WriteLine("Hello, Jupiter"); // from system.dll
MessageBox::Show ("Hello, Saturn"); // from system.windows.forms.dll
}
This is not native C++ (usually just referred to as C++), but C++/CLI, which is actually a .NET language designed to ease interacting between native and managed code, and as such can use both. It is, however, definitely not C++, despite an intentionally strong resemblance. Assemblies are .NET managed code repositories. You use the #using command to use them. #include is for native C++ headers. You should also be able to add managed references (that is, #using but done throughout for you) from the project's properties.
In native C++, then you must #include headers, and if appropriate, link to .lib files (or use GetProcAddress manually), and Visual Studio also offers #import for COM libraries. C++/CLI also offers #using for bringing in managed assemblies.
void main()
{
printf( "Hello, Earth\n"); // C native code
cout << "Hello, Mars\n"; // C++/CLI's wrapper on C++ Standard
Console::WriteLine("Hello, Jupiter"); // .NET managed code
MessageBox::Show ("Hello, Saturn"); // A thin wrapper on WinAPI
}
If you don't already know both C++ and .NET code, and/or you're not trying to link the two together, it's not recommended to use C++/CLI.