1

I'm playing around with powershell and C#. Specifically I'm using the Add-Typecommand to compile C# code to a .dll file. But here's the problem: to run the .dll It would need either an entry point or an export as I understand it, to call functions from it.

To create these exports I found the UnmanagedExports Package. With this I was able to create some exports. Next I wanted to compile this as a .dll file and try to run it. Let me give you an example code.

$code = @'

using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using RGiesecke.DllExport;

namespace HelloWorldll
{
   class Program
   {

        [DllExport("DllMain", CallingConvention = CallingConvention.Winapi)]
        public static void DllMain()
        {
            Console.WriteLine("Hello World");
        }
   }
}
'@

$FilePath = "C:\Some\Path\To\File.dll"
Add-Type -TypeDefinition $code -OutputAssembly $FilePath

The problem is the RGiesecke namespace is not recognized in Powershell, So I had the main question is.

Is there another way to Create/Define Exports WITHOUT 3rd party software/tools? Is there a way/command to install the package INSIDE THIS POWERSHELL SCRIPT before compiling to make sure this package is found?

I also read this but didn't help me as much so perhaps someone could pick up ideas to help me from here but so far I have none. install NuGet via PowerShell script

Current Error Compiling:

Add-Type : c:\Users...\AppData\Local\Temp\ahkmqrli.0.cs(6) : The typen or namespace name 'RGiesecke' could not be found (are you missing a using directive or an assembly reference?)

Community
  • 1
  • 1
Tom Kustermans
  • 521
  • 2
  • 8
  • 31
  • Have you tried specify the assemblies defining the external types using `Add-Type -ReferenceAssemblies name,name,…`? – Richard Apr 20 '16 at 14:01
  • Not yet, I tried but don't think I'm doing this correctly. Would the command look like `Add-Type -TypeDefinition $code -OutputAssembly $FilePath -ReferencedAssemblies $Package ` ? cause that gives me an error. – Tom Kustermans Apr 21 '16 at 07:03

0 Answers0