.NET Framework Interop (CLR, C#, VB)

Post your working scripts, libraries and tools
Posts: 981
Joined: 22 Jan 2017, 19:37

Re: .NET Framework Interop (CLR, C#, VB)

20 Jul 2018, 17:22

Thanks, lexikos, that's enlightening.
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: .NET Framework Interop (CLR, C#, VB)

08 Apr 2019, 08:38

I have a post at "Ask For Help" section
But, got nothing
If I was lucky enough, I could have some Help here, as a last resort.

It is related mainly SQLite, C# and lastly AHK
It is my first trying, to grab records count, the easiest one in that field.

SQLite via C# got an Error

I'm suspect, there should be some procedure hidden to me. I do not know what that is though clearly.

Posts: 176
Joined: 26 Mar 2019, 10:02

Re: .NET Framework Interop (CLR, C#, VB)

08 Apr 2019, 08:43

I have wrote very simple codes

One code is to make a MS Word file (docx) without using its own application, it is using OpenXML style.
The other is to make a MS Excel file (xlsx) without using its own application, it is using OpenXML style too.

AHK/CLR/OpenXml - Office documents w/o MS Excel

AHK/CLR/OpenXml - Office docs w/o Office (not main post, but a comment at the 2nd)
Posts: 1412
Joined: 01 Oct 2013, 14:57

Re: .NET Framework Interop (CLR, C#, VB)

19 Jun 2019, 02:21

I got an error when running from 64-bit AutoHotkey: (works fine from 32-bit AutoHotkey.)

Code: Select all

Error:  0x80004003 - Invalid pointer
Source:		mscorlib
Description:	Value cannot be null.
Parameter name: source
HelpFile:		(null)
HelpContext:	0

Specifically: GetScreen

	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   
The error was caused by line 421 of the file MirrSharp.cs:
Marshal.Copy(getChangesBuffer.UserBuffer, data, 0, bytes);
Because when I put return result; above it, error disappeared, but put below it, the error occured.

You'll need to install DFMirage driver first to run the script.
The code of MirrSharp.cs was copied from http://www.demoforge.com/sdk/mirrsharp.zip.

Appreciate any help.
(5.35 KiB) Downloaded 15 times
Posts: 4
Joined: 22 Jul 2018, 14:19

Re: .NET Framework Interop (CLR, C#, VB)

18 Jul 2019, 09:49

Hello, I am not an expert in .NET/CLR internals, maybe my question is dumb. :oops:

How does the compiler know where to lookup for DLL in the References list?

I am asking because I searched on my computer and I have 19 instances of WindowsBase.dll, and 10 instances of DocumentFormat.OpenXml.dll
Until now AHK script is able to load them only if they are copied into the current folder - which is not feasible for my case as you can see.

I installed DocumentFormat.OpenXml.dll using version 2.9.1 using NuGet.
I would like to load required references in a standard/reliable manner and not specifying the path manually. :?
Posts: 6445
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: .NET Framework Interop (CLR, C#, VB)

19 Jul 2019, 23:44

It may depend on whether you specify a full path, version number, etc. and which version of the framework you have loaded. A reference may bind to an assembly which is already loaded (such as System.dll), or the assembly may be loaded from the Global Assembly Cache or from a file elsewhere. See How the Runtime Locates Assemblies | Microsoft Docs.
Posts: 1412
Joined: 01 Oct 2013, 14:57

Re: .NET Framework Interop (CLR, C#, VB)

Today, 02:52

Example: Run PowerShell Script without powershell.exe

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();

		Pipeline pipeline = runspace.CreatePipeline();

		Collection<PSObject> results = pipeline.Invoke();

		return results[0].ToString();
		StringBuilder stringBuilder = new StringBuilder();
		foreach (PSObject obj in results)

		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"
msgbox % ps.RunScript(psCode)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: julesverne, MaxLaska and 47 guests