Add/Start/Stop IoT Core application from another application - c#

I would like to add/start/stop/remove an application (IoT Core) just like the web interface or the Power Shell commands: "IotStartup" but from within a C# application running on the IoT Core itself.
Is there a c# API for this or do I have to implement the Power Shell commands inside my application?

Using the Device Portal API could work for you.
Here are the docs.
https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/device-portal-api-core

Device Portal APIs are intended for remote management, and may not work from within a UWP against localhost due to loopback restrictions in Windows (to prevent an app from attacking the device it's running on).
That said, the Windows.Management.Deployment.PackageManager class provides installation and removal APIs for apps. To launch an app, you can use Windows.System.Launcher. Note that PackageManager is a restricted capability, so the app won't be able to go into the Store without special permissions from Microsoft. It will work in sideloaded scenarios though.
There is no way to close an app from another app with existing APIs. However, Launching the app using the above, then connecting with an AppServiceConnection to manage it, you can send a "Close" message over the AppServiceConnection that causes the app to close itself.

Related

How do I create a tray-only windows service controller using .NET Core?

I want to make a windows service worker, as described in this walkthrough: https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service.
I also want the user to see a tray icon to manipulate the service. To do this, I will create a second executable which runs in the user context and communicates with the service.
How should the app communicate with the service to perform things like checking the service status, restarting the service, etc.?
Apparently this could be done with .NET Framework by using a ServiceController in a user-run app to talk to the service (which I guess used to be a ServiceBase instead of a BackgroundService).
What is the equivalent "ServiceController" class, when using the newer BackgroundService/service worker model of .NET Core? Does ServiceController still work?
A typical pattern is to have two apps. You can't really do this. as services run in a different window station than the logged in user, so you can't have a system tray icon for those users. This is from the Microsoft docs https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications:
Windows Service applications run in a different window station than
the interactive station of the logged-on user. A window station is a
secure object that contains a Clipboard, a set of global atoms, and a
group of desktop objects. Because the station of the Windows service
is not an interactive station, dialog boxes raised from within a
Windows service application will not be seen and may cause your
program to stop responding. Similarly, error messages should be logged
in the Windows event log rather than raised in the user interface.
The Windows service classes supported by the .NET Framework do not
support interaction with interactive stations, that is, the logged-on
user. The .NET Framework also does not include classes that represent
stations and desktops. If your Windows service must interact with
other stations, you will need to access the unmanaged Windows API. For
more information, see the Windows SDK documentation.
The interaction of the Windows service with the user or other stations
must be carefully designed to include scenarios such as there being no
logged on user, or the user having an unexpected set of desktop
objects. In some cases, it may be more appropriate to write a Windows
application that runs under the control of the user.
Here are a couple of links about how to write to the system tray I found on internet and other post. You'll need another application to interface with the service, since the service can't directly have an icon in the system tray for windows..
How can I make a .NET Windows Forms application that only runs in the System Tray?
and
http://msdotnetsupport.blogspot.com/2008/02/cnet-application-windows-system-tray.html
Way to communicate with the windows service could be as follows:
-Socket communications (TCP, HTTP, Websocket or other):
-Named pipes: this is a good option for communication between processes running in the same node:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx
-shared memory: this is the fastest
Other higher level, library like SignalR which uses
sockets

Communication channel between a windows app and UWP app

I have a uwp project and a windows app (net5).
Both are installed in the same machine (same will happen in production), but the uwp is installed using sideloading, not on windows store.
The windows app gets some data by listening to a wss port. I have to pass those data from the windows app to the uwp app and also wait for a response back from it.
To do this internal communication I came across the AppServiceBridgeSamples in this repo https://github.com/microsoft/DesktopBridgeToUWP-Samples but it is outdated and I can't use it.
Is there any alternatives to it? What would be the easiest implementation to achieve this?
Any suggestions/comments are appreciated.
Currently, the general way to implement a communication channel between a UWP app and a windows app is by using App service. This is not outdated. If you don't want to use the App service, you have to build your own socket server so that both of your apps could communicate with it. Another way is that your windows app could save the data in a local file and load them in the UWP app.

UWP App design on Windows 10 with Windows Service

We are building an assistant UWP app for Windows 10 & Windows Mobile Devices. On Windows 10 we want to offer some sort of security enforcement to the user, e.g. making sure his antivirus is up to date and so on. This should be done through Powershell with Admin privileges.
Question: What would be the best approach here from architecture & user experience point of view?
Idea 1:
Architecture: UWP app communicates with a service (which is written as .NET Core web.api and offers simple REST Interface) and consumes this service through http client.
The service run's as local system. We want to host it in Kestrel webservice as this is supported when targeting .net 4.6. Prototype worked so far but everything runs in debug mode & not sure if there will be some issues with the isolation of UWP apps.
From my point of view, the UWP app is consuming the service through normal http requests which should not be isolated or intercepted in any way, right? There is no difference on consuming a local hosted webapi instead compared to a cloud api from UWP isolation perspective, right?
Idea 2:
User experience: User downloads the app from public store. App checks if it is running on Windows 10, if so, it checks if the helper windows service is already installed (through a API call on the webservice). If not, it offers the user the ability to download the service from a web page and install it (evaluated prompt).
What would be the best way to do it? Just giving a download link to the user? Or can I start a process from a UWP app that prompts to install an MSI? It should be as simple as possible.

How do I real time communicate a Universal Windows App with a older .Net 4.5 app in the same solution. IPC?

I have been struggling with this for over a month, but am slowly learning. Here is what I have:
C# Solution With 3 Projects
-Universal Windows Platform App (Cortana Voice Support)
::Above just shows a floating PNG image and is Cortana enabled
-Universal Windows Service (Cortana Background Service)
::Needed for the Cortana Support
-.Net 4.5 C# App with HTML DOM Automation
::Automates locating elements and clicking of a website
I can't re-code my .Net app's functionality in my Universal app because some of the libraries it uses won't install. (GhostDriver, PhantomJS, and a HotKey Library provided via NuGet. Installing on the Universal Platform fails on all 3 libraries).
What is needed:
-Master Universal Windows App or Service sends signal to .Net App
-.Net App receives signal and calls method/function providing my functionality.
I've never tried calling methods in other apps within the same solution and was wondering if there was a few-code workaround. What I need I think is a method of inter process communication or events, but can't find any examples of a UWP. Most are .Net communicating with other older non universal apps.
Thank you so much!
If I understand correctly, you were developing a app which will can automate the DOM operation through Cortana.
Do you need to submit this app to App Store?
If no, as Scott said, you can use WCF to communicate between UWP process and .NET 4.5 desktop app process.
If yes, as Andrew pointed out, you need an intermediate remote server hosting the service.

Run desktop app via WEB app

I have desktop application that can be installed on the users computer with "setup" msi file like any other application.
I'm developing web application and I need somehow to integrate the desktop application with the web application. Web application is developed using PHP (desktop application is developed using C#), and when clicking on one button on the web application, the desktop application needs to be launched.
Is there a way of doing this? I was thinking about a few scenarios:
-Maybe if possible to install the app directly on the server and to launch it from there?
-Maybe to be required the user to have the app installed on his computer and to call the app from there?
If possible I would prefer to not use the second approach because it's better if the third party to not be involved - it will be more user friendly if the application is launched directly. But any help will be appreciated because at this point I'm not sure if that is possible to be done at all.
You can install app at server an run it with exec() or "`" operator - but to control gui of app you need to use some like autoit ant etc.
If you ultimately decide to require your users to have the client application installed and expect majority of the users to be on Windows, you could register your application to handle specific scheme and parse its command line when started. More on this here:
http://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
You'd register a custom scheme and then intercept in in the application's command line arguments. The whole URL will be passed as an argument, e.g.:
myapp://parameter1,parameter2
The browser will mostly ask whether or not the user trusts the application to handle this scheme with an option to remember this setting.
If you don't have access to the source code of the client application, you can develop a middleware, some sort of a launcher that handles the URL and then runs the client application after maybe modifying some configuration files based on the URL or otherwise controlling the third-party application to do as you with.
As for solution #1, I don't think C# matters if your application can run on Mono, so you should be able to just run it from PHP. However, this probably won't work on web hosting and you will have to get a VPS for that.
There are very good (security) reasons why it is hard to launch client-side processes from web browsers.
Do you have access to the source code of the C# app? If so, you could consider modifying it to take advantage of Microsoft ClickOnce deployment.
Some references:
http://msdn.microsoft.com/en-us/library/t71a733d.aspx
http://msdn.microsoft.com/en-us/library/t71a733d(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/6ae39a7c.aspx
http://www.codemag.com/Article/0902031
Here's an old article on deploying WinForms applications via ClickOnce with Visual Studio 2005: http://msdn.microsoft.com/en-us/library/ms953320.aspx
It can possibly be done with IIS but it can be cumbersome setting it up to run as the current user, especially since it would probably also need to run elevated for an app that needs to attach to the current user's desktop.
Easiest is to install a Windows LAMP distro (like WAMP: http://www.wampserver.com/en/) and then run the httpd.exe directly from an elevated (Admin) command prompt; do not run apache as a service!
Once you do this performing and exec("command"); call in php will bring up a desktop app as if it was invoked from an admin command prompt. Obviously you need to set up the apache server to be accessible from outside the local system, etc.

Categories