Skip to main content

Marshalling strings with StringBuilder

The dotNet framework is pretty smart when it comes to marshalling managed to unmanaged datatypes for PInvoke calls. When you want to pass a Guid from the managed to the unmanaged world the framework knows how to transform the .Net Guid structure to a Windows GUID without you even asking for it.

Marshalling strings is usually no problem either. You can pass in a managed string and the .Net framework handles any unicode to ANSI conversions for you.

   1: [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
   2: public static extern bool SetupDiClassGuidsFromName(
   3:     string className, ref Guid guids, UInt32 classGuidListSize, ref UInt32 requiredSize);
   5: [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
   6: public static extern bool SetupDiClassNameFromGuid( 
   7:     ref Guid classGuid, string className, UInt32 classNameSize, ref UInt32 requiredSize);

These two calls from the setupapi.dll library illustrate this. The first call takes a string describing a device-class (for example keyboard) and returns an array of Guids with usually one Guid for that device class (4D36E96B-E325-11CE-BFC1-08002BE10318 for keyboards). The classGuidListSize parameter tells the dll how big we made the guids array. If it's not big enoug we can check the requiredSize parameter after the call and call again with a bigger array.

The second call is a problem though. It's exactly the reverse of the first call. It takes a single Guid and returns a string. The problem here is that in the managed world we dont have much influence over the size of strings. That's all handled for us.

One hack could be to pass in an empty string, with zero in classNameSize, and pass in a string padded with the appropriate amount of spaces the second time around to make it big enoug to hold the amount of characters in requiredSize but that would be a bit messy.

The preferred way is to pass in a StringBuilder class instead of a string. We can use the Capacity property of the StringBuilder to make it any size we want.

   1: [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
   2: public static extern bool SetupDiClassNameFromGuid( 
   3:     ref Guid classGuid, StringBuilder className, UInt32 classNameSize, ref UInt32 requiredSize);
   5: public string ClassNameFromGuidWrapper(Guid classGuid)
   6: {
   7:     // 50 is a Sensible default value, if we're lucky it's enough
   8:     uint reqSize = 50;
   9:     StringBuilder classNameBuilder = new StringBuilder((int)reqSize);
  11:     // First try.
  12:     returnValue = _SetupApi.SetupDiClassNameFromGuid(
  13:         ref classGuid, classNameBuilder, (uint)classNameBuilder.Capacity, ref reqSize);
  15:     if ((uint)classNameBuilder.Capacity != reqSize)
  16:     {
  17:         // call again with the right size stringbuilder
  18:         classNameBuilder.Capacity = (int)reqSize;
  19:         returnValue = _SetupApi.SetupDiClassNameFromGuid(
  20:             ref classGuid, classNameBuilder, reqSize, ref reqSize);
  21:     }
  23:     return classNameBuilder.ToString();
  24: }


Popular posts from this blog

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

The big move

People who visit here more often probably noticed that my blog has been down a couple of times the last few months. The problem was that after giving up on hosting it on an (expensive!) windows server I tried unsuccessfully to host my blog on Ubuntu with Mono. I could probably get this to work but it takes too much time and effort. So I decided to move to a hosted blog on Blogger. This way I can spend more time blogging and less time on blog administration.If you’re on a stylish white blogger page right now you’re already on the new blog. Have fun and expect most of my old content to appear there over time. If you’re on my old coffee themed blog you’ll probably need to move to Or you can wait. As soon as I have all my content moved over I’ll point to the new blog too.