PInvoke with Function SetupDiGetDeviceInstanceId

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hy,

Can anybody tell me how to get the function SetupDiGetDeviceInstanceId over
Platform Invoke Call to my C# Application.

I tried to code it, but I always get an error.
Can you help me and send a little example code ?

My E-Mail Adress is (e-mail address removed)

[DllImport("SetupApi.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern Boolean
SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
ref StringBuilder DeviceInstanceId,
Int32 DeviceInstanceIdSize,
ref Int32 RequiredSize);
 
can you post your code and the error you get please.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

Hy,

Can anybody tell me how to get the function SetupDiGetDeviceInstanceId over
Platform Invoke Call to my C# Application.

I tried to code it, but I always get an error.
Can you help me and send a little example code ?

My E-Mail Adress is (e-mail address removed)

[DllImport("SetupApi.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern Boolean
SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
ref StringBuilder DeviceInstanceId,
Int32 DeviceInstanceIdSize,
ref Int32 RequiredSize);



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Her is the Code:

[DllImport("SetupApi.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern Boolean
SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
ref StringBuilder DeviceInstanceId,
Int32 DeviceInstanceIdSize,
ref Int32 RequiredSize);


StringBuilder InstanceId = new StringBuilder("");
InstanceId.Capacity = DevManLib.MAX_DEV_LEN;
Int32 ReqSize = new Int32();
if(!DevManLib.SetupDiGetDeviceInstanceId(NewDeviceInfoSet,
DeviceInfoData,ref InstanceId,0, ref ReqSize))
{
ReqSize = 0;
res = DevManLib.SetupDiGetDeviceInstanceId(NewDeviceInfoSet,
DeviceInfoData,ref InstanceId,400, ref ReqSize);
Console.WriteLine
(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
if(!res)
{
//incorrect device name:
DevManLib.SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
InstanceId=new StringBuilder("");
return -4;
}
}

The Exception I get is :
Unhandled Exception, Type System.ExecutionEngineException with no details.
 
For one thing - the second parameter DeviceInfoData is a pointer to a
structure. Define it as IntPtr; use Marshal.StructureToPtr to return a
pointer to your filled out structure and pass that pointer in as the second
parameter.

hope that helps..
Imran.
 
Back
Top