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. 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.
Developer Blog
Using CANlib Visual Studio 2017 C# .NET Standard 2.0
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 BLOG: Using CANlib Visual Studio 2017 C#.NET.
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 (C# .NET)
CANlib Kvaser CANlib SDK www.kvaser.com/downloads-kvaser
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 “Kvaser.CanLib.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 (C# .NET).
(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…”
I chose to create:
- Windows Forms App (.NET Framework) Visual C#
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.
Now VS2017 creates an application for us.
It is a bit boring, just an empty form that does nothing.
4 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:
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.
5 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 “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.
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:
Add the text “using canlibCLSNET;” in the USING clauses:
Add the text “using Kvaser.CanLib;” in the USING clauses:
When I try to BUILD, then I get the errors:
The type or namespace name ‘Kvaser’ could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name ‘Canlib’ 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 Kvaser.CanLib
Right click on “References”, and select “Add Reference…”:
Select Browse and then click on the button “Browse…”:
(If you have done this before, then VS2017 will show Kvaser.CanLib.dllin the window as you can see in my example.)
Select “c:\…\…\win32\netstandard2.0\Kvaser.CanLib.dlll”
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:
If I try to BUILD and RUN the application, it works! (sometimes)
If I press Button1, something will appear in the textbox:
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 …
6 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…”:
Select “<New…>” and add x86 and x64:
Important: Use the “new” from the “Project contexts”, not from “Active solutions platform”.
Copy settings from “any CPU”:
I do this for x86 and x64 and then return to the main IDE.
7 Testing x86 and x64
Select “x86” and press “Start”:
It works! The application starts (but I get a warning).
Select “x64” and press “Start”:
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.
8 Setting PATH for Kvaser.CanLib WIN32 and WIN64
I want VS2017 to automatically select the correct version of “Kvaser.CanLib.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…”:
Go to the section “Reference Paths” and add the path to the WIN32 “Kvaser.CanKib dll”.
Please remember to press the button “Add Folder”.
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>
<ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\</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>
<ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\win32\</ReferencePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x64' ">
<ReferencePath>C:\Program Files (x86)\Kvaser\Canlib\dotnet\x64\fw40\</ReferencePath>
<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.
9 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)
10 Thoughts and FAQ
10.1 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 a 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)
10.2 Safest setup?
If I select x86 (WIN32) built application, it will run on both WIN32 and WIN64 machines.
10.3 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.
Thanks for reading this! If you have any questions or comment regarding this paper, please send a mail to SUPPORT@KVASER.COM.