General description
The CIFX API is based on a native C / C ++ library. There are no libraries for other development and / or runtime environments.
In order to access such "unmanaged" libraries or functions from a .NET application, the so-called P/Invoke framework can be implemented.
This application note describes the approach when using the P/Invoke framework with regard to the use of the CIFX API.
It essentially relates to the example implemetations.
Examples are available here:
[DllImport("cifx32dll.dll", EntryPoint = "<native-function-name>")] private static extern UInt32 _myFunction( ... );
CIFX API
Handles
Like when using the CIFX-API with C / C ++, working with different handles is necessary.
Therefore pointers are transferred to the corresponding function parameters.
In .NET this is not possible, since no pointers are available.
For this purpose, the P / Invoke framework provides the data types IntPtr and UIntPtr, which represents a pointer within the "managed" context of a .NET application.
As with an application implemented in C / C ++, the handles must be opened using the open functions of the CIFX API and continue to exist during the runtime of the application (garbage collection).
Before ending the application, the handles should be closed again, using the corresponding close functions.
x68 versus x64
The application or the wrapper can be compiled for both (64bit and 32bit) systems.
Depending on the target system, it's only needed to load the correct DLL of the CIFX driver. Both DLL versions are provided in the CIFX driver package.
Performance & PLC functions
In principle, the PLC functions are intended for high-performance applications with high timing requirements. The use of .NET itself is less suitable for such requirements.
Windows is not real-time capable by default and does not offer the possibility of influencing the time behavior of a process sufficiently.
With the .NET runtime, there is another software layer that makes the timing behavior even less predictable.
The use of .NET is not recommended for applications in this context. Accordingly, the PLC functions of the CIFX API are not supported in the C # framework.
Hilscher - C# Packet
Hilscher provides a VisualStudio project that implements the P/Invoke frame.
Link to Demo Project:
Within the cifXUser class, all CIFX API functions are made usable in the project. In addition, some standard structures of the CIFX API and constants are available.
Ideally, this demo can be used as a starting project for application development.
Using an example project, the demo also shows the integration of the wrapper DLL into an application.
Note
With .NET or VisualStudio it is possible to use both C # .NET and VB.NET projects within a VisualStudio solution.
To do this, a project of the corresponding type can simply be added: File → Add → New Project.
C# .NET - Access Violation Exception
A common issue while using C # .NET in conjunction with the CIFX libraries is the "Access Violation Exception". It occurs when using the wrong pointer type in a 64-bit application:
"In .NET there is an integral data type, not widely known, that is specifically designated to hold 'pointer' information: IntPtr whose size is dependent on the platform (eg, 32-bit or 64-bit) it is running on. "
public void SizeOfIntPtr() {Console.WriteLine( "SizeOf IntPtr is: {0}", IntPtr.Size );}
Executed on a 32-bit platform, you get the following output:
SizeOf IntPtr is: 4
On a 64-bit platform the output looks like this:
SizeOf IntPtr is: 8"
Lösung
In the old code, some function parameters were set as 32-bit parameters.
In the new version, the pointers described above are used instead, in order to work for both 32-bit and 64-bit applications.
[DllImport("cifx32dll.dll", EntryPoint = "xChannelOpen")] private static extern UInt32 _xChannelOpen( UInt32 hDriver, [MarshalAs(UnmanagedType.LPStr)] string szBoard, UInt32 ulChannel, [MarshalAs(UnmanagedType.U4)] ref UInt32 phChannel);
[DllImport("cifx32dll.dll",EntryPoint = "xChannelOpen")] private static extern Int32 _xChannelOpen ( IntPtr hDriver, [MarshalAs(UnmanagedType.LPStr)] string szBoard, UInt32 ulChannel, ref IntPtr phChannel);