Search code examples
namespaceslauncherc++-cx

C++ example for using WIndows::System::Launcher class member functions in a console C++ app


I want to make use of the member functions of the "Launcher class" Launcher class from a console C++ application, NOT a windows store app ( instead in a console application EXE).

Unlike any general WIndows API which includes a DLL, a header file as a minimum requirement, it includes a namespace Windows::System and a meta data "Windows.winmd".

So it means it uses .NET framework (Common language runtime) for this the Launcher class in thenamesapce WIndows.System.

I changed the VS settings in Properties -> Configuration -> COmmon Language runtime suspport to include CLR.

and I'm using :

using namespace System;

But I see Launcher class isn't present here. Also i tried

using namespace Windows.System;

as Launcher class is in WIndows.System namespace but couldn't find Launcher class here too.

Can I please ask for a help with a code snippet for using Launcher class member functions.


Solution

  • I didn't think this could be done initially, but it does appear to be possible.

    Sridhar Poduri put together a Visual Studio extension that creates a C++/CX Console application project template. https://visualstudiogallery.msdn.microsoft.com/e9210454-c1b5-4d89-b8ca-92a64dfb8d28 A project built from this template will be capable of calling into C++/CX APIs such as Windows::System::Launcher::LaunchURIAsync(). However I'm not sure if this particular API can be used from a command line application, when I tried it it raised an exception.

    If you want to know how to change a normal Win32 Console Application template to use C++/CX change the following in your project settings. Under C/C++->General

    1. Set "Consume Windows Runtime Extension" to "Yes /ZW"
    2. Add these paths to "Additional #using directories": "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1\ExtensionSDKs\Microsoft.VCLibs\12.0\References\CommonConfiguration\neutral\;C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral;%(AdditionalUsingDirectories)"

    Under C/C++->Code Generation

    1. Set "Enable Minimal Rebuild" to "No /Gm-"

    Making these changes should allow you to compile code that uses C++/CX APIs. Here is some sample code:

    #include <iostream>
    using namespace std;
    using namespace Platform;
    
    int main(Platform::Array<Platform::String^>^ args)
    {
        Platform::Details::Console::WriteLine("Hello World");
    
        return 0;
    }
    

    Also, I just wanted to point out that the Launcher API you are referencing is definitely not C++\CLI. It is C++\CX which shares similar syntax to C++\CLI.