Question Compiler un exécutable

Plus d'informations
il y a 9 ans 5 mois #18280 par Laurent Dardenne
Une démo de compilation de code utilisant sdtin (le pipe du 'DOS') :
[code:1]
#Charge des assemblies WPF selon la version de Powershell
if($PSVersionTable.CLRVersion -gt \"4.0\"«») {
$Language = \"CSharp\" # Version 4
Add-Type -AssemblyName \"UIAutomationClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"
Add-Type -AssemblyName \"UIAutomationTypes, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"
} else {
# In PowerShell 2, we need to use the .Net 3 version
$Language = \"CSharpVersion3\"
Add-Type -AssemblyName \"UIAutomationClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"
Add-Type -AssemblyName \"UIAutomationTypes, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"
}

$Code=@'
//from blogs.msdn.com/b/oldnewthing/archive/2014/09/08/10555736.aspx
using System;
using System.Diagnostics;
using System.Windows.Automation;
using System.Runtime.InteropServices;

class Program
{
static void Main(string[] args)
{
// Slurp stdin into a string.
var everything = Console.In.ReadToEnd();

// Fire up a brand new Notepad.
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = @\"C:\Windows\System32\notepad.exe\";
process.Start();
process.WaitForInputIdle();

// Find the Notepad edit control.
var edit = AutomationElement.FromHandle(process.MainWindowHandle)
.FindFirst(TreeScope.Subtree,
new PropertyCondition(
AutomationElement.ControlTypeProperty,
ControlType.Document));

// Shove the text into that window.
var nativeHandle = new IntPtr((int)edit.GetCurrentPropertyValue(
AutomationElement.NativeWindowHandleProperty));
SendMessage(nativeHandle, WM_SETTEXT, IntPtr.Zero, everything);
}

[DllImport(\"user32.dll\", EntryPoint=\"SendMessage\", CharSet=CharSet.Unicode)]
static extern IntPtr SendMessage(
IntPtr windowHandle, int message, IntPtr wParam, string text);
const int WM_SETTEXT = 0x000C;
}
'@

#Construit la liste des références nécessaires à la compilation
#Il s'agit du chemin des DLL.
$Rf=@(
([PSObject].Assembly.Location),
([System.Windows.Automation.Automation].Assembly.Location),
([System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute].Assembly.location),
([System.Windows.Automation.TreeScope].Assembly.Location)
)

#Compile un programme console
Add-Type -TypeDefinition $Code -OutputAssembly 'c:\temp\PipeToNotepad.exe' -OutputType ConsoleApplication -ReferencedAssemblies $R
[/code:1]
Un exemple :
[code:1]
Dir|C:\Temp\PipeToNotepad.exe
[/code:1]
Avec quelques limites :

Can we be sure that Notepad (or Wordpad, for that matter) properly updates all of its state variables when other programs manipulate its internal controls? Don't turn this Little Program into a Big Program unless you know. CreateWindow(L\"EDIT\", everything, ...) is probably easier and safer.

[Actually, we know that it doesn't. Close the Notepad window without editing anything: You are not prompted to save. -Raymond]


Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.064 secondes
Propulsé par Kunena