Using CANlib Visual Studio 2017 VB .NET Standard 2.0

  • March 5, 2021
  • Lars-Göran Fredriksson

In this document I will show you how to enable CANlib in Visual Studio 2017 when creating a Visual Basic (VB) project. I will show how to create WIN32 and WIN64 applications. I will also show how to handle the platform-settings: x86 and x64. 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”

This text is based on the Dev Blog: Using CANlib Visual Studio 2017 VB canlibCLSNET.
It has been modified for “.NET Standard 2.0” and now shows how to use the “.NET Standard 2.0” framework together with “.NET Framework 4.7.2”.

Some of the picture is not replaced, so they might still point to “canlibCLSNET”.

1 Abbreviations

“VS2017”:  Microsoft Visual Studio 2017 (VB)
“CANlib”:  Kvaser CANlib SDK (kvaser.com/download)

2 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\netstandard2.0\Kvaser.CanLib.dll
c:\Program Files (x86)\Kvaser\Canlib\dotnet\x64\netstandard2.0\Kvaser.CanLib.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.

Now we are ready to start Visual Studio 2017 (VB).
(I have used “Microsoft Visual Studio Professional 2017 (version 15.9.33)” when creating this text.)

3 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…”

pasted image 0 (19)

I chose to create:

  • Windows Forms App (.NET Framework) Visual Basic

Edit the name and the rest of the unique information and press “OK”.
Using “.NET Standard 2.0” requires “.NET Framework 4.6.1” or higher.
Many developers recommend using “.NET Framework 4.7.2” or higher because of better stability and compatibility.

unnamed (12)

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

4 Adding some code that uses CANlib (VB)

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:

pasted image 0 (21)

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

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = "Hello Visual Basic .Net (c2021 Kvaser AB" & vbNewLine

        Kvaser.CanLib.Canlib.canInitializeLibrary()

        'Dim channel As Integer
        Dim channel = 0
        Dim hnd0 As Integer
        Dim R As Kvaser.CanLib.Canlib.canStatus

        hnd0 = Kvaser.CanLib.Canlib.canOpenChannel(channel, Kvaser.CanLib.Canlib.canOPEN_ACCEPT_VIRTUAL)
        R = Kvaser.CanLib.Canlib.canSetBusParams(hnd0, Kvaser.CanLib.Canlib.canBITRATE_250K, 0, 0, 0, 0)
        R = Kvaser.CanLib.Canlib.canBusOn(hnd0)

        TextBox1.Text += "Channel 0 and 1 initiated" & vbNewLine

    End Sub

When I try to run the application, I get multiple errors. Something is wrong and VS2017 cannot find ‘Kvaser.CanLib’. We will fix that, but first we must set the platforms.

5 Enabling the WIN32 and WIN64 platforms

When creating an VB application, VB defaults to “Any CPU” as the target CPU platform.
We can not use Any CPU as platform because we can only link to one version of “Kvaser.CanLib.dll”.

We must now enable the x86 (win32) and the x64 (win64) platforms.

Select “Configuration Manager…”:

unnamed (13)

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

unnamed (14)

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

Create the new platforms x64 and x86 (we will have to do this twice). Copy settings from “any CPU”:

pasted image 0 (22)

I do this for x86 and x64. I also delete the AnyCPU platform to make sure that I not enabling it by mistake in the future (select EDIT and remove it).

Now I must edit the configuration manager to make sure that we are using the correct platform.

unnamed (15)

In the picture above, we can see that even if we have selected the combination DEBUG/x86, the compiler will try to create the platform “Any CPU”.

This time I select NEW from “Project contexts”

pasted image 0 (23)

Create the platform x86. Do this for all four combinations:

Debug/x86    Use x86
Debug/x64 Use x64
Release/x86 Use x86
Release/x64 Use x64

6 Verifying Target CPU and setting output path

6.1 Open PROJECT/PROPERTIES

I can open the PROPERTIES in two different ways:
1 Meny: Project / appname properties
2 Solution Explorer, right click on the appname, select properties.

unnamed (16)
pasted image 0 (25)

In some versions of Visual Studio, there is a bug in the properties page when it is opened with the first method. This bug sometimes claims that the platform is not selectable. If we use the second method, then the page works perfect.

Check that Target CPU is correct for all four Configuration/Platform combinations.

pasted image 0 (26)

6.2 Set Build Output Path(Optional)

When we are checking the Target CPU, we can also (if we want) edit the output path.

Default is:
bin\x86\Release\
bin\x64\Debug\
bin\x64\Release\
bin\x86\Debug\

I prefer:
bin\Debug\x86\
bin\Debug\x64\
bin\Release\x86\
bin\Release\x64\

I think this makes it a bit easier for me to find the built applications.

7 Adding a reference to Kvaser.CanLib.dll

In order to use CANlib in a VS2017 project, I must add information to show where VS2017 can find “Kvaser.CanLib.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.

Select “x86” as platform:

pasted image 0 (27)

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

pasted image 0 (28)

Yes, I am missing a “Reference”

I must add the “Reference” to Kvaser.CanLib.dll

7.1 Adding Kvaser.CanLib.dll for x86

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

pasted image 0 (29)

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

pasted image 0 (30)

(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\netstandard2.0\Kvaser.CanLib.dll ” (Hope you remembered it):

pasted image 0 (31)

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 Kvaser.CanLib reference has been added:

pasted image 0 (32)

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

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

unnamed (17)

7.2 Adding Kvaser.CanLib.dll for x64

Select x64 as platform

pasted image 0 (33)

If I try to build the application, no errors or warnings will appear.

But some warnings and errors when I try to run the application.

pasted image 0 (34)

(Sorry about the errortext in Swedish!)

I can only add one version of Kvaser.CanLib.dll. So I must remove the reference to the x86 version, before I add the x64 version.
(Please wait a bit to remove the reference, we can keep it and manually enable both x86 and x64. I will show it in section 7.4)

7.3 Remove reference to Kvaser.CanLib.dll

(Please do not remove the reference, continue with section 7.4 !)
Remove the reference by right clicking on it and select REMOVE

unnamed (18)

When the reference is removed, I can add the reference to x64. Follow the same procedure as in section 7.1

7.4 Manually adding Kvaser.CanLib for x86 and x64

It is possible to add the reference for Kvaser.CanLib.dll for both x86 and x64 at the same time. When building the application, the correct version will be used and no errors or warnings shall appear.

I will show two methods, one manual and one automatic version.

Both versions work fine and which one you should select depends more how you later decide to handle your software. 

The manual method (section 7.4.1) gives you more control of the version and you must manually copy the dll files in the future when updating to newer SDK versions. 

The automatic version (section 7.4.2) copies the latest installed SDK files and includes them into your project.

If you are unsure, use the automatic version, you can switch methods whenever you want.

7.4.1 Manually adding canlibCLSNET for x86 and x64

Before we start, add the reference for x86 and test it with BUILD and RUN.

(Please read section 7.4.2 before trying this method)

When VS2017 is building the application, it also copies the canlibCLSNET.DLL to the application directory.

If we turn this feature off, and manually copies the DLL files, then  we can use the x86 reference for both x86 and x64 platform

In Solution explorer/references, select canlibCLSNET

unnamed (18)

Change the property “Copy Local” to false.

When I build my application, I get four directories:
.\WindowsApp1\bin\Debug\x86\
.\WindowsApp1\bin\Debug\x64\
.\WindowsApp1\bin\Release\x86\
.\WindowsApp1\bin\Release\x64\

Copy the file canlibCLSNET.dll to the four directories above (make sure that you copy the x86 version to the x86 platform directory, and the x64 version to the x64 directory)

7.4.2 Automatic adding canlibCLSNET for x86 and x64

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

(I assume that you have followed the example and skipped section 7.4.1)

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:
WindowsApp1.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.

7.4.2.1 Creating the file “ProjName.csproj.user”

Open “Project” – “ProjName Properties…” as described in section 6.1

unnamed (19)

Go to the section “References” and press the button “Reference Path…”

pasted image 0 (35)

Press button “Browse”

unnamed (20)

Select directory: ”c:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\fw40\”

pasted image 0 (36)

Press the button “Add Folder”

pasted image 0 (37)

Press the “OK” button

pasted image 0 (38)

I save my project and close VS2017.

7.4.2.2 Editing the file “ProjName.csproj.user”

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\netstandard2.0\</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\netstandard2.0\</ReferencePath>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Platform)' == 'x64' ">
    <ReferencePath>c:\Program Files (x86)\Kvaser\Canlib\dotnet\x64\netstandard2.0\</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.

8 Build and Run platform x86 and x64

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 VB dotNET CANlib application in Visual Studio 2017.

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

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

9 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 [email protected]

lgf-20201114-cr

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.