Red Gate forums :: View topic - SetupDiGetDeviceRegistryProperty
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

SetupDiGetDeviceRegistryProperty

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
pandasanta



Joined: 19 Apr 2010
Posts: 3

PostPosted: Mon Apr 19, 2010 2:46 pm    Post subject: SetupDiGetDeviceRegistryProperty Reply with quote

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:

...
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!
Back to top
View user's profile Send private message
pandasanta



Joined: 19 Apr 2010
Posts: 3

PostPosted: Tue May 11, 2010 5:27 pm    Post subject: SetupDiGetDeviceRegistryProperty - resolved Reply with quote

I figured this out.

I changed the line:

Code:
string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf)


to read:

Code:
string ControllerDeviceDesc = Marshal.PtrToStringAnsi(ptrBuf)


I also allocated the size of the ptrBuf:

Code:
ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE).


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

[/code]
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group