STLplus 3.8 Released

This is an update incorporating a number of small changes and bug-fixes.

See http://stlplus.sourceforge.net/stlplus3/docs/changes.html for more details.
Posted in Announcements, C++, Software | Comments Off

C# – calling C functions – C# callbacks

The final tutorial in this series on calling C functions from C# covers the problem of how to pass a C# callback function to a C function. This is needed if you are using a C DLL and there are some exported functions in the C library that require callback functions as parameters.

C# allows you to pass a C# method as a C function. This has the benefit that the C# method has access to its parent object and any data fields or other methods.

As with previous tutorials, the key to this method is to use delgates which have been marshalled to match the parameter profile of the C callback and the C function that uses the callback.

The first step is to look at some C declarations for a function that requires a callback.

typedef int (message_callback*) (char* message);
int register_message_callback(message_callback);

So, there is a type definition that declares the parameter profile of a function that can be used as a message callback. It says that the callback must take a C string (char*) as a parameter and return an int.

The second declaration is of a function that takes such a callback function as a parameter and uses it.

Lets say that the register_message_callback is declared in a C DLL which needs to be loaded into the C# program and then given a C# method as its parameter. How is this done?

The first stage is to declare a delegate that represents the type of a function with each parameter translated into an equivalent C# type:

public delegate int message_callback_delegate(string data);

This is in effect a type definition of a method with this particular parameter profile. However, this does not match the C typedef for the callback. It needs marshalling attributes:

[return: MarshalAs(UnmanagedType.I4)]
public delegate int message_callback_delegate(
   [MarshalAs(UnmanagedType.LPStr)] string data);

The LPStr marshalling attribute maps the C# string onto a char* C parameter.

I can then write a message callback in C# with the same parameter profile as the delegate just declared:

public int message_callback(string message)
{
    ...
}

The next stage is to declare a delegate type for the function that registers the callback:

private delegate int register_message_callback_t(message_callback_delegate callback);

This then also needs to have marshalling attributes added to match up the calling conventions and type profiles of the C function:

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.I4)]
private delegate int register_message_callback_t(
    [MarshalAs(UnmanagedType.FunctionPtr)] message_callback_delegate callback);

Now it is possible to use the dynamic loader written in a previous article to load the C library, load the register function and then call it:

dynaloader loader = new dynaloader("mylibrary.dll");
register_message_callback_t register_message_callback =
    loader.load_function("register_message_callback");
register_message_callback(message_callback);

So, the first line loads the DLL into memory. The second loads the register function into memory and associates the function with the variable register_message_callback. This is of the delegate type register_message_callback_t which is effectively a pointer to the function, with strong typing onto the correct parameter profile. The third line calls this function, passing the name of our C# method message_callback as its parameter. This C# method will then be used by the DLL as its message callback.

Posted in C#, Software, Tutorials | Leave a comment

C# – calling C functions – marshalling structs

A Previous Post explained how to marshall simple parameters so that C# code can call C functions. The marshalling ensures that all the conversions to get from C# to C types on calling the function, then back to C# types on return or out parameters, is done for you.

The next step in complexity is to be able to call C functions that take structs as arguments. In this case, each field in the struct needs to be marshalled in order to map it onto the correct C type and to get the conversions to/from the C type. For example, a char[] C field in the struct can be mapped onto a C# string.

To start off, here’s a C struct:

struct MessageStruct
{
  char[11] MsgID;
  char[501] MsgText;
  char IsAlert;
  char Status;
};

And here’s the function that takes such a struct as an argument:

int GetUsrMsg(int Index, MessageStruct* UserMsg);

In this case, an uninitialised struct is passed into the function, that then fills it in.

To call this function from C#, first we need to define a C# struct that is marshalled to have the same fields and field sizes as the C version. This takes some attention to details because it is very easy to get a size wrong.

First, create a C# struct with fields defined using C# types that are suitable for mapping onto the C types.

public unsafe struct MessageStruct
{
    public string MsgID;
    public string MsgText;
    public char IsAlert;
    public char Status;
}

Note that this is declared unsafe because it is used with an unsafe C function.

So, char arrays are mapped onto strings and char is mapped onto char. However, a char in C# is a Unicode char 16-bits wide whereas in C it is an ASCII char 8-bits wide.

To ensure that the char types are mapped correctly, an attribute is attached to the struct to control the mapping.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct MessageStruct
{

This attribute declares a number of parameters that control the mapping of C# types onto the bare bytes of the C type.

The Sequential value tells the marshaller that the fields in the C struct are laid out in the same order as the C~ struct. An alternative is to specify the exact byte position of each field.

Then the CharSet value indicates that the underlying C struct uses Ansi characters, so the marshaller must convert from the C# unicode characters to Ansi on in parameters and vice-versa on out parameters.

Finally, the Pack value indicates how the C types are packed into words. Each field is packed to a multiple of this parameter. The deault is 4, so that each field is guaranteed to start on a word boundary. However, in this library, the C structs have been packed down to individual bytes, thus a pack value of 1 is used.

The next stage is to marshall each of the fields by adding a MarshallAs attribute to each one:

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string MsgID;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 501)]
    public string MsgText;

    [MarshalAs(UnmanagedType.U1)]
    public char IsAlert;

    [MarshalAs(UnmanagedType.U1)]
    public char Status;

The marshalling attributes map the field of the C# type onto the corresponding C type. Take the first field. This is a char[11] in the C struct. It has been mapped onto a string and then the string has been marshalled as a ByValTStr with a size of 11. The ByValTStr is a C character array which is embedded in the struct, i.e. a char[11] rather than a char* which points to a separate character buffer.

The char fields have also been marshalled onto a one-byte unsigned type.

The next stage is to marshall the C function that takes this struct as a parameter.

Remember the C function definition:

int GetUsrMsg(int Index, MessageStruct* UserMsg);

So, as with previous tutorials, the first step is to declared a delegate type to represent the function’s type with each parameter marshalled onto the C types.

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.I4)]
private delegate int GetUsrMsg_t(
    [MarshalAs(UnmanagedType.I4)] int index,
    out MessageStruct message);

Note that the message parameter is an out parameter, meaning that there is no need to initialise it and it will be passed by reference.

Then, using the dynamic loader class, the function of this name can be loaded from the DLL and called:

dynaloader loader = new dynaloader("myclib.dll");
GetUsrMsg_t GetUsrMsg = loader.load_function("GetUsrMsg");
MessageStruct message;
int message_status = GetUsrMsg(0, out message);

So, the first two lines use the dynamic DLL loader to load the C DLL. Then the relevant function is loaded from the DLL. The third line declares a bare MessageStruct to use in the function call. The last line calls the function, passing the struct as an out parameter.

Posted in C#, Software, Tutorials | 1 Comment

C# – calling C functions – marshalling parameter types

The Problem

I am using a DLL provided for me, written in C and I want to call its functions from C#. I have explained in a previous post how to dynamically load a function from a DLL and map it onto a C# delegate so it can be called from C#.

However, this is only part of the problem. The other part is how to map the C types of the function parameters and return value onto C# types so that data can be passed backwards and forwards between the two languages.

The Solution – Marshalling

The solution that Microsoft provide is called Marshalling and is provided by classes in the System.Runtime.InteropServices namespace.

The mashalling mechanism allows the calling conventions and the parameter types of the C function to be specified and, furthermore, to automatically be type converted. So, for example, here’s a C function:

char* ErrorMessage(int id);

In order to dynamically load and call this function from C#, I first declare a delegate with marshalling attributes applied:

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.LPStr)]
private delegate string ErrorMessage_t([MarshalAs(UnmanagedType.I4)] int id);

As explained in the earlier article, this delegate declaration effectively declares a function type. The actual function can then be loaded and converted to this type and then called.

The attributes (i.e. the code in square brackets []) need further explanation.

First, the function itself is declared as an UnmanagedFunctionPointer – this is letting the system know that the function is not managed, so for example it will not be garbage collected. It also says that parameters are to by passed using the WinAPI parameter-passing convention.

The next attribute marshals the return type as unmanaged type “LPStr”. This is a pointer to a C string and corresponds to the char* type in C. The unmanaged part of the marshalling states that the memory associated with the return value will not be garbage collected. In this case the function returns a pointer to an internal buffer and so doesn’t need to be collected anyway.

Note that the return type of the C# delegate is “string”, i.e. a C# string type. The return value from the function will be automatically converted from char* (a byte-wide null-terminated character buffer) to string (a 16-bit wide unicode C# string).

The function parameter itself is of C type int and is marshalled as unmanaged type “I4″. This is unnecessary since the C type int is the same as the C# type int, but I have included it for completeness. Note how the marshalling attribute prefixes the parameter declaration.

Once you have such a delegate declaration, you can now dynamically load a function from a DLL and map it onto the delegate so it can be called from C# as a previous post explained.

Posted in C#, Software, Tutorials | Leave a comment

C# – building a dynamic DLL loader

Following on from the previous C# article – dynamic loading of a C DLL at run-time, the next step is to make the dynamic loading functions into a class.

The class will be in global scope for this example – in practice I might put it into a namespace.

The class manages the DLL so that it can be unloaded or unloaded at any time. Since the DLL is an unmanaged assembly, the class will need a destructor to unload the DLL as well as a constructor to load it. Also, it is good practice to allow for deferred loading – that is, an instance of the class can be constructed without loading its associated DLL, then the DLL can be loaded on demand later.

So, the outline class looks like this:

class dynaloader
{
    // construct an unloaded object
    public dynaloader()
    {
    }

    // construct a loaded object
    public dynaloader(string dll_name)
    {
    }

    // destructor
    ~dynaloader()
    {
    }

    // test whether this object is unloaded or loaded
    public bool loaded()
    {
    }

    // load the DLL
    public bool load(string name)
    {
    }

    // unload the DLL
    public bool unload()
    {
    }

    // Generic method for loading a function from the DLL
    public T load_function<T>(string name) where T : class
    {
    }

}

So, these functions now need implementing using the external WIN32 functions to load/unload the DLL and to load functions from the DLL.

The class needs to contain a handle that points to the DLL, i.e. contains it’s unmanaged address. This is managed by the constructors/destructor in conjunction with the load/unload methods:

using System;
using System.Runtime.InteropServices;

class dynaloader
{
    private IntPtr m_dll = IntPtr.Zero;

    public dynaloader()
    {
    }

    public dynaloader(string dll_name)
    {
        load(dll_name);
    }

    ~dynaloader()
    {
        if (loaded()) unload();
    }

    public bool loaded()
    {
        return m_dll != IntPtr.Zero;
    }

    [DllImport("kernel32.dll")]
    private static extern IntPtr LoadLibrary(string dllToLoad);

    public bool load(string name)
    {
        if (loaded()) unload();
        m_dll = LoadLibrary(name);
        if (!loaded())
            return false;
        return true;
    }

    [DllImport("kernel32.dll")]
    private static extern bool FreeLibrary(IntPtr hModule);

    public bool unload()
    {
        if (!loaded()) return true;
        if (!FreeLibrary(m_dll))
            return false;
        m_dll = IntPtr.Zero;
        return true;
    }

}

The final stage is to provide the generic method for loading a function from the DLL:

class dynaloader
{
    ...

    [DllImport("kernel32.dll")]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    public T load_function<T>(string name) where T : class
    {
        IntPtr address = GetProcAddress(m_dll, name);
        if (address == IntPtr.Zero)
            return null;
        System.Delegate fn_ptr = Marshal.GetDelegateForFunctionPointer(address, typeof(T));
        return fn_ptr as T;
    }

}

In this case, the method returns null on failure, but it could instead throw an exception.

Posted in C#, Software, Tutorials | 1 Comment

VHDL for Logic Synthesis – 3rd Edition Published

Author's copies

VHDL for Logic Synthesis 3rd Edition

I now have my author’s copies of VHDL for Logic Synthesis 3rd Edition (in fact I’ve had them for a while), confirming that it is now out there and available at bookshops.

To buy the book, go to:

 

Posted in VHDL, VHDL for Logic Synthesis | Leave a comment

C# – dynamic loading of a C DLL at run-time

The Problem

I am working on a C# project which needs to load one of a set of DLLs at run-time. Indeed, it needs to be able to load and unload them, possibly then loading a different one. The problem is similar to that of supporting plugins.

The DLLs are supplied by another company, so I have no control over them. They are written in C and export a set of C functions.

A Solution

Surprisingly, after a long search I discovered that C# does not seem to support dynamic linking. So in order to solve this problem, I need to use the dynamic linking functions supported by the WIN32 API, a set of low-level C functions.

The first step then was to create bindings to the WIN32 functions so that they are linked into the application. This is done using the Platform Invoke services provided in the namespace System.Runtime.InteropServices, and documented on MSDN.

using System;
using System.Runtime.InteropServices;

static class win32
{

    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);

}

The DllImport attribute tells the linker which external library to load the functions from.

When mapping from C# to C, it is necessary to “Marshall” the parameters to map C# types onto C types. However, these functions work as they stand since the default marshalling of the parameters is correct. I will revisit the marshalling issue in another article.

These functions perform the following tasks:

LoadLibrary
Loads the DLL into memory and returns a raw address of the DLL’s handle. If the DLL cannot be found, returns IntPtr.Zero.
GetProcAddress
Loads a function by name from the DLL. Returns a raw address of the function. If the function cannot be found, returns IntPtr.Zero.
FreeLibrary
Releases the DLL loaded by the LoadLibrary function.

The LoadLibrary and FreeLibrary functions can now be used directly in C#. However, the GetProcAddress function returns a function pointer and these cannot be called directly from C#. The trick is to convert the function pointer into a delegate using the GetDelegateForFunctionPointer method from the System.Runtime.InteropServices.Marshall class. The code for loading a function and converting it into a delegate is:

using System;
using System.Runtime.InteropServices;
...
IntPtr address = win32.GetProcAddress(m_dll, name);
System.Delegate fn_ptr = Marshal.GetDelegateForFunctionPointer(address, typeof(T));

In this case, the type T is the type of the delegate to convert to. Sadly, the GetDelegateForFunctionPointer method returns a System.Delegate rather than a T. However, this can now be wrapped into a generic method that does return the right type:

class plugins
{
    public T load_function<T>(string name) where T : class
    {
        IntPtr address = win32.GetProcAddress(m_dll, name);
        System.Delegate fn_ptr = Marshal.GetDelegateForFunctionPointer(address, typeof(T));
        return fn_ptr as T;
    }

}

The last line upcasts the System.Delegate onto type T. Of course, the real version has some error checking – for example you could throw an exception if the function wasn’t found.

So what is type T? This is a delegate type declaration for the C function. For example:

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int Ready_t();

The attribute tells the compiler that this is an unmanaged function (no garbage collection etc.) and that it uses Windows calling conventions. In this case, the DLL providers have told me in their documentation that their DLLs use Winapi calling conventions.

Note that I have used the delegate keyword to indicate that this is a delegate type. I have used the name Ready_t to indicate that this is the type declaration of the function and not the actual function.

Again, in this case, the return type int already marshalls correctly using the default conversions so there are no marshalling attributes on the function parameters. Marshalling will be dealt with in another article.

The final stage to get a callable C# function is to use this delegate type as the T parameter of the generic load_function method:

Ready_t Ready = load_function<Ready_t>("Ready");
int ready_status = Ready();

The first line loads a function called “Ready” of type Ready_t. The second calls the function as if it were a C# method.

Posted in C#, Software, Tutorials | Leave a comment

VHDL for Logic Synthesis – Case Study Code

In Chapter 15 of VHDL for Logic Synthesis (3rd edition) I work through a case study of a low-pass digital filter.

The source code for the filter is downloadable below:

This design is then reworked as a generic filter core which can then be used to implement the low-pass filter:

Posted in Examples, VHDL, VHDL for Logic Synthesis | Leave a comment

Shell scripting tutorials – more prefix commands

The previous article on shell scripting of prefix commands introduced the subject, introduced some pre-defined prefix commands and then showed how to write a basic prefix command using the sleep-before command as an example.

This article continues in the same vein, building a set of prefix commands and then showing how to combine them.

The Wait-After Command

Another useful prefix command is the wait-after command which waits for user input when a command has completed. I use this for background scripts running in command windows to stop the shell from exiting and thereby closing the window before I’ve read any output messages. Pressing return continues the script, exits the shell and closes the window.

Much of the code for this is similar to the sleep-before command, assembled in a slightly different order. Here is the usage function that also explains the options I want to make available:

function usage()
{
  cat <<-EOF
 usage: $0 [ -e ] [ -p <prompt> ] <command>
   a prefix operator that executes <command>
   then waits for input afterwards before exiting
   <command>   : the command-line to execute in the background
   -e          : only wait on error
   -p <prompt> : the prompt printed on wait
 EOF
}

So, there are two options (apart from the -h for help, which is always implied). One is a switch that says ony wait if there was an error, otherwise exit immediately. The other is a value parameter that allows the prompt displayed on the pause to be customised.

As always, the command-line options are processed by the getopts command:

# process command-line
error=0
error_only=0
prompt="press return to finish"
while getopts p:eh option; do
  case $option in
    p)
      prompt=$OPTARG
      ;;
    e)
      error_only=1
      ;;
    h)
      usage
      exit 0
      ;;
    \?)
      error=1
      ;;
  esac
done
shift $((OPTIND-1))
# check for the existence of a script to run
if [ $# -eq 0 ]; then
  echo "$0: a command to run is required"
  error=1
fi
if [ $error -gt 0 ]; then
  usage
  exit 2
fi

Note how the switch and value parameters have been handled and the processed options are discarded by the shift after the end of the loop.

There is then a requirement that there is at least one more command-line parameter representing the command to be run, and finally a catch-all handler for errors that prints the usage and exits with the exit code 2 which is the conventional code for incorrect usage.

The next stage is to run the command itself and capture its exit status:

"$@"
status=$?

As in the previous article, the command is run by executing the remaining command-line parameters "$@" and then capturing the value of the special variable $?.

The penultimate part of the script is to now wait for user input: if the -e option is present only wait on error, otherwise always wait:

if [ $status -eq 0 ]; then
  if [ $error_only -ne 1 ]; then
    echo -n "SUCCESS: $prompt..."
    read response
  fi
else
  echo -n "ERROR: status=$status: $prompt..."
  read response
fi

The echo commands print the prompt to standard output, without a newline in this case, and then the read commands will wait for user input. All user input will be read until a return is entered, at which point the read command completes and the script continues.

The final stage is to exit with the same exit status as the command that was run before the wait:

exit $status

The Log Command

The final example of a prefix command that I will demonstrate is the log command. This is used to save the output from a command to a log file while still displaying the output on the screen.

The key to writing the log command is the pre-defined tee command. This is used to split a text stream into two streams, one to standard output and one to a file. Thus the log command is a simple script to wrap this up in a form that’s suitable for use as a prefix.

We start as usual with the shebang to say which shell to use. In this case I’m going to use the bash shell, not the usual Bourne shell (sh).

#!/bin/bash

The reason for doing this will be explained later, but basically its because we need part of the extended functionality of bash to make this work.

As usual I then write a function to print out the usage information for the script:

function usage()
{
  cat <<EOF
usage: $0 <command>
  executes the <command>, directing the output to both the>
  terminal and a log file.  This script acts as a prefix
  command - its arguments form a command to be executed.  Both
  standard output and standard error are duplicated to the
  terminal and to a log file.  The log file has the same name
  as the first argument (the command) with .log appended.  The
  log file is stored in the same directory that the command is
  run in.

  <command> : the command to execute

The command-line processing in this case is very simple, because the only option is the automatic -h option for printing help and a chack to ensure there is a command to run:

error=0
while getopts h option; do
  case $option in
    h) usage; exit 0 ;;
    \?) error=1 ;;
  esac
done
shift $((OPTIND-1))
# check for the existence of a script to run
if [ $# -eq 0 ]; then
  echo "$0: a command to run is required"
  error=1
fi
if [ $error -gt 0 ]; then
  usage
  exit 2
fi

The next stage is to generate a log file name from the first part of the suffix command to be run:

logbase=`basename $1 .exe`
logfile=${logbase}.log

This first takes the first part of the suffix command from $1 and extracts the ‘basename’ of the command, which is the name of the command with any path information stripped off. For the sake of Windows users, this optionally also strips off any .exe suffix from the command if present. So, for example, if the suffix command was ‘/usr/bin/search.exe’, then the basename would be ‘search’.

This is then converted into the name of the log output file by adding the .log extension.

The meat of the command is the running of the suffix command and the redirection into the tee command.

echo "logging output to $logfile"
"$@" 2>&1 | tee "$logfile"
status=${PIPESTATUS[0]}

The second line is the key one. This runs the remaining command-line parameters as a sub-command ("$@"), quoting its arguments appropriately. Then, standard error is redirected to standard output (2>&1) so that both streams are now routed into a single pipe. Finally, this is piped into the tee command (| tee "$logfile") which routes the output to both standard output and to the specified logfile.

Normally, the exit status of a pipeline is the exit status of the last command, which in this case is the tee command. However, we don’t want the exit status of the tee command. The requirement for prefix commands is that they should return the exit status of the suffix command. The third line in the above snapshot (status=${PIPESTATUS[0]}) is a bash-specific feature that allows you to get the exit status of any command in a pipeline. All the exit statuses are stored in an array called PIPESTATUS, indexed from 0 upwards. In this case, we’ve extracted the first element, which is the exit status of the suffix command.

The final part of the log command is to then return this status:

exit $status

This is the basic log command. It can of course be expanded, for example to allow the log file to be redirected to a log directory, or to date/time stamp the logfile name, but these extra details are not specific to the writing of prefix commands, so I won’t go into that here.

Using Prefix Commands

Lets say I have a backup script – called backup – and I want to run it on startup. However, I don’t want it to run straight away since my internet connection might not be up and running, so I’ll delay the start for a few seconds. Also, I want the script to exit if the backup succeeded, but not if it failed. I also want to log the backup so that I can check it later. I can do this with a sequence of prefix commands which I can run as a background task on startup:

nice sleep-before -w 60 wait-after -e log backup &

So, this runs at low priority (nice). It does a sleep of 60 seconds before running the est of the command (sleep-before -w 60). It will wait after it has finished, but only on error (wait-after -e). And it will log its output to a log file (log). By putting the log command at the end of the sequence, it ensures that the log file is called ‘backup.log’, since it uses its first argument as the basename for the logfile.

Posted in Shell scripting, Software, Tutorials | Leave a comment

STLplus 3.7 Released

This is an update incorporating a number of small changes and bug-fixes. I have also rearranged the IDE project files to be separate from the source code.

See http://stlplus.sourceforge.net/stlplus3/docs/changes.html for more details.
Posted in Announcements, C++, Software | Comments Off