Tip: How to use Win32 API Calls or Win32 DLL Functions

In the last days I was looking for a way to realize Win32 API calls without additional packages. Surely this is a rather rare requirement, since almost everything can be handled via the dotNET framework. However, sometimes there are use cases where this may be required. Especially in the environment of older applications there may be Win32 libraries whose functions can be used on this way.

In the main thing the Invoke Code activity calls another C# provider, which offers the possibility to perform a DLLImport.

Here the code, it is more or less a reduced dotNETRunner. It compiles the code, invokes the method of the instance and delivers a result back:

//-UiPath Begin---------------------------------------------------------

CompilerResults CompResult;

//-Setting of the compiler version--------------------------------------
Dictionary<string, string> providerOptions = new Dictionary<string, string> {
  {"CompilerVersion", "v4.0"}

//-Setting of the language----------------------------------------------
CodeDomProvider CodeProvider = new CSharpCodeProvider(providerOptions);

//-Setting of the compiler parameters-----------------------------------
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateInMemory = true;
compilerParams.GenerateExecutable = false;
compilerParams.IncludeDebugInformation = true;
compilerParams.WarningLevel = 3;
string[] assemblies = Assemblies.Split(new char[] { ',' } );
foreach(string Assembly in assemblies) {

//-Checking the code----------------------------------------------------
if(System.String.IsNullOrEmpty(Code.Trim())) {
  ErrorMessage = "Code missed";
//-Compiling of the code------------------------------------------------
CompResult = CodeProvider.CompileAssemblyFromSource(compilerParams, Code);
//-Checking if compiling code exists------------------------------------
if(CompResult == null) {
  ErrorMessage = "Compiling code missed";
StringCollection CompOutput = CompResult.Output;
if(CompResult.Errors.Count != 0) {
  ErrorMessage = "Compilation failed" + Environment.NewLine;
  foreach(string CompOutputLine in CompOutput) {
    if(!string.IsNullOrEmpty(CompOutputLine)) {
      if(!CompOutputLine.Contains("This compiler is provided as part of the Microsoft (R) .NET Framework") &&
        !CompOutputLine.Contains("Copyright (C) Microsoft Corporation. All rights reserved.")) {
        ErrorMessage += CompOutputLine + Environment.NewLine;

//-Read constructor parameters and add it to a list---------------------
object[] ConstructorParams = null;
if(!System.String.IsNullOrEmpty(ConstructorParameters)) {
  string[] partsConstructorParams = ConstructorParameters.Split(new char[] { ',' });
  ConstructorParams = new List<string>(partsConstructorParams).ToArray();

//-Read method parameters and add it to a list--------------------------
object[] MethodParams = null;
if(!System.String.IsNullOrEmpty(MethodParameters)) {
  string[] partsMethodParams = MethodParameters.Split(new char[] { ',' });
  MethodParams = new List<string>(partsMethodParams).ToArray();

//-Checking the instance------------------------------------------------
if(System.String.IsNullOrEmpty(Instance.Trim())) {
  ErrorMessage = "Instance missed";
//-Create instance and invoke method------------------------------------
try {
  object oInstance = CompResult.CompiledAssembly.CreateInstance(
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance,
    null, // use default binder
    null, // use CultureInfo from current thread,
  if(!System.String.IsNullOrEmpty(Method)) {
    MethodInfo MethInfo = oInstance.GetType().GetMethod(Method);
    object oResult = MethInfo.Invoke(oInstance, MethodParams);
    if(oResult != null) {
      Result = oResult.ToString();
} catch (System.Exception ex) {
  ErrorMessage = ex.Message + Environment.NewLine + ex.StackTrace;

//-UiPath End-----------------------------------------------------------

Here a tiny example to call the Win32 API function.

Here the invoked code:

using System;
using System.Runtime.InteropServices;

class Test {

  // Use DllImport to import the Win32 MessageBox function.
  [DllImport(""user32.dll"", CharSet = CharSet.Unicode)]
  public static extern int MessageBox(
    IntPtr hWnd,
    String text,
    String caption,
    uint type

  public static void Main() {
    int Result = MessageBox(IntPtr.Zero, ""Hello World from UiPath via Win32 API Call"", 
      ""MessageBox from Win32 API"", 0);


The only drawback is the necessary doubling of the quotation marks in the string.

And it works as expected.


Maybe not elegant, but on this way functions of Win32 libraries can be used in the context of UiPath, without additional packages.

Here my example project:
DLLImport.zip (33.4 KB)

Enjoy it.


Great work!

1 Like