https://github.com/francedot/desktopbridge.extension
Desktop Bridge Extension to call Win32 APIs from UWP
https://github.com/francedot/desktopbridge.extension
Last synced: 4 months ago
JSON representation
Desktop Bridge Extension to call Win32 APIs from UWP
- Host: GitHub
- URL: https://github.com/francedot/desktopbridge.extension
- Owner: francedot
- License: mit
- Created: 2017-05-17T22:01:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-18T10:46:58.000Z (almost 8 years ago)
- Last Synced: 2024-12-29T07:04:46.364Z (4 months ago)
- Language: C#
- Size: 67.4 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Desktop Bridge Extension Library for the UWP
The DesktopBridge.Extension Library allows for Win32 APIs to be called from the UWP (x86, x64) abstracting the standard IPC (Inter Process Comunication) approach in favour of a more testable API Interface.
Nuget package is in the works. If you want to try it out before its release just clone the repo.
### Setup
* Reference **DesktopBridge.Extension.Interface** and **Reference DesktopBridge.Extension.Shared** from the UWP project.
* Modify the project **Package.appxmanifest**:1. Add rescap and desktop namespaces:
```xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"```
```xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"```
2. Add ignorable namespaces: ```IgnorableNamespaces="uap mp rescap desktop"```
3. Add the AppService Extension called DesktopBridgeMiddleware and register the embedded executable for running as a full trust process
```xml
```See the [DesktopBridge.Extension.SampleApp](https://github.com/francedot/DesktopBridge.Extension/tree/master/DesktopBridge.Extension/DesktopBridge.Extension.SampleApp) for further reference.
### API Usage
1. Make the App class inherit the **AppConnectionAware** abstract class in order for the APIs to gain access to the DesktopBridgeMiddleware AppService.
2. In the App **OnLaunched**, call the base class implementation ```base.OnLaunched(e)```. This is responsible for initializing the AppService and launching the registered full trust process.Alternatively, make sure to obtain an instance of the DesktopBridgeMiddleware AppService by overriding the App **OnBackgroundActivated** and pass the **AppServiceConnection** to the **InflateConnection()** method of the **DesktopBridgeExtension** class. Lastly, call **InitializeAsync()** to start the full trust process.
```csharp
///
/// Starts the embedded full trust process
///
///
public async Task InitializeAsync();///
/// Inflates the DesktopBridgeMiddleware App Service Connection
///
/// The DesktopBridgeMiddleware App Service Connection
public void InflateConnection(AppServiceConnection appServiceConnection);
```The **DesktopBridgeExtension** exposes its implementation through its singleton: ```DesktopBridgeExtension.Instance```
### Executing Main Programs
```csharp
///
/// Executes the Main Program
///
/// The code of the Main Program
///
public async Task ExecuteMainProgramAsync(string code);///
/// Executes the Main Program
///
/// The path to a file containing the Main Program relative to the installed location
///
public async Task ExecuteMainProgramFromFileAsync(string path);```
#### Example: Execute a Console Main Program```csharp
var code =
@"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello Desktop Bridge!"");
Console.ReadLine();
}
}
}";await DesktopBridgeExtension.Instance.ExecuteMainProgramAsync(code);
```
### Executing Code Scripts
```csharp
///
/// Executes the Code Script
///
/// The code of the Script
///
public async Task ExecuteScriptAsync(string code);///
/// Executes the Code Script
///
/// The path to a file containing the code of the Script relative to the installed location
///
public async Task ExecuteScriptFromFileAsync(string path);///
/// Executes the Code Script and returns a result value
///
/// The Type of the expected Result
/// The code of the Script
/// The evaluated result
public async Task ExecuteScriptAsync(string code);///
/// Executes the Code Script and returns a result value
///
/// The Type of the expected Result
/// The path to a file containing the code of the Script relative to the installed location
/// The evaluated result
public async Task ExecuteScriptFromFileAsync(string path);
```Additional informations can be passed with the Script by using the following set of Fluent APIs.
#### Passing Parameters to a Script
```csharp
///
/// Adds a Parameter to be passed as a part of the Script
///
/// The Type of the Parameter
/// Name of the Parameter
/// Value of the Parameter
///
public DesktopBridgeExtension WithParameter(string paramName, TParam paramValue);```
#### Passing Using directives to a Script
```csharp
///
/// Adds a Using directive to be passed as a part of the Script
///
/// The using namespace (without the using keyword)
///
public DesktopBridgeExtension WithUsing(string @using);///
/// Adds a collection of Using directives to be passed as a part of the Script
///
/// A collection of using namespaces (without the using keyword)
///
public DesktopBridgeExtension WithUsing(IEnumerable usings);```
#### Passing Assembly References to a Script
```csharp
///
/// Adds an assembly path to be referenced by the Script
///
/// Path to the assembly relative to the project folder
///
public DesktopBridgeExtension WithReference(string referencePath);///
/// Adds a collection of assembly paths to be referenced by the Script
///
/// Paths to the assemblies relative to the installed location
///
public DesktopBridgeExtension WithReference(IEnumerable referencePaths);```
#### Example 1: Start a new Process passing its executable path
```csharp
var code =
@"Process cmd = new Process
{
StartInfo = { FileName = path }
};
cmd.Start();";await DesktopBridgeExtension.Instance.WithParameter("path", "myExe.exe").
ExecuteScriptAsync(code);```
#### Example 2: Show a Notify Icon in the Taskbar
```csharp
var code =
@"var notifyIcon = new NotifyIcon
{
BalloonTipText = @""Hello, NotifyIcon!"",
Text = @""Hello, NotifyIcon!"",
Icon = new Icon(""NotifyIcon.ico""),
Visible = true
};
notifyIcon.ShowBalloonTip(1000);";await DesktopBridgeExtension.Instance.WithUsing("System.Drawing").
WithUsing("System.Windows").
WithUsing("System.Windows.Forms").
WithUsing("System.Diagnostics").
WithUsing("System.Runtime.InteropServices").
ExecuteScriptAsync(code);
```