【发布时间】:2015-01-21 14:01:20
【问题描述】:
我正在尝试在 PowerShell 中使用 Windows.System 命名空间,它是 WinRT 的一部分。
程序集不是dll,而是winmd。将程序集加载到 PowerShell 中的两种方式似乎都不起作用
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd")
#Add-Type -Path "C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd"
我知道使用 Windows API 比加载我的 .NET 代码要复杂一些。我有 pinched 使用部分 win32 的代码,WinRT 解决方案是否类似?
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"@
}
Register-NativeMethod "user32.dll" "int MessageBox (IntPtr hWnd, string text, string caption,int type)"
Add-NativeMethods
[NativeMethods]::MessageBox(0, "Please do not press this again.", "Attention", 0)| Out-Null
我的目标是在 PowerShell 中运行 Windows.System.Launcher.LaunchFileAsync("C:\file");。如何加载我需要的 WinRT 组件?
【问题讨论】:
标签: windows powershell windows-8 windows-runtime