Re: .NET Framework Interop (CLR, C#, VB)
Posted: 20 Jul 2018, 17:22
Thanks, lexikos, that's enlightening.
Let's help each other out
https://www.autohotkey.com/boards/
Code: Select all
---------------------------
GetScreen.ahk
---------------------------
Error: 0x80004003 - Invalid pointer
Source: mscorlib
Description: Value cannot be null.
Parameter name: source
HelpFile: (null)
HelpContext: 0
Specifically: GetScreen
Line#
004: RunAsAdmin()
006: FileRead,c#,MirrSharp.cs
007: dlls := "System.dll|System.ComponentModel.dll|System.Data.dll|System.Drawing.dll|System.Reflection.dll|System.Windows.Forms.dll|System.Runtime.InteropServices.dll|System.Threading.dll"
008: asm := CLR_CompileC#( c#, dlls )
010: _mirror := CLR_CreateObject(asm, "MirrSharp.Driver.DesktopMirror")
011: _mirror.Load(), Sleep(1000)
012: _mirror.Connect(), Sleep(1000)
---> 014: _mirror.GetScreen().Save("screenshot.png")
016: _mirror.Disconnect()
017: _mirror.Unload()
018: _mirror.Dispose()
020: ExitApp
022: {
023: Sleep,Delay
024: }
Continue running the script?
---------------------------
Yes No
---------------------------
Code: Select all
cSharp =
(
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
public class ps
{
public string RunScript(string scriptText)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
return results[0].ToString();
/*
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
*/
}
}
)
asm := CLR_CompileC#( cSharp, "System.Core.dll | C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" )
ps := asm.CreateInstance("ps")
MsgBox % ps.RunScript("echo hello")
MsgBox % ps.RunScript("Get-Service")
/*
psCode =
(
[Reflection.Assembly]::LoadFile("D:\Desktop\EPPlus.dll") | Out-Null
$xl = New-Object OfficeOpenXml.ExcelPackage
$ws = $xl.Workbook.Worksheets.Add("test")
$ws.Cells["A1"].Value = 123
$ws.Cells["A2"].Value = "test"
$xl.SaveAs("CreateByPS.xlsx")
)
msgbox % ps.RunScript(psCode)
*/
Code: Select all
csharp=
(
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Xceed.Words.NET;
using System.Drawing;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Script
{
[STAThread]
static void Main(string[] args)
{
//Get parameters
string FirstName = "Benjamin";
string LastName = "Adler";
string Username = "Badler";
//Get template
string TemplateFileName = @"test2.docx";
DocX template = DocX.Load(TemplateFileName);
//Replace macros with parameters
template.ReplaceText("<FirstName>", FirstName, false, RegexOptions.Singleline, null, null, MatchFormattingOptions.ExactMatch);
template.ReplaceText("<LastName>", LastName, false, RegexOptions.Singleline, null, null, MatchFormattingOptions.ExactMatch);
//Save with new file name
string newFile = string.Format(@"test_{0}.docx", Username);
template.SaveAs(newFile);
}
}
)
asm := CLR_CompileC#(csharp,"System.dll|System.Drawing.dll|System.Text.RegularExpressions.dll|System.Collections.dll|Microsoft.CSharp.dll|Xceed.Words.NET.dll|System.Windows.Forms.dll", 0, "DahkX4.exe", "/target:winexe")
RunWait, DahkX4.exe
sleep, 100
FileDelete, DahkX4.exe
FileDelete, DahkX4.pdb
ExitApp
Code: Select all
#include CLR.ahk
code =
(
using System.Collections.Generic;
public class ListTest
{
public List<string> GetList()
{
return new List<string>{"One", "Two", "Three"};
}
}
)
asm := CLR_CompileC#(code)
cs := asm.CreateInstance("ListTest")
l := cs.GetList()
lw := new ListWrapper(l)
str := "Count(): " lw.Count() "`n"
str .= "MaxIndex(): " lw.MaxIndex() "`n"
str .= "Length(): " lw.Length() "`n"
for k, v in lw {
str .= "Item " k ": " v "`n"
}
msgbox % str
return
; A class to wrap a List<T> coming from .NET
; Converts 0-based indexes to 1-based, provides enumerator, Count() etc
Class ListWrapper {
__New(list){
this.List := list
; https://www.autohotkey.com/boards/viewtopic.php?p=155453#p155453
this.ICollection := ComObject(9, ComObjQuery(this.List, "{DE8DB6F8-D101-3A92-8D1C-E72E5F10E992}"), 1)
this.IList := ComObject(9, ComObjQuery(this.ICollection, "{7BCFA00F-F764-3113-9140-3BBD127A96BB}"), 1)
}
; Handle index lookups ([1], [2] etc)
__Get(i){
return this.IList.Item(i - 1)
}
; === Enumerator code to handle for loops
; https://www.autohotkey.com/boards/viewtopic.php?f=7&t=7199
_NewEnum(){
this.i := 0
return this
}
Next(ByRef k, ByRef v){
this.i++
if (this.i > this.Count()){
return False
}
k := this.i
v := this[k]
return True
}
; ==== Count, MaxIndex etc
Count(){
return this.ICollection.Count()
}
MaxIndex(){
return this.ICollection.Count()
}
Length(){
return this.ICollection.Count()
}
}
Code: Select all
Class ListWrapper {
__New(list){
this.List := list
; https://www.autohotkey.com/boards/viewtopic.php?p=155453#p155453
this.ICollection := ComObject(9, ComObjQuery(this.List, "{DE8DB6F8-D101-3A92-8D1C-E72E5F10E992}"), 1)
this.IList := ComObject(9, ComObjQuery(this.ICollection, "{7BCFA00F-F764-3113-9140-3BBD127A96BB}"), 1)
}
Code: Select all
asm := CLR.LoadLibrary("System.dll")
DT := CLR.CreateObject(asm, "System.DateTime", 1987, 3, 23)
MsgBox % DT.Ticks
Code: Select all
GetTickCount64()
{
return DllCall("kernel32.dll\GetTickCount64", "UInt64")
}
MsgBox % GetTickCount64()
Code: Select all
StringSplit, Refs, References, |, %A_Space%%A_Tab%
Code: Select all
Refs := StrSplit(References, "|", A_Space A_Tab)
Code: Select all
aRefs := ComObjArray(8, Refs0)
Loop % Refs0
aRefs[A_Index-1] := Refs%A_Index%
Code: Select all
"Message":
"Error CS0234 on line 4: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
Error CS0234 on line 5: The type or namespace name 'Web' does not exist in the namespace 'System' (are you missing an assembly reference?)
Error CS0234 on line 6: The type or namespace name 'EnterpriseServices' does not exist in the namespace 'System' (are you missing an assembly reference?)
Error CS0246 on line 7: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)"
Code: Select all
Refs := StrSplit(References, "|", A_Space A_Tab)
aRefs := ComObjArray(8, totalRefs := ObjMaxIndex(Refs))
loop % totalRefs
aRefs[A_Index-1] := Refs[A_Index]
; print(aRefs[A_Index - 1])