S
Schorschi
Can someone please point out why I am getting an 87 error? I am sure
it is obvious, but I am new to C# and seem to be having a lot of
stress understanding managed versus unmanaged code when API calls are
needed!
//
using System;
using System.Runtime.InteropServices;
//
namespace DeviceClassLibrary
{
/// <summary>
/// Device Class Library.
/// </summary>
//
//typedef struct _SP_DEVINFO_DATA
// {
// DWORD cbSize;
// GUID ClassGuid;
// DWORD DevInst;
// ULONG_PTR Reserved;
// } SP_DEVINFO_DATA, *PSP_DEVINFO_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVINFO_DATA
{
//
public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theInstance;
public Int64 theReserved;
}
//typedef struct _SP_DEVICE_INTERFACE_DATA
// {
// DWORD cbSize;
// GUID InterfaceClassGuid;
// DWORD Flags;
// ULONG_PTR Reserved;
// } SP_DEVICE_INTERFACE_DATA, *PSP_DEVICE_INTERFACE_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DATA
{
//
public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theFlags;
public Int64 theReserved;
}
//typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA
// {
// DWORD cbSize;
// TCHAR DevicePath[ANYSIZE_ARRAY];
// } SP_DEVICE_INTERFACE_DETAIL_DATA,
*PSP_DEVICE_INTERFACE_DETAIL_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DETAIL_DATA
{
//
public int theSize;
public byte[] thePath;
}
//
public enum DeviceError
{
//
Success = 0
}
public class DeviceClass
{
//
[DllImport("SETUPAPI", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs (
byte[] theClass,
IntPtr theEnumerator,
IntPtr theOwner,
int theFlags);
[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(
IntPtr theHandle
);
[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(
IntPtr theHandle,
int theIndex,
_SP_DEVINFO_DATA theData);
// Constructor...
public DeviceClass()
{
//
}
// Destructor...
~DeviceClass()
{
//
}
public int Enumerate(string theClass, int theCode, ref IntPtr
theHandle)
{
int theResult = (int)DeviceError.Success;
Guid theGuid;
//
theGuid = new Guid(theClass);
theHandle = SetupDiGetClassDevs
(theGuid.ToByteArray(), IntPtr.Zero, IntPtr.Zero, theCode);
//
return (theResult);
}
public int UnEnumerate(ref IntPtr theHandle)
{
int theResult = (int)DeviceError.Success;
//
theHandle = IntPtr.Zero;
return (theResult);
}
public int RegistryProperty(IntPtr theHandle, int theIndex)
{
int theResult = (int)DeviceError.Success;
_SP_DEVINFO_DATA theData = new _SP_DEVINFO_DATA();
//Buffer theBuffer;
//int theSize;
bool thePropertyOrNot;
//
theData.theSize = Marshal.SizeOf(typeof(_SP_DEVINFO_DATA));
thePropertyOrNot = SetupDiEnumDeviceInfo(theHandle, theIndex,
theData);
theResult = Marshal.GetLastWin32Error();
return (theResult);
}
}
}
it is obvious, but I am new to C# and seem to be having a lot of
stress understanding managed versus unmanaged code when API calls are
needed!
//
using System;
using System.Runtime.InteropServices;
//
namespace DeviceClassLibrary
{
/// <summary>
/// Device Class Library.
/// </summary>
//
//typedef struct _SP_DEVINFO_DATA
// {
// DWORD cbSize;
// GUID ClassGuid;
// DWORD DevInst;
// ULONG_PTR Reserved;
// } SP_DEVINFO_DATA, *PSP_DEVINFO_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVINFO_DATA
{
//
public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theInstance;
public Int64 theReserved;
}
//typedef struct _SP_DEVICE_INTERFACE_DATA
// {
// DWORD cbSize;
// GUID InterfaceClassGuid;
// DWORD Flags;
// ULONG_PTR Reserved;
// } SP_DEVICE_INTERFACE_DATA, *PSP_DEVICE_INTERFACE_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DATA
{
//
public Int32 theSize;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] theClass;
public Int32 theFlags;
public Int64 theReserved;
}
//typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA
// {
// DWORD cbSize;
// TCHAR DevicePath[ANYSIZE_ARRAY];
// } SP_DEVICE_INTERFACE_DETAIL_DATA,
*PSP_DEVICE_INTERFACE_DETAIL_DATA;
[StructLayout(LayoutKind.Sequential)]
public struct _SP_DEVICE_INTERFACE_DETAIL_DATA
{
//
public int theSize;
public byte[] thePath;
}
//
public enum DeviceError
{
//
Success = 0
}
public class DeviceClass
{
//
[DllImport("SETUPAPI", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs (
byte[] theClass,
IntPtr theEnumerator,
IntPtr theOwner,
int theFlags);
[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiDestroyDeviceInfoList(
IntPtr theHandle
);
[DllImport("SETUPAPI", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(
IntPtr theHandle,
int theIndex,
_SP_DEVINFO_DATA theData);
// Constructor...
public DeviceClass()
{
//
}
// Destructor...
~DeviceClass()
{
//
}
public int Enumerate(string theClass, int theCode, ref IntPtr
theHandle)
{
int theResult = (int)DeviceError.Success;
Guid theGuid;
//
theGuid = new Guid(theClass);
theHandle = SetupDiGetClassDevs
(theGuid.ToByteArray(), IntPtr.Zero, IntPtr.Zero, theCode);
//
return (theResult);
}
public int UnEnumerate(ref IntPtr theHandle)
{
int theResult = (int)DeviceError.Success;
//
theHandle = IntPtr.Zero;
return (theResult);
}
public int RegistryProperty(IntPtr theHandle, int theIndex)
{
int theResult = (int)DeviceError.Success;
_SP_DEVINFO_DATA theData = new _SP_DEVINFO_DATA();
//Buffer theBuffer;
//int theSize;
bool thePropertyOrNot;
//
theData.theSize = Marshal.SizeOf(typeof(_SP_DEVINFO_DATA));
thePropertyOrNot = SetupDiEnumDeviceInfo(theHandle, theIndex,
theData);
theResult = Marshal.GetLastWin32Error();
return (theResult);
}
}
}