C and C++ Binaries

C# API for CodeSonar (beta)

The C# API is essentially identical to the Java API.

The C# API is in beta at this stage. Please report any problems to CodeSecure support. The C# API is only supported on Windows. It may be possible for a determined POSIX user to create a C plugin that loads the Mono virtual machine and loads your assembly.

Building

In order to build a C# plugin, compile the files at $CSONAR/src/api/csharp/*.cs, plus all the C# files you write, into a .NET assembly (dll file). For example:

csc -t:library -noconfig -out:MyPlugin.dll MyPlugin.cs $CSONAR/src/api/csharp/*.cs

The contents of MyPlugin.cs might look like this:

using System;

class echo_point_visitor: point_visitor{
    public echo_point_visitor(){}
    
    public override void visit(point p) 
    {
        /* Always wrap visitors in exception handlers.  If an
         * exception isn't caught, behavior is undefined.
         */
        try{
            Console.WriteLine("csharp visits " + p);
        }
        catch( Exception e )
        {
            Console.WriteLine(e);
        }
    }
};

public class Main
{
   public static int main(String dummy)
   {
        /* Always wrap everything in an exception handler.  If an
         * exception isn't caught, behavior is undefined. 
         */
        try{
            analysis.add_point_visitor(new echo_point_visitor());
        }
        catch( Exception e )
        {
            Console.WriteLine(e);
        }
        return 0; // ignored
   }
}
    

Running

Set the CSHARP_PLUGINS, and CSHARP_PLUGIN_DOTNET_VERSION .conf settings appropriately. For example:

      CSHARP_PLUGINS += h:\MyPlugin.dll
      CSHARP_PLUGIN_DOTNET_VERSION = v4.0.30319

API Reference

There is fairly little documentation for the C# API at this point. The interface can be seen by browsing the $CSONAR/src/api/csharp/*.cs files. The C# API is essentially identical to the Java API. The major difference between Java and C# is that enumeration values in Java can be referenced with expressions such as point_kind.getCALL_SITE(). C# supports attributes, so the equivalent C# expression is point_kind.CALL_SITE.