WinRT library not working in Release mode

2019-09-13 07:47发布

问题:

I've have been trying to build a desktop application using WinRT libraries in Visual Studio 2012. The code snippet goes as follows.

[STAThread] 
int   wmain (Platform :: Array <String ^> ^ args) 
{ 
    wcout << L"Copyright (c) Microsoft Corporation. All rights reserved." << endl; 
wcout << L"FindPackages sample" << endl << endl; 
 try 
{ 
    auto packageManager = ref new Windows::Management::Deployment::PackageManager(); 
    auto packages = packageManager->FindPackages();

    int packageCount = 0; 
    std::for_each(Windows::Foundation::Collections::begin(packages), Windows::Foundation::Collections::end(packages), 
        [&packageManager, &packageCount](Windows::ApplicationModel::Package^ package)  
    {  
        DisplayPackageInfo(package); 
        DisplayPackageUsers(packageManager, package); 
        wcout << endl; 
        packageCount += 1;  
    }); 
} 
catch (AccessDeniedException^) 
{ 
    wcout << L"FindPackagesSample failed because access was denied. This program must be run from an elevated command prompt." << endl; 
    return 1; 
} 
catch (Exception^ ex) 
{ 
    wcout << L"FindPackagesSample failed, error message: " << ex->ToString()->Data() << endl; 
    return 1; 
} 
getchar();
return 0; 

}

This is used to list the metro apps details. And this code works fine in DEBUG mode. But when I change it to release mode, I am getting an error:

error LNK2001: unresolved external symbol _NtProcessStartup 

NOTE: I've changed certain settings such as
Configuration properties -> C/C++-> COnsume Windows Runtime Exception to Yes(/ZW)
Configuration properties -> C/C++-> Code Generation-> Enable Minimal Rebuild to NO(/gm-)
Configuration properties -> C/C++-> Code Generation-> Runtime LIbrary to Multi-threaded DLL(/MD)

Its been told that these settings are mandatory for WinRT library inclusion. So basically, I have to run my code in Multi_Threaded (/MT) format for Release mode. But /MT or /Mtd is not compatible with (/ZW) method which is necessary for WinRT libraries. Please guide me on my mistakes.

回答1:

The fact that you get a message about a missing NtProcessStartup symbol implies that the linker switch /SUBSYSTEM:NATIVE was used. Because this in the only option that requires a NtProcessStartup function instead of wmain/main. So your release mode options have somehow marked your application as a NATIVE (usually device driver) application. (Or you specifically added a /ENTRY:NtProcessStartup but that seems very unlikely to me).