When this call AddPackageByUriAsync could fail?

Rohan Pande 515 Reputation points
2024-04-16T06:13:17.4366667+00:00

Hi,

I am trying to create an application in the mode of 'package with external location'. Now if we go via this mode, we need to fetch the package identity during runtime, right? So, the API that has been provided is AddPackageByUriAsync .

I am following this sample of Microsoft provided here. Can we know when this call could fail?

Another question, There is some if condition in the example File Name: startup.cs (C#)
if (!ExecutionMode.IsRunningWithIdentity()) // Is there an equivalent C++ call for this?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Ryan Knuth (MSFT) 0 Reputation points Microsoft Employee
    2024-10-30T18:28:42.3233333+00:00

    IsRunningWithIdentity is a function within the demo app. It uses the following method which can be used in C++:

     [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    static extern int GetCurrentPackageFullName(ref int packageFullNameLength, ref StringBuilder packageFullName);
    

    Was this answer helpful?

    0 comments No comments

  2. Hui Liu-MSFT 48,716 Reputation points Microsoft External Staff
    2024-04-16T08:58:33.55+00:00

    Hi,@Rohan Pande. Welcome to Microsoft Q&A.

    The AddPackageByUriAsync method could fail due to various reasons, including:

    Invalid Package Uri: If the URI provided to AddPackageByUriAsync does not point to a valid package, the method will fail.

    Network Issues: If there are network issues or connectivity problems, the method may fail to download the package from the specified URI.

    Permission Issues: If the application does not have the necessary permissions to access the package from the provided URI, the method may fail.

    Invalid Package Format: If the package at the specified URI is not in the correct format or is corrupted, the method may fail.

    Security Restrictions: If the application is running in a restricted environment where downloading and installing packages from external locations is not allowed, the method may fail.

    In a C++ application, there may not be a direct equivalent to ExecutionMode.IsRunningWithIdentity(). However, you could achieve similar functionality by implementing custom logic to determine the execution context or identity of the application.

    #include <iostream>
    
    
    bool IsRunningWithIdentity()
    {
        // Your custom logic to determine if the application is running with identity
        // For example, you could check if the application is running within a specific user context or environment.
        // This could involve checking user privileges, process information, etc.
    
        // For demonstration purposes, let's assume the application is always running with identity
        return true;
    }
    
    
    int main(int argc, char* argv[])
    {
        if (!IsRunningWithIdentity())
        {
            // Do something if the application is not running with identity
            std::cout << "Application is not running with identity\n";
        }
        else
        {
            // Do something if the application is running with identity
            std::cout << "Application is running with identity\n";
        }
    
        return 0;
    }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.