none
How to deploy and run an appx from c# code?

    Question

  • Not entirely sure if this is the correct forum, but seems the most relevant to this particular subject.

    I'm trying to figure out exactly what I need to do in order to deploy and run an appx on my development machine from a .net 4.5 c# desktop program.  The appx will then be submitted to the store at some point in the future, but for the moment I just want to test on my local machine.

    I have run into a bit of a problem that the package manager functions (Windows.Management.Deployment.PackageManager) all appear to require that you are running as an administrator. Visual studio doesn't run as an administrator and yet is able to deploy. How can I deploy, preferably without building an appx, and definitely not as an administrator?

    Then having done that, how do I run my newly added metro application from my c# desktop program?

    ...Stefan

    Sunday, May 27, 2012 2:59 AM

Answers

  •     public enum ActivateOptions
        {
            None = 0x00000000,  // No flags set
            DesignMode = 0x00000001,  // The application is being activated for design mode, and thus will not be able to
            // to create an immersive window. Window creation must be done by design tools which
            // load the necessary components by communicating with a designer-specified service on
            // the site chain established on the activation manager.  The splash screen normally
            // shown when an application is activated will also not appear.  Most activations
            // will not use this flag.
            NoErrorUI = 0x00000002,  // Do not show an error dialog if the app fails to activate.                                
            NoSplashScreen = 0x00000004,  // Do not show the splash screen when activating the app.
        }
    
        [ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IApplicationActivationManager
        {
            // Activates the specified immersive application for the "Launch" contract, passing the provided arguments
            // string into the application.  Callers can obtain the process Id of the application instance fulfilling this contract.
            IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
            IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
            IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
        }
    
        [ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]//Application Activation Manager
        class ApplicationActivationManager : IApplicationActivationManager
        {
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/]
            public extern IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            public extern IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            public extern IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                ApplicationActivationManager appActiveManager = new ApplicationActivationManager();//Class not registered
                //IApplicationActivationManager iappActiveManager = (IApplicationActivationManager)appActiveManager;
                uint pid;
                appActiveManager.ActivateApplication("MyTest_nyr66b22ne60y!App", null, ActivateOptions.None, out pid);
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    Why that part has no document to show to developers, it would has the reasons, but we don't know.

    Use the Metro Style Application without installed by the way .bat file provided(it in the folder when you chose to create a package for local installation) has no document supported. Even though the thinking form my post about Visual Studio way, as I said it is my thinking, rather than it really do things like that.

    For the undocumented things we do not recommend you use, since it would change in future, or some others reason which would cause your system or the product cannot work well(we cannot ensure the reverse engineering way got things is complete which can let it work as the Visual Studio did for you well).

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 11:35 AM

All replies

  • Mike,

    Thanks for your suggestions. Unfortunately,  the powershell command requires administrative privileges.  I can add the package in C# code, but it requires administrative privileges.  I want deploy the same way visual studio does without building the appx, deploying directly from a folder, which does not appear to require administrator privileges.

    Regarding the second part, running the application, the code you mentioned is all in C++, and it appears that someone else has had trouble running it from C#. How do I utilize that functionality in C# ?

    Thanks

    Stefan

    Monday, May 28, 2012 8:11 AM
  • [Regarding the second part, running the application, the code you mentioned is all in C++, and it appears that someone else has had trouble running it from C#. How do I utilize that functionality in C# ?]

    What errors?

    [ Unfortunately,  the powershell command requires administrative privileges.  I can add the package in C# code, but it requires administrative privileges.  I want deploy the same way visual studio does without building the appx, deploying directly from a folder, which does not appear to require administrator privileges.]

    I'm afraid that there's no public way can let developers to research and use the Visual Studio deployed way.

    Or it would be the Visual Studio does not "deploy" the application, it is just add the registry according to the package xml content, and then active it. I used the "Process monitor" to find out that the assembly it used is just your debug output path, so I said it is just add the registry and point the path to here, rather than deploy the application for you.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, May 29, 2012 8:30 AM
  • Mike,

    Right now I cannot find the message that I was referring to regarding errors in a C# application. The question I have is how the way to use that interface in a C# application, there doesn't appear to be any documentation on how to do that.  Where do I look to get some information on how to do that?

    Which particular keys are you referring to, and how would I get them into the registry in an appropriate format to be recognised by WinRT?  It seems to be something that should be documented, so that others can use it.  It's not something that is so valuable to me that it is worth spending days reverse engineering how visual studio works it, but seems to be something that should be documented for desktop applications like mine that are building appx files.

    Thanks,
    Stefan

    Tuesday, May 29, 2012 10:01 PM
  •     public enum ActivateOptions
        {
            None = 0x00000000,  // No flags set
            DesignMode = 0x00000001,  // The application is being activated for design mode, and thus will not be able to
            // to create an immersive window. Window creation must be done by design tools which
            // load the necessary components by communicating with a designer-specified service on
            // the site chain established on the activation manager.  The splash screen normally
            // shown when an application is activated will also not appear.  Most activations
            // will not use this flag.
            NoErrorUI = 0x00000002,  // Do not show an error dialog if the app fails to activate.                                
            NoSplashScreen = 0x00000004,  // Do not show the splash screen when activating the app.
        }
    
        [ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        interface IApplicationActivationManager
        {
            // Activates the specified immersive application for the "Launch" contract, passing the provided arguments
            // string into the application.  Callers can obtain the process Id of the application instance fulfilling this contract.
            IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
            IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
            IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
        }
    
        [ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]//Application Activation Manager
        class ApplicationActivationManager : IApplicationActivationManager
        {
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/]
            public extern IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            public extern IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            public extern IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                ApplicationActivationManager appActiveManager = new ApplicationActivationManager();//Class not registered
                //IApplicationActivationManager iappActiveManager = (IApplicationActivationManager)appActiveManager;
                uint pid;
                appActiveManager.ActivateApplication("MyTest_nyr66b22ne60y!App", null, ActivateOptions.None, out pid);
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    Why that part has no document to show to developers, it would has the reasons, but we don't know.

    Use the Metro Style Application without installed by the way .bat file provided(it in the folder when you chose to create a package for local installation) has no document supported. Even though the thinking form my post about Visual Studio way, as I said it is my thinking, rather than it really do things like that.

    For the undocumented things we do not recommend you use, since it would change in future, or some others reason which would cause your system or the product cannot work well(we cannot ensure the reverse engineering way got things is complete which can let it work as the Visual Studio did for you well).

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 11:35 AM
  • Thanks Mike, I can at least start the application with that code, so that is helpful, thank you.

    ...Stefan

    Wednesday, June 06, 2012 10:26 PM
  • You're welcome!

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, June 07, 2012 4:27 AM
  • Hi Stefan,

    How did you get this to work?

    I have spent a few hours on it and am at a loss.

    Can you build a winrt solution using MSBuild and then call this to run/deploy the WinRT solution? Even if I could get it to work running an existing solution (which I cant) I am also struggling to understand how I could call the appx install file without needing admin privileges in powershell for my users.

    Any help with this would be awesome - I am writing in a C# desktop application.

    Cheers

    Brennon


    Expression MVP

    my blog : http://brennonwilliams.com

    Monday, July 29, 2013 2:07 AM
  • Brennon,

    Funnily enough, I have actually been working on this exact problem over the last few days.  With windows 8.1 I now get the application building and starting up (wouldn't even start-up under windows 8), but when it runs I get a Xaml Parsing error when you try and go to a new page.  This makes little sense to me as if I build it directly from visual studio it works...

    So here's my process:
    msbuild the solution

    I then copy the release folder to my own new folder and edit the manifest file with the new title of my particular build.

    I then run makeappx on that new folder.

    I then run signtool on the appx.

    Then, in order to install it, I run a program that I have written that installs the appx (happy to provide this to you off-line if you want it).  This prompts for administrative privileges to do the install.

    Then I try running resulting installed application.  Some reason the moment when I run it it does crash at start-up, but if I run it from the start screen it works, apart from the XAML parsing error trying to go to any page except the first one.

    So I'm really not sure how to solve the xaml problem I will probably have to submit a proper support request.

    As to your particular issue I don't know that you can in anyway install it without requiring administrative privileges.

    Stefan

    Monday, July 29, 2013 3:14 AM