.Net Native Interface Library

On this page you can learn more about the .Net Native Interface Library which can be use by any programming language. For more information about the product you are interested in, navigate to one of its information pages.

Download Buy



The .Net Native Interface Library (DotNetNativeInterface) is a native programming interface which provides a way for programming languages such as C, C++, Fortran, Assembly, Pascal, Java, VB etc to interact with applications and libraries written in any of the .Net languages (such as C#, VB.Net etc). Programmers can use DotNetNativeInterface native methods to access .Net 3rd party libraries or .Net Framework Libraries without the need for registry entries, COM registrations or changes to the .Net libraries.

The following examples illustrate when you need to use DotNetNativeInterface native methods:

  • You want to access and use .Net Framework Class Library in your application.
  • You want to access and use 3rd party .Net Libraries in your application.

By programming with the DotNetNativeInterface library, you can use the native methods to:

  • Load .Net assembly from file or Global Assembly Cache (GAC).
  • Create instance of .Net objects (including arrays).
  • Create and use .Net objects.
  • Invoke .Net methods (including generic methods).
  • Get or Set .Net object field and property values (including static members).
  • Subscribe and handle .Net events.
  • Catch and throw .Net exceptions.
  • Load assembly types and obtain type information.
  • ...and many more

Programmers can produce software by combining source codes from any programming language with the .Net framework and other .Net 3rd party libraries using the DotNetNativeInterface methods.

Programs written for .NET Framework execute in a software environment known as Common Language Runtime (CLR) which is an application virtual machine that provides services such as security, memory management, and exception handling.

Competitive Advantages

The following are some of the advantages the .Net Native Interface Library have over it competitors:
  • Full access to .Net Framework Class Library (Including new and emerging .NET technologies).
  • No COM registration of .Net Libraries is required when deployed.
  • Allows any programming language to consume .Net libraries as if they were native code.
  • Easy to use.

DotNetNativeInterface can do so much:

  • Can access and use .Net Framework Class Library (such as Collections, Data Configuration, DataSets, Data Access, Database Connectivity, Diagnostics, IO, Linq, Dynamic Linq, System, Device and Application Management, Networking, Reflections, Security, Encryption, Cryptogrphy, Character Encoding and String Manipulation, XML etc).
  • Can access and use 3rd Party .Net Libraries.
  • Can host the .Net Common Language Runtime (CLR) in any programming language.
  • Can load and access assemblies and their types from any .Net libraries.
  • Can load and access assemblies and their types from Global Assembly Cache (GAC).
  • Can invoke members of the loaded assembly types which includes constructor, fields, properties, methods and events.
  • Can invoke static members of the loaded assembly types which includes constructor, fields, properties, methods and events.
  • Can create instance of .Net object.
  • Can handle .Net exceptions.
  • Can handle, access and invoke .Net events.
  • ...etc.

Example

The following code loads an assembly named "mscorlib" into the current application domain and print the assembly location to the console.
C/C++
int main(int argc, char** argv) {

    ClrAssembly m_assembly = NULL;

    // Load assembly from assembly fullname
    HRESULT m_result = Assembly_Load(L"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", m_assembly);
    if (m_result == CLR_OK) {

        // Get the assembly location
        int m_bufferLen = Assembly_GetLocation(m_assembly, NULL, 0);
        wchar_t *m_buffer = new wchar_t[m_bufferLen + 1];
        // Get the assembly location
        Assembly_GetLocation(m_assembly, m_buffer, m_bufferLen);
        std::wcout << "Assembly Location: " << m_buffer << std::endl;
        delete[] m_buffer;
    }

    return 0;
}

Object Pascal
var
  m_result: HResult;
  m_assembly: ClrAssembly;
  m_bufferLen: Integer;
  m_buffer: PWideChar;
begin
  m_assembly := nil;

  // Load assembly from assembly fullname
  m_result := Assembly_Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089',
    m_assembly);
  if m_result = CLR_OK then
  begin
    // Get the assembly location
    m_bufferLen := Assembly_GetLocation(m_assembly, nil, 0);
    GetMem(m_buffer, m_bufferLen);
    try
      // get assembly location
      Assembly_GetLocation(m_assembly, m_buffer, m_bufferLen);
      Writeln('Assembly Location: '+ string(m_buffer));
    finally
      FreeMem(m_buffer);
    end;
  end;
end.

Deployment

  • Microsoft .Net Framework (Latest versions of Windows have .Net Framework pre-installed)
  • Basic Copy Deployment
    • Your application
    • .Net Native Interface Library (DotNetNativeInterface.dll)

The .Net Native Interface provides a rich set of functions for:

  • loading .Net assembly.
  • finding .Net type in assembly.
  • creating an instance of a .Net object.
  • getting and setting the field values of a .Net object.
  • getting and setting the property values of a .Net object.
  • invoking methods of a .Net object.
  • subscribing to and handling of a .Net object events.
  • creating an instance of a .Net array.
  • getting and setting the array elements.
  • handling .Net exceptions

Load .Net Assembly

The .Net Native Interface provides a rich set of functions for loading 3rd party .Net assemblies or .net framework library.

To load an assembly, call one of the following methods:

  • AppDomain_Load
  • Assembly_Load
  • Assembly_LoadFile
  • Assembly_LoadFrom
  • Assembly_LoadWithPartialName
  • Assembly_UnsafeLoadFrom

Find .Net Type

The .Net Native Interface provides a rich set of functions for finding .Net type in an assembly.

To find a .Net type in an assembly, call one of the following methods:

  • Assembly_FindType
  • Assembly_GetType
  • Type_GetType

Create .Net Object

The .Net Native Interface provides a rich set of functions for creating an instance of a .Net object and also for creating a static class as object.

To create an object or create a static class object, call one of the following methods:

  • NewBaseObject
  • NewObjectNoParams
  • NewObject
  • NewObject{num}
    • Where num represents a number from 1 to 4 which indicates the number of parameters required by the .Net object constructor.
  • NewStaticObject
  • NewGenericObject
  • NewObjectByType

Get/Set .Net Object Field Values

The .Net Native Interface provides a rich set of functions for getting and setting a .Net object instance or static object's field values.

To Get or Set the field values of a .Net object or a static class object, call one of the following methods:

GET Functions:

  • Object_GetFieldValueAsObject
  • Object_GetFieldValueAs{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_GetFieldValueAsString
  • Object_GetFieldValueAsDecimal
  • Object_GetFieldValueAsDateTime

SET Functions:

  • Object_SetFieldObjectValue
  • Object_SetField{PrimitiveType}Value
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_SetFieldStringValue
  • Object_SetFieldDecimalValue
  • Object_SetFieldDateTimeValue

Get/Set .Net Object Property Values

The .Net Native Interface provides a rich set of functions for getting and setting a .Net object instance or static object's property values.

To Get or Set the property values of a .Net object or a static class object, call one of the following methods:

GET Functions:

  • Object_GetPropertyValueAsObject
  • Object_GetPropertyValueAs{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_GetPropertyValueAsString
  • Object_GetPropertyValueAsDecimal
  • Object_GetPropertyValueAsDateTime

SET Functions:

  • Object_SetPropertyObjectValue
  • Object_SetProperty{PrimitiveType}Value
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_SetPropertyStringValue
  • Object_SetPropertyDecimalValue
  • Object_SetPropertyDateTimeValue

To Get or Set the property values of a .Net INDEXER object, call one of the following methods:

GET Indexer Functions:

  • Object_GetPropertyIndexValueAsObject
  • Object_GetPropertyIndexValueAs{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_GetPropertyIndexValueAsString
  • Object_GetPropertyIndexValueAsDecimal
  • Object_GetPropertyIndexValueAsDateTime

SET Indexer Functions:

  • Object_SetPropertyObjectIndexValue
  • Object_SetPropertyStringIndexValue{PrimitiveType}IndexValue
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_SetPropertyStringIndexValue
  • Object_SetPropertyDecimalIndexValue
  • Object_SetPropertyDateTimeIndexValue

Invoke .Net Object Methods

The .Net Native Interface provides a rich set of functions for invoking a method of a .Net object instance or static object.

To invoke a non generic method of a .Net object or a static class object, call one of the following methods:

  • Object_InvokeVoidMethod
  • Object_InvokeObjectMethod
  • Object_Invoke{PrimitiveType}Method
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_InvokeStringMethod
  • Object_InvokeDecimalMethod
  • Object_InvokeDateTimeMethod

To invoke a GENERIC method of a .Net object or a static class object, call one of the following methods:

  • Object_InvokeGenericVoidMethod
  • Object_InvokeGenericObjectMethod
  • Object_InvokeGeneric{PrimitiveType}Method
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_InvokeGenericStringMethod
  • Object_InvokeGenericDecimalMethod
  • Object_InvokeGenericDateTimeMethod

Handle .Net Object Events

The .Net Native Interface provides a rich set of functions for subscribing to a .Net object and handling the event raised.

To subscribe to or unsubscribe from a .Net object or a static class object event, call one of the following methods:

SUBSCRIBE Functions:

  • Object_RegisterEventCallBack
  • Object_RegisterEventCallBackPointer
  • Object_RegisterEventCallBackWithReturn

UNSUBSCRIBE Functions:

  • Object_UnRegisterEventCallBack
  • Object_UnRegisterEventCallBackPointer
  • Object_UnRegisterEventCallBackWithReturn

EVENT ARGUMENTS Functions:

  • EventArgs_GetEventParameters
  • EventArgs_ToClrObject

Create .Net Array

The .Net Native Interface provides a rich set of functions for creating an instance of a .Net array object.

To create an array object, call one of the following methods:

  • Array_NewArray
  • Array_NewArrayByType
  • Array_NewAssemblyArray
  • Array_New{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Array_NewDecimalArray
  • Array_NewDateTimeArray
  • Array_NewObjectArray
  • Array_NewStringArray
  • Array_NewTypeArray

Get/Set .Net Array Elements

The .Net Native Interface provides a rich set of functions for getting and setting the element of .Net array.

To Get or Set the element of a .Net array, call one of the following methods:

GET Functions:

  • Object_GetElement
  • Object_GetElement{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_GetElementString
  • Object_GetElementDecimal
  • Object_GetElementDateTime

SET Functions:

  • Object_SetElement
  • Object_SetElementAs{PrimitiveType}
    • PrimitiveType: Boolean, Byte, Double, Float, Int, Long, SByte, Short
  • Object_SetElementAsString
  • Object_SetElementAsDecimal
  • Object_SetElementAsDateTime

Handle .Net Exception

The .Net Native Interface provides a rich set of functions for handling .Net exceptions.

To handle an exception, call these methods:

  • Exception_GetExceptionForErrorCode
  • Exception_Check
  • Exception_GetClrFatalError
  • Exception_Occurred
  • Exception_ToString
  • Exception_GetMessage

Other Functions

See documentation for more information.


The .Net Native Interface Library supports many programming languages as far as the programming language can LINK to the .Net Native Interface Library and port to the library C/C++ header files or the object pascal files.

Some of the programming languages which can use the .Net Native Interface Library to access any .Net Libraries includes:

  • C/C++
  • FORTRAN
  • Assembly Language
  • Java
  • Pascal
  • Python
  • ....and many more.
  • PHP
  • Ruby
  • JavaScript
  • Delphi
  • C++Builder
  • Go
  • Visual Basic
  • Elixir
  • MatLab
  • Perl
  • R
  • Rust
  • Typescript

Some of the programming IDE's which has been used for testing .Net Native Interface Library includes:

  • C/C++ NetBeans
  • C++Builder
  • Delphi
  • Free Pascal
  • Lazarus
  • Java

Header Files

  • C/C++
  • Object Pascal

Platforms

  • .Net Framework 4.5.2 or higher version.
  • Any programming language Integrated Development Environment(IDE).

Windows Applications

  • Supports 32bit windows applications.

Operating Systems

  • Microsoft® Windows, 32-bit and 64-bit

1.0.0.0 01-Jan-2020 (Official Release)
  • First release of .Net Native Interface Library.
  • Supports .Net Framework 4.5 or higher.
  • Supports 32bit applications ONLY.
  • Supports any programming language.

Source Code Examples

The following examples demonstrate how to use the .Net Runtime Library for Delphi to produce a software by combining pascal source codes with .Net framework and other .Net libraries.

How to write a simple Hello World using .Net Console

How to use AppDomain to Load .Net Assemblies

How to use Activator to Create instance of a .Net Object

How to use Dispatch Activator to Create instance of a .Net Object as Dispatch Interface

How to load external .Net DLL and access the members of loaded assembly types

How to use ClrObject interface to connect to SQL Server

How to use .Net Collections in Delphi

How to use .Net Generic Dictionary in Delphi

How to raise and handle .Net Events in Delphi 1

How to raise and handle .Net Events in Delphi 2

How to use .Net XML Classes to compare two element names

How to use .Net Trace in delphi to indicate the beginning and the end of a program's execution

How to encrypt and decrypt sample data using the .Net RijndaelManaged