Developer Blog

09/03/2021 by Lars-Göran Fredriksson

Using CANlib Visual Studio 2017 C#.NET (canlibCLSNET)

In this document I will show you how to enable CANlib in Visual Studio 2017 when creating a C# .NET project. I will show how to create WIN32 and WIN64 applications. I will also show how to handle the platform-settings: x86, x64 and AnyCPU. When this is done, you can use the same project (source code) for creating WIN32 and WIN64 applications without editing your code. The instructions in this document can also be used when enabling CANlib in an existing project.

 

Important information from the author:

Since CANLib version 5.30, the support for canlibCLSNET has been removed. Instead we now supports “.NET Standard 2.0.”


Abbreviations

VS2017Microsoft Visual Studio 2017 (C# .NET)
CANlib Kvaser CANlib SDK www.kvaser.com/download


Before we start...

First we must download and install “Kvaser CANlib SDK“ and also “Kvaser Drivers for Windows”. After you have installed CANlib, please check your hard-drive and identify where “canlibCLSNET.DLL” has been installed.

On my 64-bit Windows machine it is installed here:

c:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\fw40\canlibCLSNET.dll

c:\Program Files (x86)\Kvaser\Canlib\dotnet\x64\fw40\canlibCLSNET.dll

As you can see, one is for building 32-bit and one is for 64-bit applications.

Please remember where you found them, as you will need this information soon.

Please also install “Microsoft Visual C++ Redistributable for Visual Studio 2017” if you not have done it. (Installs by default if you install the C++ tools when installing VS2017.)

Now we are ready to start Visual Studio 2017 (C# .NET).

(I have used “Microsoft Visual Studio Professional 2017” when creating this text.)


Creating an empty project

Creating a project in VS can be done in many different ways, this is one way to do it.

Press, “File” – “New” -”Project…”

Screen Shot 2018-11-13 at 5.45.48 PM

I chose to create a Windows Forms App (.NET Framework) Visual C#.

Edit the name and the rest of the unique information and press “OK”.

Screen Shot 2018-11-13 at 5.46.12 PM

Now VS2017 creates an application for us. It is a bit boring, just an empty form that does nothing.

(Do not try to create an “Universal Windows” app. Our drivers will not work with “Universal Windows Apps”.)


Adding some code that uses CANlib (C#)

I will now add some code that uses CANlib; exactly what it does is not important in this example. For more information about how to use CANlib, please check CANlib SDK Help.

To add a BUTTON and a TEXTBOX, change the property “Multiline” on the textbox to true. (I assume that you are familiar with how to add components in VS2017, I will not show it here).

After a bit of editing, we have this beautiful application:

Screen Shot 2018-11-13 at 5.49.26 PM

It still does nothing, so in the editor, double click on “button1” and add some code inside the “button1_Click” function.

private void button1_Click(object sender, EventArgs e)
        {
            Canlib.canStatus R;
            int V;
        
            textBox1.Clear();
            textBox1.Text = "DEMO VS2017 C# .NET\r\n";

            Canlib.canInitializeLibrary();

            V = Canlib.canGetVersionEx(Canlib.canVERSION_CANLIB32_PRODVER32);
            int V1 = (V & 0xFF0000) >> 16;
            int V2 = (V & 0xFF00) >> 8;
            textBox1.Text += string.Format("Found CANlib version {0}.{1}\r\n",V1,V2 );

            R = Canlib.canGetNumberOfChannels(out int NOC);

            textBox1.Text += string.Format("Found {0} channels\r\n", NOC);
            textBox1.Text += "----------------------------------------\r\n";
        }

When I try to run the application, I get multiple errors. Something is wrong and VS2017 cannot find CANlib.


Adding a reference to canlibCLSNET.dll

In order to use CANlib in a VS2017 project, we must add information to show where VS2017 can find “canlibCLSNET.dll”. It is only possible to add a reference to either the x86 version or the x64 version. However, with a minor modification in one file, we can enable both. I will show that later in this document.

Before I start, I select the platform to “Any CPU” (for the moment, it is the only platform I have enabled in this application).

Select “Any CPU” as platform:

Screen Shot 2018-11-13 at 5.53.22 PM

Add the text “using canlibCLSNET;” in the USING clauses:

Screen Shot 2018-11-13 at 5.53.31 PM

When I try to BUILD, then I get the error:

“The type or namespace name ‘canlibCLSNET’ could not be found (are you missing a using directive or an assembly reference?)”

Yes, I am missing a “Reference”

I must add the “Reference” to canlibCLSNET

Right click on “References”, and select “Add Reference…”:

Screen Shot 2018-11-13 at 5.56.32 PM

Select Browse and then click on the button “Browse…”:

Screen Shot 2018-11-13 at 5.57.32 PM

(If you have done this before, then VS2017 will show canlibCLSNET.dll in the window as you can see in my example.)

Select “c:\…\…\win32\fw40\canlibCLSNET.dll” (Hope you remembered it):

Screen Shot 2018-11-13 at 5.59.03 PM

I am selecting the WIN32 version (even if I intend to use the WIN64 at a later date).

Press the “OK” button.

Now I can see that the canlibCLSNET reference has been added:

Screen Shot 2018-11-13 at 6.00.16 PM

If I try to BUILD and RUN the application, it works! (sometimes)

If I press Button1, something will appear in the textbox:

Screen Shot 2018-11-13 at 6.01.55 PM

Sometimes I get a WARNING, and sometimes I get an ERROR. I am not sure why I get the warning or error, because I should always get the error. (I am running my application on a WIN64 PC and the result should be an error. Maybe the compiler tries to save me.)

But, there is a simple fix to this little problem, and while I fix it, I also add support for using the WIN32 and WIN64 application platforms. Keep on reading …


Enabling CANlib for WIN32 and WIN64

So far, I have set “Any CPU” as the target CPU platform. Now I want to set it to the platforms so I can build applications for x86 (win32) and x64 (win64).

Select “Configuration Manager…”:

Screen Shot 2018-11-13 at 6.06.13 PM

Select “<New…>” and add x86 and x64:

Screen Shot 2018-11-13 at 6.07.29 PM

Important: Use the “new” from the “Project contexts”, not from “Active solutions platform”.

Copy settings from “any CPU”:

Screen Shot 2018-11-13 at 6.08.26 PM

I do this for x86 and x64 and then return to the main IDE.


Testing x86 and x64

Select “x86” and press “Start”:

Screen Shot 2018-11-13 at 6.11.24 PM

It works! The application starts (but I get a warning).

Select “x64” and press “Start”:

Screen Shot 2018-11-13 at 6.12.22 PM

I got a warning, but expected an error. This is normal and will be fixed in the next section.

OK, now the situation has been improved a lot. We can select the platform, and sometimes we get a warning and sometimes an error. I said earlier that I should fix this, and now this seems to be a proper time to do that.


Setting PATH for canlibCLSNET WIN32 and WIN64

I want VS2017 to automatically select the correct version of canlibCLSNET.dll when I build my project.

I have found one method to do it (there might be more methods).

In my directory structure for the project, there is a file we can edit:

CANlib_VS2017_DEMO.csproj.user (ProjName.csproj.user)

But I can not find it! Where is it? 

OK, no problem. It is not created by default, only when needed.

Let us create it!

This file is little like Harry Potter’s “Room of Requirement”; it can be almost whatever we want it to be.

Open “Project” – “ProjName Properties…”:

Screen Shot 2018-11-13 at 6.15.36 PM

Go to the section “Reference Paths” and add the path to the WIN32 canlibCLSNET:

Screen Shot 2018-11-13 at 6.18.54 PM

I save my project and close VS2017.

Now, when I check my project directory, I can find the file “ProjName.csproj.user”.

Open it in a text editor, and my file looks like this:

(Please note, this file can contain a lot of information, so it is necessary to locate the part where the “ReferencePath” has been defined.)

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>

    <ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\fw40\</ReferencePath>

  </PropertyGroup>

</Project>

I am now adding some lines, and when I am done, my file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\fw40\</ReferencePath>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Platform)' == 'x64' ">
    <ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\x64\fw40\</ReferencePath>
  </PropertyGroup>
</Project>

I added a PropertyGroup with a condition (x64). This group will override the first PropertyGroup if I have selected x64 as platform. It is important that you add the x64 group after the general group.

Please, remember to check the path to the two DLL files.


Selecting platform x86, x64 and AnyCPU

I can now select x86 (win32) and x64 (win64) as active platforms, in both cases the application starts correctly and CANlib is available and working.

If everything worked well, you will also be able to create an C# dotNET CANlib application in Visual Studio 2017.

We have now enabled CANlib in Visual Studio 2017, created a C# .NET project and tested it for both x86 and x64. 

ΟΕΔ  (Q.E.D.  Quod erat demonstrandum)


Thoughts and FAQ

Can I use AnyCPU? 
When I select AnyCPU as a platform, I get a warning or an error!

When the computer starts Visual Studio, if I have selected AnyCPU, the software will decide whether to use the 32bit or 64bit version of the DLL. Yes, AnyCPU might work, but if I run the built application on another machine, with different setup, it might not work.

(Dynamic loading of the DLL might work, please check this external link for more information: https://stackoverflow.com/questions/108971/using-side-by-side-assemblies-to-load-the-x64-or-x32-version-of-a-dll)

Safest setup? 
If I select x86 (WIN32) built application, it will run on both WIN32 and WIN64 machines.

Do I need WIN64?
If your application needs it, CANlib will work perfectly, but only on WIN64 machines. However, today (2021), the majority of Windows PCs are win64.

Microsoft Visual C++ and canlibCLSNET Redistributable
When installing your app on another machine (might not have Visual Studio installed), please remember to redistribute (and install) the “Microsoft Visual C++ Redistributable for Visual Studio 2017”. You will need to copy the canlibCLSNET.dll (x86 and/or x64) with your project executable. These dlls are not installed as part of the driver (they are part of KVASER CANlib SDK).

Thanks for reading this! If you have any questions or comment regarding this paper, please send a mail to SUPPORT@KVASER.COM. 

Author Image

Lars-Göran Fredriksson

Lars-Göran Fredriksson is a Field Application Engineer for Kvaser AB. His background is in geographic information system (GIS) and Remote Sensing and his current focus is on connecting the deep knowledge of Kvaser's developers with the practical questions of our end users. If you doubt his passion for CAN, just know that his first week in the office he created an interactive CAN Trivia game that sent the office scouring the halls for the correct answers. He is a passionate fisherman who would like to develop new environmentally friendly fishing methods. Biggest catch and release fish is for the moment a Bluefin Tuna at appr 325kg / 715lbs.