Calling UWP api from unmanaged DLL - c++

Currently I have a Credential Provider implemented in C++ and I would like to incorporate BLE communication to it. This is provided in Windows 10 by the UWP class Windows.Devices.Bluetooth.GenericAttributeProfile.GattServiceProvider.
I tried to load to make a UWP DLL implementing the function and load it using LoadLibrary but got an ERROR_NOT_APPCONTAINER
Is it possible to use the UWP APIs from some unmanaged C++ code?

You can not load UWP dll inside of desktop application. However it is possible to use many UWP APIs directly as they are essentially COM-based. Typically anything not depending on application context and having public activation factory (constructor) can be used. For example see How to: Activate and Use a Windows Runtime Component Using WRL.

Related

Using ATL/COM objects in UWP apps

My company has many old projects that are written in vb6. We have some reusable software components which are written in ATL/C++. We're now considering to rewrite our app with UWP but it seems that UWP apps do not support the old COM technology. I am researching if there is a way to implement a COM object which also supports UWP. I understand that porting our old components to UWP Components would definitely work. But we do not want to maintain two version of common components because we have shipped many old software and we still provide software support. Some of those software have been used by our customers for more than 10 years. It is impossible to upgrade all software we have shipped.
I have come up with few possible solutions:
Desktop Bridge
I have found a phrase called "Packaged COM" which can be used by UWP apps with Desktop Bridge. This article, COM Server and OLE Document support for the Desktop Bridge, gives an example of calling an exe COM server from an UWP app. It seems to be a good solution if it also supprts InProcessServer. I did found something suspicious, InProcessServer, but it turns out that it is for WinRT components.
I have also tried to add the COM reference to the UWP project. The code can successfully refer to the COM object. However, it doesn't work at runtime. The UWP app has its own registry hive, therefor is won't load the COM object which is registered to the system registry. I'm thinking that if I add the registration information to the private hive of the UWP app and pack the COM dll along with the app, this may work. Just couldn't find any example.
ATL/COM
Since we have implemented our business logic into COM objects. The most viable solution would be upgrading our COM object to support UWP applications. WinRT components still use COM technology but implement the new interface IInspectable, I assume that the old ATL based COM object can be modified to support the calling convention of UWP if it implements IInspectable interface. I just don't have any clue to do this.
WRL
I found an MSDN article, How to: Create a Classic COM Component Using WRL, talking about using WRL project template to implement classes COM object which can be used in Win32 application as well as UWP apps.
It says:
You can use the Windows Runtime C++ Template Library (WRL) to create basic classic COM components for use in desktop apps, in addition to using it for Universal Windows Platform (UWP) apps
But I soon realise that WRL project template is no longer supported by Microsoft. All the download links of the project template I found on the web are no longer available.
C++/WinRT
This technology seems to be the successor of WRL. It seems promising but unfortunately have not yet been released.
To clarify few things from #Peter Torr:
(1) Do you want your app / COM object to be "pure" UWP (runs on all
devices) or are you OK with fullTrust UWP (aka Desktop Bridge) that is
Desktop-only? And (2) are the COM objects only for use with the app
that ships them, or are they supposed to be used by other apps?
My company uses Windows platform to provide PC-based industrial automation services(both hardware and software). So the answer is yes, we are happy with fullTrust UWP. Our products will only run on desktop. We just want to embrace the new technology and leverage the great UI framework of UWP.
COM objects are for both new apps(UWP) and old software(some are vb6 based). We are happy with packaging COM objects with UWP apps and using system registered COM for old software as long as those COM objects are the same(built from the same code base).

Microsoft ANGLE for uwp

guys! I'm currently working on new cross-platform application (iOS, Android, UWP). I have graphical library for UI written in c++, using freeglut for opengGL.
The problem is, I don't know how to use this library in my UWP application. I have read a lot of information about all the wrappers for OpenGL, but they all for WPF or written in C++. I also know about Microsoft ANGLE Project, but it is written in the c++ too, but I need to write the application in C# language.
So, how can I modify my own library using excluding freeglut and import it into my C# application?
By the way, sorry for mistakes in my English.
So you want to consume your legacy C++ assembly in your UWP application?
You can create a Windows Runtime Component(Universal Windows) project, in which you need to include the assembly, and declare as many APIs as you want to expose, all marked with DllImport attribute.
Eventually, this WinRT component will do the Platform Invoke, and works as a "wrapper" layer between your legacy code and UWP application.
It's up to your preference which language you use for the WinRT component, C++ or C#, either way, you need to worry about struct/type mashalling and unmashalling.
After you're done with above, you'll be able to reference the WinRT component in your UWP application, and call the APIs you declared in that assembly.
The same way works for Microsoft Angle project as well.
You might find this post useful.
One last word, you might want to avoid using forbidden APIs, otherwise your UWP app won't pass the Windows Store Certification.
According to https://msdn.microsoft.com/library/windows/desktop/jj714080.aspx there is an option to use DirectX and some native API functions including LoadLibrary and GetProcAddress functions https://msdn.microsoft.com/library/windows/desktop/jj714080.aspx
This should give you ability to use any native dll.
You can try to make RuntimeComponent project and put it there. Also you can try clr oldsyntax option to use native code as it is.

Is it possible to send Windows Toast notifications from a c++ Windows service?

I am trying to learn how to show Toast notifications from my program which is native c++ console application registered as a windows service.
I learnt that Toasts are part of the windows runtime UI components; So does this mean I have to develop a GUI component for my product in order to be able to send Toasts?
I have developed WinToast, a library written in C++ to integrate Windows Toast Notification easily. I have used it to integrate Toast notifications in different projects, specially with Qt Framework.
The native Toast Notification needs some functions of the Com Fundamentals which are availables only in moderns version of Windows (minimum supported client: Windows 8).
That's why the library loads all the required libraries dynamically. Make your application compatible with older versions of Windows using WinToast. There is an attached example explaining how to use it in the repository.
To show a toast, just create the template and your custom handler and launch it:
WinToastHandlerExample* handler = new WinToastHandlerExample;
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageWithTwoLines);
templ.setImagePath(L"imagepath");
templ.setTextField(L"firstline", 0);
templ.setTextField(L"secondline", 1);
if (!WinToast::instance()->showToast(templ, handler)) {
std::wcout << L"Could not launch your toast notification!";
}
You should check this. Pure unmanaged c++ & COM.
Yes it might be possible check this link Here
Certain WinRT classes can be used from desktop apps, including portions of the Windows.UI namespace. The toast notification APIs are one such example - they can be used by both Windows Store apps and desktop apps

Can I use an existing native Windows COM interface in a Qt app

I'm having to use an SDK modeled on Microsoft's COM. I'm developing for Windows in C++.
I found this article that implies one can "use COM in Qt", but it is not clear whether they mean to register my own, new COM interface or whether they mean I can use an existing one that wasn't created in Qt (they speak of "creating a COM server").
The question is whether this is possible in Qt and whether this is somewhat of a hack or standard practice. If it is possible, would it make a difference if Qt is using the MinGW or VS compiler?
You can read in the Qt documentation about Active Qt module :
Qt's ActiveX and COM support allows Qt for Windows developers to:
Access and use ActiveX controls and COM objects provided by any ActiveX server in their Qt applications.
Make their Qt applications available as COM servers, with any number of Qt objects and widgets as COM objects and ActiveX controls.
So the answer is that you can easily use existing com objects and activex controls created with any language in your Qt application with the API provided by the Active Qt module. You can either use VS compiler or MinGW.

How to detect if running inside Windows Metro application process?

I am developing a library and I would like to be able to detect if it is running inside Metro style application to selectively disable/enable some functionality. Is it possible?
You can use the IMetroMode interface to check if your library has been loaded inside a Metro-style application. Call the GetMonitorMode() method, if pMode will hold MMM_METRO then you'll be sure you're running inside a Metro application.
Here is a simple test:
if (WinJS) {
// WinJS exists in global if in WinRT app
}