Click here to Skip to main content
13,053,276 members (61,672 online)
Rate this:
Please Sign up or sign in to 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:
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:
public static extern int USB_OpenDevice(ushort ProductID);

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

came out with the same...
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 1-Mar-13 7:28am
Updated 1-Mar-13 7:31am
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).
Niles Davis 2-Mar-13 5:13am
platform and sizes are matching as well as platform, but that did not help.
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...
Richard MacCutchan 1-Mar-13 13:48pm
Wouldn't it be more sensible to ask this question of the people who provided the library?
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...
Niles Davis 2-Mar-13 5:10am
Hi! Thanks for your replys. I started a completely new project also on .net2 as the vb version was but no improvements...
i am doing this for a few years and I completely agree your opinion on software from hardware manufacturers. some really do care but most of them don't! it is not possible to get any useful information from them, sorry.
BUT i really appreciate any hints for dllimport debugging, etc. so if anybody got an idea, please help
It looks like your comment was addressed to me, but I did not receive a notification. I paid attention for this one only because of your other comment above. If you need you comment noticed, you need to use "Reply" to the post of the person you are addressing to.

Unfortunately, I cannot see any bugs in your code. I would test it by writing the invoked code by myself (its imitation version) using C++ (I never install Basic and trying not to deal with manufactures supplying basic code), just to test P/Invoke, just in case... Sometimes calling conventions can be different from what was claimed.... cannot tell anything else.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100