extracting string from []byte

setupdigetdeviceregistryproperty , pinvoke , . csharp , .net Oxford, United Kingdom
  • 11 years ago

    Hello. This is a P/Invoke question which, quite frankly, is doing my head in.

    I need to get a string from a []byte because calling the object.Tostring() method on he variable doesn't yield the correct information.

    given the following (which was extracted from p/invoke.net:

    Code: public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SPDEVINFODATA DeviceInfoData, uint Property, out uint PropertyRegDataType, byte[] PropertyBuffer, uint PropertyBufferSize, out uint RequiredSize);

    result = SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref tBuff, (uint)SPDRP.HARDWAREID, out RegType, null, 0, out RequiredSize);

    ptrBuf = new byte[RequiredSize]; buffersize = RequiredSize;

    result = SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref tBuff, (uint)SPDRP.HARDWAREID, out RegType, ptrBuf, buffersize, out RequiredSize); I need to retrieve the string returned in that variableI tried string tmp = Marshal.PtrToStringAuto(ptrBuf) but that doesn't work; although this is the way it's used in the Link Text example. Then I tried using Encodings (Encoding.Unicode.Getstring() ) and coughed up several other problems. In short I just need a reliable way to retrieve a string from a []byte and be able to output it to the label on my form

    Thank you very much.

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann