Calling Unmanaged DLLs Functions from Powershell

Knowledge Base Article # Q200351

Read Prior Article Read Next Article
Summary This article demonstrates how to access Marvin Test Solutions' unmanaged DLL from PowerShell with example using the GxDmm software package to control and interface with Gx2065.
  
Login to rate article

Introduction

All Marvin Test Solutions (MTS) driver packages, like GxDMM, comes with a Windows DLL and accessible from PowerShell (PS).  This article demonstrates how PS can access MTS unmanaged libraries, like GxDmm.DLL, to interface and control the Gx2065 instrument.

PS is a cross-platform task automation solution built on the .NET Common Language Runtime (CLR) is made up of command-line shell, a scripting language, and a configuration management framework that runs on Windows, Linux, and macOS. All modern versions of Windows operating systems ship with PowerShell installed, allowing access to a readily available tool where we can create scripts for automating tasks without the need of additional IDE (Integrated Development Environment). See resource [1] to learn more about PowerShell.

PowerShell DLL Support

It may be of value to discuss what are unmanaged and managed codes. Unmanaged codes are NOT developed in .NET environment such as Win32 API or C/C++, or COM components, while managed codes like C# are developed in .NET. Managed code execution is managed by CLR. Since MTS DLLs are unmanaged, bridging the gap between the unmanaged code in the DLL and the managed code is necessary. This is done through the concept called Marshalling where the unmanaged data types are converted to managed. See KB article [2] from resources section for more information. MTS software packages include the C# class that takes care of this step. Figure 1 illustrates how PowerShell access the functions in the DLL.

Figure 1: Overview of DLL, C# Class, and PowerShell Interface

Figure 1: Overview of DLL, C# Class, and PowerShell Interface

Process

The simplest way to find PowerShell is through the Windows search bar. The PowerShell ISE (Integrated Scripting Environment) is used to demonstrate interfacing and controlling the Gx2065 instrument. PowerShell ISE provides the environment to develop the script as well as the command line to interact with the instrument.
Figure 2: Launching PowerShell

Figure 2: Launching PowerShell

Figure 3: PowerShell ISE Editor

Figure 3: PowerShell ISE Editor

  1. Import C# Class and Assign to a new Type

  2. Find the folder where GxDMM software is installed and open the GxDmm.cs file.
    C:\Program Files (x86)\Marvin Test Solutions\GxDmm

    Copy the code from the GxDmm.cs. In PowerShell, use the cmdlet Add-Type -TypeDefinition @"paste C# code here"@ , and paste the copied code in between double quotes " ". Make sure to make the class public so it can be accessed from the PowerShell script. We can omit from defining the class in the namespace GxDmmNamespace by removing lines 19, 20, and 382. The example provided in this article have the namespace definition omitted.

    Figure 4: Importing C# Class

  3. Members access

  4. After the class is imported, we need to run the script by pressing (F5) to define the class in the current session. This will allow us to access member functions and variables as we normally would in any managed method from the command-line.
    We can start by declaring some symbols and call GxDmmGetDriverSummary to get the driver summary.


  5. Considerations

  6. The following are considerations to keep in mind when declaring and accessing variables in PS script.
    1. Conversion from C++

    2. Since DLLs could be originating from C++, this may impose a problem in the .NET environment. See resource [3] for reference when converting data types from C++ to C# to ensure compatibilities.

    3. Symbol Declaration

    4. The default for integers is Int32. Uint16, Int16, and others must be explicitly declared. The following are examples of how to declare symbols.
      [Uint16] $unsign16 - unsigned integer16
      [Int16] $sing16 - signed integer16
      [Uint32] $unsign32 - unsigned integer32
      [UInt64] $sing64 - unsigned integer64
      [Int64] $sing64  - signed integer64

      $count = 10
      $arrayMeasurements = New-Object double [] $count - declares an array of double with 10 elements.
      $Summary=[System.Text.StringBuilder]::new()
      $Summary.Length=256

    5. Var Access/[ref]

    6. To access by reference, use [ref] before the symbol when passing it as a parameter.
      $Version and $Status are passed by reference on [GxDmm]::GxDmmGetDriverSummary($Summary, 256, [ref] $Version, [ref] $Status)

Summary

Calling MTS DLLs from PS is fairly straightforward process. The software packages include the C# classes and ready for implementing in PS script.
  • Find out the functions in DLL
  • Create a class to contain prototype functions that are linked to the DLL functions
  • Import the class in PowerShell using Add-Type -TypeDefinition cmdlet (COPY/PASTE)
  • Call the method in the class as you would in any other managed method

Example

Gx2065 Example

Resource

[1]
PowerShell Overview
PowerShell
[2]
Using DLL in .NET environment
KB Article #Q200191
[3]
Data type Conversion
KB Article #Q200210 - Data type Reference table
Article Date 4/5/2023
Keywords PowerShell, DLL, Gx2065


Login to rate article

Read Prior Article Read Next Article