SetupDiGetDeviceRegistryProperty

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

SetupDiGetDeviceRegistryProperty

Postby pandasanta » Mon Apr 19, 2010 2:46 pm

I tried running the example for this method, but I get an error at this line:

string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf)

I think the reason is because ptrBuf is defined as a byte[].

I am trying to invoke SetupDiGetDeviceRegistryProperty in a C# application in order to obtain the device descriptions/names from a device information set of net and modem devices. I'm not getting an error, but I'm not getting the desired result. Here is my code:

Code: Select all
...
IntPtr ptrBuffer = new IntPtr();
UInt32 requiredSize;
UInt32 regType = REG_SZ (which is defined as 1);
string deviceName;

if ( SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref devInfoData, SPDRP_DEVICEDESC, out regType, out ptrBuffer, BUFFER_SIZE, out requiredSize) )
{   
     deviceName = Marshal.PtrToStringAuto(ptrBuffer);
     MessageBox.Show("Device Name = " + deviceName);
} else
{   
     MessageBox.Show(GetLastWin32Error());
}
...


The code runs, but the message box only has "Device Name = ". I had originally tried using SPDRP_FRIENDLYNAME, but I got an invalid data value error, or something like that. How do I get that device name to display? Eventually, I want the device name to actually be added to a list box. But for now I just want to see it being returned properly.

Thanks!
pandasanta
 
Posts: 3
Joined: Mon Apr 19, 2010 2:33 pm

SetupDiGetDeviceRegistryProperty - resolved

Postby pandasanta » Tue May 11, 2010 5:27 pm

I figured this out.

I changed the line:

Code: Select all
string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf)


to read:

Code: Select all
string ControllerDeviceDesc = Marshal.PtrToStringAnsi(ptrBuf)


I also allocated the size of the ptrBuf:

Code: Select all
ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE).


I have updated the PInvoke with this information. I hope it helps someone else!

[/code]
pandasanta
 
Posts: 3
Joined: Mon Apr 19, 2010 2:33 pm


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests