Share via


How to Call the Windows Runtime Component in Standard C++

 

#1 Create a new Win32 Console Project (named it Win32DesktopApp)

 

#2 Change the target platform to Window 10

 

 

 

#3 Add “WindowsApp.lib” to “Additional Dependencies” in Linker -> Input.

 

 

 

Code:

 #include "stdafx.h"
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <iostream>
#include <sstream>

using namespace std;
using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

int main()
{
   // Initialize the Windows Runtime.
 RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);

  if (FAILED(initialize))
 {
       cout << "Failed to initialize";
  }

   // Get the activation factory for the IUriRuntimeClass interface.
  ComPtr<IUriRuntimeClassFactory> uriFactory;

 HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);

  if (FAILED(hr))
 {
       cout << "failed to activate the Windows Runtime Class Factory";

      return 0;
   }

   // Create a string that represents a URI.

  HString uriHString;

 hr = uriHString.Set(L"https://www.microsoft.com");

  if (FAILED(hr))
 {
       cout << "failed to wrapper the string";

      return 0;
   }

   // Create the IUriRuntimeClass object.
 ComPtr<IUriRuntimeClass> uri;

   hr = uriFactory->CreateUri(uriHString.Get(), &uri);

   if (FAILED(hr))
 {
       cout << "failed to create a runtime instance";

       return 0;
   }

   // Get the domain part of the URI.
 HString domainName;

 hr = uri->get_Domain(domainName.GetAddressOf());

   if (FAILED(hr))
 {
       cout << "failed to call method get_Domain";

      return 0;
   }

   wprintf_s(L"Domain name: %s\n", domainName.GetRawBuffer(nullptr));

    return 0;
}

Comments

  • Anonymous
    August 05, 2015
    The comment has been removed