Click here to Skip to main content
14,420,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I have a C# application that needs to get the list of a connected devices. The actual operation is done by a 3rd party DLL.

The program works in Win7 and Vista, however for XP, the call always returns zero (0) number of devices connected, in structure field NumDevices.

The code was built in VS2008, Framework 3.5.

Can anyone help in resolving this or point me to sources/articles/KB detailing any differences between XP and Win7 C# development.

Here's the C# code that calls that DLL.

[DllImport(BSApi.BSAPI)]
public static extern int ABSEnumerateDevices(
        string pszEnumDsn,
        ref IntPtr ppDeviceList);



The ppDeviceList is a pointer to a structure containing the list of devices returned by the dll call.

[StructLayout(LayoutKind.Sequential)]
unsafe public struct ABS_DEVICE_LIST
{
    public UInt32 NumDevices;     ///Number of devices in the list

    [MarshalAs(UnmanagedType.ByValArray)]
    public ABS_DEVICE_LIST_ITEM[] List;     ///The list of devices.
}


Below is the structure for the ABS_DEVICE_LIST_ITEM

[StructLayout(LayoutKind.Sequential)]
unsafe public struct ABS_DEVICE_LIST_ITEM
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
    public byte[] DsnSubString;     ///device name

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
    public byte[] reserved;     ///Reserved for future use.
}


We call the dll function as follows:

int rc = 0;

BSTypes.ABS_DEVICE_LIST deviceList = new BSTypes.ABS_DEVICE_LIST();

int rawSize = Marshal.SizeOf(deviceList);

IntPtr buffer = Marshal.AllocHGlobal(rawSize);

Marshal.StructureToPtr(deviceList, buffer, false);

rc = BSApi.ABSEnumerateDevices("usb", ref buffer);

BSApi.ABSFree(buffer);

deviceList = (BSTypes.ABS_DEVICE_LIST)Marshal.PtrToStructure(buffer,
typeof(BSTypes.ABS_DEVICE_LIST));

int count = (int)deviceList.NumDevices;
Posted
Updated 23-Sep-10 22:39pm
v3
Comments
CPallini 24-Sep-10 3:22am
   
Removed the boldface stuff.
knockNrod 1-Oct-10 13:48pm
   
I assume the biometric reader is actually functioning in XP? I'm not aware of any differences in marshalling, so I'm wondering if the problem isn't that it's empty before you copy.

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




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