Handling Unmanaged Code with Clarion#

Top  Previous  Next

In .NET technology, unmanaged code is defined as library code that is not managed by the .NET Common Language Runtime. Sometimes, you may need to call into this type of code, primarily in some Win32 libraries, or an external device whose drivers are written for the non-.Net programmer

 

There are two main ways that Clarion# code can directly call unmanaged code:

 

1.

Directly call a function exported from a DLL.

2.

Call an interface method on a COM object

 

For both techniques, you must provide the Clarion# compiler with a declaration of the unmanaged function, and you may also need to provide the Clarion# compiler with a description of how to marshal the parameters and return a value to and from the unmanaged code.

 

The DllImport attribute tells the compiler what DLL the implementation is located in.

 

For every .NET Framework type there is a default unmanaged type in which the common language runtime will use to marshal data across a managed to unmanaged function call. You can override the default marshaling using the MarshalAs attribute in the Clarion# declaration of the unmanaged function.

 

The MarshalAs attribute can be placed on method parameters, method return values, and fields of STRUCTs and classes. To set the marshaling of a method return value, place the MarshalAs attribute in an attribute block on the method with the return attribute location override.

 

Example:

 

 MAP

  MODULE('Win32dll.dll')

!!! <summary>

!!! Prototype for calling a simple Clarion for Windows procedure using the MODULE syntax

!!! Only valid for procedures that do not need to marshal parameters

!!! </summary>

AVerySimpleProcedure  PROCEDURE()

  END

!!! <summary>

!!! A procedure showing that you can return BSTRING data from Clarion# programs

!!! As this procedure needs to marshal parameters, it cannot use the MODULE syntax

!!! <!summary>

BStringData PROCEDURE(CLALONG myLong, [MarshalAs(UnmanagedType.BStr)]String str1, CLALONG l2, [MarshalAs(UnmanagedType.BStr)]String str2),[DllImport('Win32Dll.dll')]

    END

 

 

!!! <summary>

!!! A class for holding prototypes to the win32 procedures

!!! <!summary>

Win32Dll    CLASS

!!! <summary>

!!! A simple procedure that can be called from .NET and receives a LONG

!!! <!summary>

[DllImport('Win32Dll.dll')]

AProcedureWithALong PROCEDURE(CLALONG l),STATIC

 

!!! <summary>

!!! A procedure showing that you can return a LONG to .NET

!!! <!summary>

[DllImport('Win32Dll.dll')]

ReturnALong         PROCEDURE(),CLALONG,STATIC

 

!!! <summary>

!!! A procedure showing that you can get CSTRING data from .NET programs

!!! <!summary>

[DllImport('Win32Dll.dll')]

CStringData         PROCEDURE(String str),STATIC

 

 

!!! <summary>

!!! A procedure showing that you can return BSTRING data to Clarion# programs

!!! <!summary>

[DllImport('Win32Dll.dll')]

[return: MarshalAs(UnmanagedType.BStr)]

ReturnABString      PROCEDURE(), String,STATIC

           END

 

See Also: Platform Invocation Service Support (PInvoke)