LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with Kvaser CAN bus interface

I have downloaded the Kvaser LabVIEW driver and the required Canlib sdk from Kvaser.

 

I am trying to run the demo that came with the Kvaser LabVIEW drivers it keeps returning the error "Can not find requested DLL".

But the DLL in question is right were the Call Library Function expects it to be.

 

KV1Capture.PNG

 

kv2Capture.PNG

 

What am I doing wrong here?

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 1 of 15
(210 Views)

I guess you are on Win64 (LV64) and trying to load a 32bit dll here?

 

If not check if a dependency is missing (not findable) with  https://github.com/lucasg/Dependencies for canlib32.dll

Actor Framework
0 Kudos
Message 2 of 15
(148 Views)

@Quiztus2 wrote:

I guess you are on Win64 (LV64) and trying to load a 32bit dll here?

 

If not check if a dependency is missing (not findable) with  https://github.com/lucasg/Dependencies for canlib32.dll


Well it is Windows 10 64 bit but I am using 32bit LabVIEW 

 

I can't run any of that on my computer, corporate security policies will have my (you know what) in a sling.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 3 of 15
(139 Views)

Can you confirm that "canlib32.dll" is actually a 32-bit PE executable and working?

 

There are a few ways to check without installing tools - there should be a PE signature ("PE\0\0") in the first 300-ish bytes. You could just read the .dll as a binary file into LabVIEW and check by hand.

 

To confirm the dll loading correctly at all, try loading it into a powershell environment.

 

I had a similar issue with 32bit executables on a 64bit host having some system directories redirected for compatibility. This might throw off the dll.

0 Kudos
Message 4 of 15
(125 Views)

I have never used Powershell but a quick Google and tried to use the Add-Type command and get an error basically saying it does not understand the (x86) part of the path?

 

PS Z:\> Add-Type C:\Program Files (x86)\Kvaser\Canlib\Bin\canlib32.dll
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:28
+ Add-Type C:\Program Files (x86)\Kvaser\Canlib\Bin\canlib32.dll
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException 

 

I loaded the file into LabVIEW as a binary file and all I see besides "This program cannot be run in DOS mode" is a bunch of non-printable characters.

dlldCapture.PNG

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 5 of 15
(111 Views)

@RTSLVU wrote:

I have never used Powershell but a quick Google and tried to use the Add-Type command and get an error basically saying it does not understand the (x86) part of the path?

 

PS Z:\> Add-Type C:\Program Files (x86)\Kvaser\Canlib\Bin\canlib32.dll
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:28
+ Add-Type C:\Program Files (x86)\Kvaser\Canlib\Bin\canlib32.dll
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException 

 

I loaded the file into LabVIEW as a binary file and all I see besides "This program cannot be run in DOS mode" is a bunch of non-printable characters.

dlldCapture.PNG

 


This is the relevant part:

LLindenbauer_0-1769191602141.png

If you switch the string view to "\ codes", this part will probably become "PE\00\00\01\4C" (the x86 marker, as per https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#coff-file-header-object-and-image )

 

So the file is indeed a 32bit dll. The powershell import needs quotation marks for the complete file name, like so:

 Add-Type "C:\Program Files (x86)\Kvaser\Canlib\Bin\canlib32.dll"

 

However, Add-Type only works on .NET stuff. A binary dll needs an explicit type declaration to be loaded into powershell, which is a bit more involved: https://www.raydbg.com/2017/Call-Native-Win32-API-in-PowerShell/

 

So it should probably look like this (I recommend changing to the directory containing the dll with the cd command):

 

cd "C:\Program Files (x86)\Kvaser\Canlib\Bin\"

function ImportedCanOpenChannel {
    $Signature = @"
[DllImport("canlib32.dll", EntryPoint = "canOpenChannel")]
public static extern int canOpenChannel(uint channelNumber, uint flags);
 
uint channel = 1; 
uint flags = 0x0;
 
public static void CanOpenChannelExport() {
    canOpenChannel(channel, flags);
}
"@
    Add-Type -MemberDefinition $Signature -Name CanOpenChannelType -Namespace User_
    [User_.CanOpenChannelType]::CanOpenChannelExport()
}

ImportedCanOpenChannel

 

This is quite the sandwich though, wrapping the import into a .NET function wrapped into a .NET type, imported and called into powershell. It might be a good idea to check if the dll exports a simpler function (like GetVersion or the like).

0 Kudos
Message 6 of 15
(100 Views)

Have you loaded in the library? 

There should be a VI and dll function to load tthe library into memory. 

0 Kudos
Message 7 of 15
(95 Views)

I don't know what this is telling me.

 

PS C:\Program Files (x86)\Kvaser\Canlib\Bin> function ImportedCanOpenChannel {
>> $Signature = @"
>> [DllImport("canlib32.dll", EntryPoint = "canOpenChannel")]
>> public static extern int canOpenChannel(uint channelNumber, uint flags);
>>
>> uint channel = 1;
>> uint flags = 0x0;
>>
>> public static void CanOpenChannelExport() {
>> canOpenChannel(channel, flags);
>> }
>> "@
>> Add-Type -MemberDefinition $Signature -Name CanOpenChannelType -Namespace User_
>> [User_.CanOpenChannelType]::CanOpenChannelExport()
>> }
>>
>> ImportedCanOpenChannel
Add-Type : c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(15) : An object reference is required for the
non-static field, method, or property 'User_.CanOpenChannelType.channel'
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(14) : public static void CanOpenChannelExport() {
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(15) : >>> canOpenChannel(channel, flags);
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(16) : }
At line:13 char:5
+ Add-Type -MemberDefinition $Signature -Name CanOpenChannelType -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
ion
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(15) : An object reference is required for the
non-static field, method, or property 'User_.CanOpenChannelType.flags'
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(14) : public static void CanOpenChannelExport() {
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(15) : >>> canOpenChannel(channel, flags);
c:\Users\searsrt\AppData\Local\Temp\ke4fuggk\ke4fuggk.0.cs(16) : }
At line:13 char:5
+ Add-Type -MemberDefinition $Signature -Name CanOpenChannelType -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
ion
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.
At line:13 char:5
+ Add-Type -MemberDefinition $Signature -Name CanOpenChannelType -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

Unable to find type [User_.CanOpenChannelType].
At line:14 char:5
+ [User_.CanOpenChannelType]::CanOpenChannelExport()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (User_.CanOpenChannelType:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

PS C:\Program Files (x86)\Kvaser\Canlib\Bin>

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 8 of 15
(89 Views)

@dkfire wrote:

Have you loaded in the library? 

There should be a VI and dll function to load tthe library into memory. 


I think so... There is this VI that runs at the beginning of the demo program without error

 

KV4Capture.PNGKV3Capture.PNG

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 9 of 15
(88 Views)

And how is that lib call setup ?

0 Kudos
Message 10 of 15
(75 Views)