Setting Up CANlib for Visual Studio

  • July 8, 2015
  • Magnus Carlsson

This is the first post in a 3-part series about CANlib development:

  1. Setting up CANlib in Visual Studio
  2. Basic API Calls
  3. Event Driven Applications

Intro

Welcome to our guide on how to setup a Visual Studio C# project to use the Kvaser CANlib Software Development Kit.

Before beginning please make sure that you’ve installed CANlib and our latest drivers. In addition to the Visual Studio C# tools, you will need to install the Microsoft Visual C++ redistributable package. This package is necessary because the managed CANlib Dynamic Link Libraries are created using C++.

Example

For our example we are going to create a Windows C# console application. To do this, we will launch the New Project wizard by selecting the New Project link from the Start Page. Once the wizard is open, select Windows Desktop under Visual C#, in the Templates tree. Select Console Application from the template list. You may also choose your desired .NET framework from the drop menu above the available templates list. We need to create an application name and select a location for the project to be saved. Finally, press OK and Visual Studio will create the solution directory and associated project files.

Once the project has opened, right click on the project file in the Solution Explorer. Select Properties, then under the Application tab, make sure that the Target Framework has the desired Framework version.  Next we move to the Build tab to set the Platform Target. First change the Configuration to “All Configurations,” then select the desired Platform Target. The Platform Target cannot be set to Any CPU. The CANlib Dynamic Link Libraries are created using C++ which requires a specific target platform. Therefore, you must select a Platform target for your project of x86 or x64. For this example, we will choose x64.

Save these settings, and then right click on References in the Solution Explorer. Select “Add Reference,” and then browse to select the CANlib DLL file that matches your project’s target platform and framework. In the CANlib installation directory these files are sorted into different subdirectories by Target Platform followed by Framework version. First we will need to browse for dotnet in the CANlib installation directory. Since we chose to work with the 4.5.1 Framework and x64 in this example, we will select the x64 folder and then select fw40 to see the available CANlib DLLs that are compatible with our project. For this project, we are working with communication on a Kvaser CAN interface, so we will choose canlibCLSNET.dll. Once a selection has been made, it should appear under References in the Solution Explorer.

We are now ready to add code that uses the CANlib API. To prove that the project is setup correctly, we will add the first couple of necessary lines to access the functions in the canlibCLSNET.dll. We first add the canlibCLSNET namespace to the project with the “using” directive. Then in the main routine we will add a call to the canInitializeLibrary function. Now we can try building the project. Since the project builds without errors, we can make sure all of the runtime components are present by running the project. Our example was able to launch without any runtime exceptions so we know our libraries have been successfully linked to the project.

More Info

For more information about CANlib, you can view our CANlib SDK Help and check out our series of developer blogs.

Author Image

Magnus Carlsson

Magnus Carlsson is a Software Developer for Kvaser AB and has developed firmware and software for Kvaser products since 2007. He has also written a number of articles for Kvaser’s Developer Blog dealing with the popular Python language.