Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,
I want to control some USB-measurement device (spectrometer) and they gave me this dll: specu1a.dll
The manufacturer gave me some VB-Code which is working and the Interop-Decleration looks like this:
VB
Public Declare Ansi Function USB_OpenDevice Lib "specu1a.dll" Alias "USB_OpenDevice" ( _
                    ByVal ProductID As UShort _
                    ) As Integer

This Code works and the function return a valid Integer representing the DeviceHandle
I am not new to DllImport stuff so I tried the function to open the device like this:
C#
[DllImport("specu1a.dll")]
public static extern int USB_OpenDevice(ushort ProductID);

And? result=-1
even more sofisticated attempts like this:
C#
[DllImport("specu1a.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi)]
[return:MarshalAs(UnmanagedType.I4)]
public static extern unsafe int USB_OpenDevice([MarshalAs(UnmanagedType.U2)]ushort ProductID);

came out with the same...
WHAT A I DOING WRONG?
There is no error Message from VS2012, I use x86 build settings and this "Marshal.Prelink(Methodinfo USB_OpenDevice)" also gives no error so the dll should be loading correctly.
Posted
Updated 1-Mar-13 7:31am
v2
Comments
Sergey Alexandrovich Kryukov 1-Mar-13 13:37pm    
At first glance, looks correct. Are you sure that CPU instruction-set architectures match? Is the target platforms of this VB library x86? Is the target platform of your application assembly (important! The one where your entry-point is, not the one importing the library!) also x86?
By the way, you don't need both Marshal as; you only need to check that the type sizes match (as well as signed/unsigned; sorry, I'm not using VB, please check up; I think they are correct).
—SA
Niles Davis 2-Mar-13 5:13am    
platform and sizes are matching as well as platform, but that did not help.
Sergey Alexandrovich Kryukov 1-Mar-13 13:42pm    
And I really sorry for your troubles. Those manufacturers of such equipments are mostly so illiterate in computing! And the mere fact they offer VB (VB6?) these days, looks very suspicious: these days, many manufacturers improved their work a bit, and often offer .NET interface libraries.
I deal with those hardware interfaces many years, many of those manufacturers make most idiotic "solution" in their "software". Unfortunately...
—SA
Richard MacCutchan 1-Mar-13 13:48pm    
Wouldn't it be more sensible to ask this question of the people who provided the library?
Sergey Alexandrovich Kryukov 1-Mar-13 21:55pm    
You are right, but... look at what kind is the library is. I tried to ask manufacturer's representative many times, not that they are not responsive enough, but... very hard to reach a person who understand the matter at least a bit. But of course OP should try...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900