473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help With PInvoke and SetupDiEnumDriverInfo/SP_DRVINFO_DATA

I would really appreciate it if someone could help me figure out what
I'm doing wrong trying to PInvoke SetupDiEnumDriverInfo. All the
other PInvokes i've done up to this point work fine. Whenver I
PInvoke SetupDiEnumDriverInfo though, I get winerror.h code of 1784L,
which basically says it doesn't like my buffer, which I assume is the
structure.

Below is my PInvoke, my structure and a sample of my code. Please,
please PLEASE help me!

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDriverInfo(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType, int
MemberIndex,
ref SP_DRVINFO_DATA DriverInfoData);

[StructLayout(LayoutKind.Sequential)]
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public IntPtr Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public long DriverVersion;
}

void EnumerateDriverList(IntPtr devInfoSet, SP_DEVINFO_DATA
devInfoData)
{
int memIndex = 0;
int drvErr = 0;
SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA();
drvData.cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DATA));
bool enumResult = SetupDiEnumDriverInfo(devInfoSet, ref
devInfoData, SPDIT_COMPATDRIVER,
memIndex, ref drvData);
if(!enumResult) drvErr = Marshal.GetLastWin32Error();
while (enumResult && drvErr != ERROR_NO_MORE_ITEMS)
{
// Never Gets to this point.
}
}

Mar 8 '07 #1
7 7848
"Rymfax" <cw*****@bigbangllc.comwrote in message
news:11**********************@64g2000cwx.googlegro ups.com...
>I would really appreciate it if someone could help me figure out what
I'm doing wrong trying to PInvoke SetupDiEnumDriverInfo. All the
other PInvokes i've done up to this point work fine. Whenver I
PInvoke SetupDiEnumDriverInfo though, I get winerror.h code of 1784L,
which basically says it doesn't like my buffer, which I assume is the
structure.

Below is my PInvoke, my structure and a sample of my code. Please,
please PLEASE help me!

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDriverInfo(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType, int
MemberIndex,
ref SP_DRVINFO_DATA DriverInfoData);

[StructLayout(LayoutKind.Sequential)]
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public IntPtr Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public long DriverVersion;
}

void EnumerateDriverList(IntPtr devInfoSet, SP_DEVINFO_DATA
devInfoData)
{
int memIndex = 0;
int drvErr = 0;
SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA();
drvData.cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DATA));
bool enumResult = SetupDiEnumDriverInfo(devInfoSet, ref
devInfoData, SPDIT_COMPATDRIVER,
memIndex, ref drvData);
if(!enumResult) drvErr = Marshal.GetLastWin32Error();
while (enumResult && drvErr != ERROR_NO_MORE_ITEMS)
{
// Never Gets to this point.
}
}


Try using the Unicode function by specifying [DllImport( setupapi), CharSet =
CharSet.Unicode,..]
and do the same to your structure declarations...
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]

Willy.

Mar 8 '07 #2
Willy,

I made that change and I still get the 1784L error. I'm wondering if
the problem is not with SP_DRVINFO_DATA (cause I have written that
thing every possible way I can think of), but with some other
structure or DllImport I'm using further up the chain. I'm literally
at my whits end on this. I have very similar code in C++ that works
just fine. I'm posting a C# class that has everything I'm doing in
it. I would be VERY grateful if you could take a look at it and see
if you see a problem. As above, everything works fine until it gets
to SetupDiEnumDriverInfo.

Thanks a ton!
--- CODE START ----
class GroupExample
{
public const int CR_SUCCESS = (0x00000000);
public const int DIGCF_PRESENT = (0x00000002);
public const int DIGCF_ALLCLASSES = (0x00000004);

public const int ERROR_NO_MORE_ITEMS = 259;

public const int SPDIT_COMPATDRIVER = (0x00000002);
public const int SPDIT_CLASSDRIVER = (0x00000001);

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public int Reserved;
}

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINSTALL_PARAMS
{
public int cbSize;
public int Rank;
public int Flags;
public int PrivateData;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINSTALL_PARAMS
{
public int cbSize;
public int Flags;
public int FlagsEx;
public IntPtr HwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public ulong CallInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string DriverPath;

}

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public ulong Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public ulong DriverVersion;
}

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern Boolean
SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid
ClassGuid, IntPtr Enumerator,
IntPtr hwndParent, int Flags);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr
DeviceInfoSet, int MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);

[DllImport("cfgmgr32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern int CM_Get_DevNode_Status(ref ulong
status, ref ulong probNum,
IntPtr devInst, int Flag);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool
SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int Property, ref int
PropertyRegDataType,
StringBuilder PropertyBuffer, int PropertyBufferSize, ref
int RequiredSize);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, ref
SP_DEVINSTALL_PARAMS DeviceInstallParams);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiDestroyDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiBuildDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiEnumDriverInfo(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType, int
MemberIndex,
ref SP_DRVINFO_DATA DriverInfoData);

public void RunExample()
{
Guid newG = Guid.Empty;
IntPtr NewDeviceInfoSet = SetupDiGetClassDevs(ref newG,
IntPtr.Zero, IntPtr.Zero,
DIGCF_ALLCLASSES);
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
DeviceInfoData.cbSize =
Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
int deviceCounter = 0;
bool devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
int devInfoErr = Marshal.GetLastWin32Error();
while (devInfoRetVal && devInfoErr != ERROR_NO_MORE_ITEMS
&& deviceCounter < 3) // < 3 is to keep the loop short for testing
{
ulong status = 0;
ulong problem = 0;
if (CM_Get_DevNode_Status(ref status, ref problem,
DeviceInfoData.DevInst, 0) == CR_SUCCESS)
{
StringBuilder sbDesc = new StringBuilder("");
int MAX_LEN = 512;
int propRegDataType = 0;
sbDesc.Capacity = MAX_LEN;
int reqSize = 0;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_FRIENDLYNAME, ref
propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
if (sbDesc.ToString() == "")
{

SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_DEVICEDESC,
ref propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
}
if (sbDesc.ToString() == "")
sbDesc.Append("Unknown Description");
StringBuilder sbHWID = new StringBuilder("");
sbHWID.Capacity = MAX_LEN;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_HARDWAREID, ref
propRegDataType, sbHWID,
MAX_LEN, ref reqSize);
ScanResults scanResult = new
ScanResults(sbHWID.ToString(), sbDesc.ToString());
Console.WriteLine("Device Detected: {0} - {1}",
sbDesc.ToString(), sbHWID.ToString());

SP_DEVINSTALL_PARAMS deviceInstallParams = new
SP_DEVINSTALL_PARAMS();
deviceInstallParams.cbSize =
Marshal.SizeOf(typeof(SP_DEVINSTALL_PARAMS));
SetupDiGetDeviceInstallParams(NewDeviceInfoSet,
ref DeviceInfoData,
ref deviceInstallParams);
deviceInstallParams.DriverPath = @"C:
\MyDriverPath";
if (!SetupDiBuildDriverInfoList(NewDeviceInfoSet,
ref DeviceInfoData, SPDIT_COMPATDRIVER))
{
int eCode = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Building Driver
Info List. Code: {0}", eCode);
}
int memIndex = 0;
int drvErr = 0;
SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA();
drvData.cbSize =
Marshal.SizeOf(typeof(SP_DRVINFO_DATA));
bool enumResult =
SetupDiEnumDriverInfo(NewDeviceInfoSet, ref DeviceInfoData,
SPDIT_COMPATDRIVER,
memIndex, ref drvData);
if (!enumResult)
{
drvErr = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Enumerating Driver
Info. Code: {0}", drvErr);
}
while (enumResult && drvErr !=
ERROR_NO_MORE_ITEMS)
{
memIndex++;

}
SetupDiDestroyDriverInfoList(NewDeviceInfoSet, ref
DeviceInfoData, SPDIT_COMPATDRIVER);
}
deviceCounter++;
devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
devInfoErr = Marshal.GetLastWin32Error();
}
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
}
}
--- CODE END ---

Mar 8 '07 #3
"Rymfax" <cw*****@bigbangllc.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
Willy,

I made that change and I still get the 1784L error. I'm wondering if
the problem is not with SP_DRVINFO_DATA (cause I have written that
thing every possible way I can think of), but with some other
structure or DllImport I'm using further up the chain. I'm literally
at my whits end on this. I have very similar code in C++ that works
just fine. I'm posting a C# class that has everything I'm doing in
it. I would be VERY grateful if you could take a look at it and see
if you see a problem. As above, everything works fine until it gets
to SetupDiEnumDriverInfo.

Thanks a ton!
--- CODE START ----
class GroupExample
{
public const int CR_SUCCESS = (0x00000000);
public const int DIGCF_PRESENT = (0x00000002);
public const int DIGCF_ALLCLASSES = (0x00000004);

public const int ERROR_NO_MORE_ITEMS = 259;

public const int SPDIT_COMPATDRIVER = (0x00000002);
public const int SPDIT_CLASSDRIVER = (0x00000001);

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public int Reserved;
}

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINSTALL_PARAMS
{
public int cbSize;
public int Rank;
public int Flags;
public int PrivateData;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINSTALL_PARAMS
{
public int cbSize;
public int Flags;
public int FlagsEx;
public IntPtr HwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public ulong CallInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string DriverPath;

}

[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public ulong Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public ulong DriverVersion;
}

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern Boolean
SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid
ClassGuid, IntPtr Enumerator,
IntPtr hwndParent, int Flags);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr
DeviceInfoSet, int MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);

[DllImport("cfgmgr32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern int CM_Get_DevNode_Status(ref ulong
status, ref ulong probNum,
IntPtr devInst, int Flag);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool
SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int Property, ref int
PropertyRegDataType,
StringBuilder PropertyBuffer, int PropertyBufferSize, ref
int RequiredSize);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, ref
SP_DEVINSTALL_PARAMS DeviceInstallParams);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiDestroyDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiBuildDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiEnumDriverInfo(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType, int
MemberIndex,
ref SP_DRVINFO_DATA DriverInfoData);

public void RunExample()
{
Guid newG = Guid.Empty;
IntPtr NewDeviceInfoSet = SetupDiGetClassDevs(ref newG,
IntPtr.Zero, IntPtr.Zero,
DIGCF_ALLCLASSES);
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
DeviceInfoData.cbSize =
Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
int deviceCounter = 0;
bool devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
int devInfoErr = Marshal.GetLastWin32Error();
while (devInfoRetVal && devInfoErr != ERROR_NO_MORE_ITEMS
&& deviceCounter < 3) // < 3 is to keep the loop short for testing
{
ulong status = 0;
ulong problem = 0;
if (CM_Get_DevNode_Status(ref status, ref problem,
DeviceInfoData.DevInst, 0) == CR_SUCCESS)
{
StringBuilder sbDesc = new StringBuilder("");
int MAX_LEN = 512;
int propRegDataType = 0;
sbDesc.Capacity = MAX_LEN;
int reqSize = 0;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_FRIENDLYNAME, ref
propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
if (sbDesc.ToString() == "")
{

SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_DEVICEDESC,
ref propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
}
if (sbDesc.ToString() == "")
sbDesc.Append("Unknown Description");
StringBuilder sbHWID = new StringBuilder("");
sbHWID.Capacity = MAX_LEN;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_HARDWAREID, ref
propRegDataType, sbHWID,
MAX_LEN, ref reqSize);
ScanResults scanResult = new
ScanResults(sbHWID.ToString(), sbDesc.ToString());
Console.WriteLine("Device Detected: {0} - {1}",
sbDesc.ToString(), sbHWID.ToString());

SP_DEVINSTALL_PARAMS deviceInstallParams = new
SP_DEVINSTALL_PARAMS();
deviceInstallParams.cbSize =
Marshal.SizeOf(typeof(SP_DEVINSTALL_PARAMS));
SetupDiGetDeviceInstallParams(NewDeviceInfoSet,
ref DeviceInfoData,
ref deviceInstallParams);
deviceInstallParams.DriverPath = @"C:
\MyDriverPath";
if (!SetupDiBuildDriverInfoList(NewDeviceInfoSet,
ref DeviceInfoData, SPDIT_COMPATDRIVER))
{
int eCode = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Building Driver
Info List. Code: {0}", eCode);
}
int memIndex = 0;
int drvErr = 0;
SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA();
drvData.cbSize =
Marshal.SizeOf(typeof(SP_DRVINFO_DATA));
bool enumResult =
SetupDiEnumDriverInfo(NewDeviceInfoSet, ref DeviceInfoData,
SPDIT_COMPATDRIVER,
memIndex, ref drvData);
if (!enumResult)
{
drvErr = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Enumerating Driver
Info. Code: {0}", drvErr);
}
while (enumResult && drvErr !=
ERROR_NO_MORE_ITEMS)
{
memIndex++;

}
SetupDiDestroyDriverInfoList(NewDeviceInfoSet, ref
DeviceInfoData, SPDIT_COMPATDRIVER);
}
deviceCounter++;
devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
devInfoErr = Marshal.GetLastWin32Error();
}
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
}
}
--- CODE END ---
Your struct should look like this (partly):

[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)] <-----1
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public IntPtr Reserved; <--------------2

1) You need to use the correct Packing (the same as the packing used by the C structure in
the setupapi.h).
2) Why did you change this into a ulong?

Note that I diddn't check the other structs!!!!

WIlly.

Mar 8 '07 #4
On Mar 8, 3:50 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Rymfax" <cwal...@bigbangllc.comwrote in message

news:11**********************@p10g2000cwp.googlegr oups.com...


Willy,
I made that change and I still get the 1784L error. I'm wondering if
the problem is not with SP_DRVINFO_DATA (cause I have written that
thing every possible way I can think of), but with some other
structure or DllImport I'm using further up the chain. I'm literally
at my whits end on this. I have very similar code in C++ that works
just fine. I'm posting a C# class that has everything I'm doing in
it. I would be VERY grateful if you could take a look at it and see
if you see a problem. As above, everything works fine until it gets
to SetupDiEnumDriverInfo.
Thanks a ton!
--- CODE START ----
class GroupExample
{
public const int CR_SUCCESS = (0x00000000);
public const int DIGCF_PRESENT = (0x00000002);
public const int DIGCF_ALLCLASSES = (0x00000004);
public const int ERROR_NO_MORE_ITEMS = 259;
public const int SPDIT_COMPATDRIVER = (0x00000002);
public const int SPDIT_CLASSDRIVER = (0x00000001);
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public IntPtr DevInst;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINSTALL_PARAMS
{
public int cbSize;
public int Rank;
public int Flags;
public int PrivateData;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DEVINSTALL_PARAMS
{
public int cbSize;
public int Flags;
public int FlagsEx;
public IntPtr HwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public ulong CallInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string DriverPath;
}
[StructLayout(LayoutKind.Sequential, CharSet =
CharSet.Unicode)]
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public ulong Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string MfgName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string ProviderName;
public System.Runtime.InteropServices.ComTypes.FILETIME
DriverDate;
public ulong DriverVersion;
}
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern Boolean
SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid
ClassGuid, IntPtr Enumerator,
IntPtr hwndParent, int Flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr
DeviceInfoSet, int MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("cfgmgr32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern int CM_Get_DevNode_Status(ref ulong
status, ref ulong probNum,
IntPtr devInst, int Flag);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool
SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int Property, ref int
PropertyRegDataType,
StringBuilder PropertyBuffer, int PropertyBufferSize, ref
int RequiredSize);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, ref
SP_DEVINSTALL_PARAMS DeviceInstallParams);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiDestroyDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiBuildDriverInfoList(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType);
[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiEnumDriverInfo(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, int DriverType, int
MemberIndex,
ref SP_DRVINFO_DATA DriverInfoData);
public void RunExample()
{
Guid newG = Guid.Empty;
IntPtr NewDeviceInfoSet = SetupDiGetClassDevs(ref newG,
IntPtr.Zero, IntPtr.Zero,
DIGCF_ALLCLASSES);
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
DeviceInfoData.cbSize =
Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
int deviceCounter = 0;
bool devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
int devInfoErr = Marshal.GetLastWin32Error();
while (devInfoRetVal && devInfoErr != ERROR_NO_MORE_ITEMS
&& deviceCounter < 3) // < 3 is to keep the loop short for testing
{
ulong status = 0;
ulong problem = 0;
if (CM_Get_DevNode_Status(ref status, ref problem,
DeviceInfoData.DevInst, 0) == CR_SUCCESS)
{
StringBuilder sbDesc = new StringBuilder("");
int MAX_LEN = 512;
int propRegDataType = 0;
sbDesc.Capacity = MAX_LEN;
int reqSize = 0;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_FRIENDLYNAME, ref
propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
if (sbDesc.ToString() == "")
{
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_DEVICEDESC,
ref propRegDataType, sbDesc,
MAX_LEN, ref reqSize);
}
if (sbDesc.ToString() == "")
sbDesc.Append("Unknown Description");
StringBuilder sbHWID = new StringBuilder("");
sbHWID.Capacity = MAX_LEN;
SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,
ref DeviceInfoData,
(int)Setup.SPDRPCodes.SPDRP_HARDWAREID, ref
propRegDataType, sbHWID,
MAX_LEN, ref reqSize);
ScanResults scanResult = new
ScanResults(sbHWID.ToString(), sbDesc.ToString());
Console.WriteLine("Device Detected: {0} - {1}",
sbDesc.ToString(), sbHWID.ToString());
SP_DEVINSTALL_PARAMS deviceInstallParams = new
SP_DEVINSTALL_PARAMS();
deviceInstallParams.cbSize =
Marshal.SizeOf(typeof(SP_DEVINSTALL_PARAMS));
SetupDiGetDeviceInstallParams(NewDeviceInfoSet,
ref DeviceInfoData,
ref deviceInstallParams);
deviceInstallParams.DriverPath = @"C:
\MyDriverPath";
if (!SetupDiBuildDriverInfoList(NewDeviceInfoSet,
ref DeviceInfoData, SPDIT_COMPATDRIVER))
{
int eCode = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Building Driver
Info List. Code: {0}", eCode);
}
int memIndex = 0;
int drvErr = 0;
SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA();
drvData.cbSize =
Marshal.SizeOf(typeof(SP_DRVINFO_DATA));
bool enumResult =
SetupDiEnumDriverInfo(NewDeviceInfoSet, ref DeviceInfoData,
SPDIT_COMPATDRIVER,
memIndex, ref drvData);
if (!enumResult)
{
drvErr = Marshal.GetLastWin32Error();
Console.WriteLine("\tError Enumerating Driver
Info. Code: {0}", drvErr);
}
while (enumResult && drvErr !=
ERROR_NO_MORE_ITEMS)
{
memIndex++;
}
SetupDiDestroyDriverInfoList(NewDeviceInfoSet, ref
DeviceInfoData, SPDIT_COMPATDRIVER);
}
deviceCounter++;
devInfoRetVal =
SetupDiEnumDeviceInfo(NewDeviceInfoSet, deviceCounter, ref
DeviceInfoData);
devInfoErr = Marshal.GetLastWin32Error();
}
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
}
}
--- CODE END ---

Your struct should look like this (partly):

[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)] <-----1
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public IntPtr Reserved;
...

read more »- Hide quoted text -

- Show quoted text -
Willy, You are a GOD among men!!!!

Do you think you could explain to me what the Pack = 4 does and why I
need it for this structure but not any of my others?

Thanks! Would you like my first born? He's yours! :)

Mar 8 '07 #5
"Rymfax" <cw*****@bigbangllc.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
On Mar 8, 3:50 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Rymfax" <cwal...@bigbangllc.comwrote in message

news:11**********************@p10g2000cwp.googlegr oups.com...
Your struct should look like this (partly):

[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Unicode)] <-----1
public struct SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public IntPtr Reserved;
...

read more »- Hide quoted text -

- Show quoted text -
Willy, You are a GOD among men!!!!

Do you think you could explain to me what the Pack = 4 does and why I
need it for this structure but not any of my others?

The problem is with this:

public System.Runtime.InteropServices.ComTypes.FILETIME DriverDate;
public ulong DriverVersion;
}
DriverDate, being a long (8 bytes) will be aligned at its natural boundary with the default
packing (Pack = 0), that means that it will be stored at a address which is a multiple of 8.
However, if you look at the preceeding fields, DriverDate would not end on such an address,
so, the lay-out manager will pad the preceeding field with 4 bytes (the fixed char array),
and as such render the struct incompatible (both in length and some field addresses) with
what the function is expecting.
When setting the Packing to 4 (the same as used in the header file) , no such padding is
done and the struct length corresponds to what the function expects.
Thanks! Would you like my first born? He's yours! :)

Wow, but thanks, too much honor really ;-).

Willy.

Mar 8 '07 #6
Willy,

Would love your help one more time please! I'm now trying to get
SetupDiGetDriverInfoDetail to work. I'm getting the same 1784L
error. I was hoping you could take a look again. My struct/invoke/
code is below. The new snippet of code is just being run insde the
while loop of the previous code. Thanks! Oh, and FYI, I tried this
struct with and without the Pack=4.
[StructLayout(LayoutKind.Sequential, Pack=4, CharSet =
CharSet.Unicode)]
public struct SP_DRVINFO_DETAIL_DATA
{
public int cbSize;
public System.Runtime.InteropServices.ComTypes.FILETIME
InfDate;
public int CompatIDsOffset;
public int CompatIDsLength;
public ulong Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string SectionName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string InfFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string DrvDescription;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string HardwareID;
}

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
private static extern bool SetupDiGetDriverInfoDetail(IntPtr
DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DRVINFO_DATA
DriverInfoData,
ref SP_DRVINFO_DETAIL_DATA DriverInfoDetailData, int
DriverInfoDetailDataSize,
ref int RequiredSize);

// New Code Snipped

string infPath = "";
int drvRank = 0;
SP_DRVINSTALL_PARAMS drvParams = new
SP_DRVINSTALL_PARAMS();
drvParams.cbSize =
Marshal.SizeOf(typeof(SP_DRVINSTALL_PARAMS));
if (SetupDiGetDriverInstallParams(devInfoSet, ref
devInfoData, ref drvData, ref drvParams))
{
drvRank = drvParams.Rank;
}
int dataSize = 2048;
SP_DRVINFO_DETAIL_DATA drvInfDetail = new
SP_DRVINFO_DETAIL_DATA();
drvInfDetail.cbSize =
Marshal.SizeOf(typeof(SP_DRVINFO_DETAIL_DATA));
if (!SetupDiGetDriverInfoDetail(devInfoSet, ref
devInfoData, ref drvData, ref drvInfDetail,
dataSize, ref dataSize))
{
int didErr = Marshal.GetLastWin32Error();
infPath = "blah";
}
memIndex++;
enumResult = SetupDiEnumDriverInfo(devInfoSet, ref
devInfoData, SPDIT_COMPATDRIVER,
memIndex, ref drvData);

Mar 8 '07 #7
"Rymfax" <cw*****@bigbangllc.comwrote in message
news:11********************@30g2000cwc.googlegroup s.com...
Willy,

Would love your help one more time please! I'm now trying to get
SetupDiGetDriverInfoDetail to work. I'm getting the same 1784L
error. I was hoping you could take a look again. My struct/invoke/
code is below. The new snippet of code is just being run insde the
while loop of the previous code. Thanks! Oh, and FYI, I tried this
struct with and without the Pack=4.
[StructLayout(LayoutKind.Sequential, Pack=4, CharSet =
CharSet.Unicode)]
public struct SP_DRVINFO_DETAIL_DATA
{
public int cbSize;
public System.Runtime.InteropServices.ComTypes.FILETIME
InfDate;
public int CompatIDsOffset;
public int CompatIDsLength;
public ulong Reserved;

public IntPtr Reserved;

Note, as I said previously, I didn't check the other structs for packing issues and the
like. You really have to check the header files before defining your interop stucts (or stay
with C++ :-)).
Willy.

Mar 9 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
4
by: Sačo Zagoranski | last post by:
Hi, I'm trying to play an AVI file from memory with MCI. The documentation is great if you want to play a file from a file but directly from memory... almost nothing. After hours of...
3
by: Michael | last post by:
Hi all, I believe I'm not PInvoking this struct correctly. Here's the API definition. typedef struct _SP_DRVINFO_DATA { DWORD cbSize; DWORD DriverType; ULONG_PTR Reserved; TCHAR ...
3
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
1
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i get a "Wrong Parameter" from Marshal.GetLastWin32Error() on using this: public class SP_DEVINFO_DATA { public int cbSize; public Guid classGuid;
3
by: Siegfried Heintze | last post by:
Can someone kindly help me finish my attempt at pinvoke? HKL is a void* and I don't know how to translate it. //static extern UInt32 MapVirtualKeyExW(UInt32 uCode, UInt32 uMapType, HKL dwhkl); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.