Device Name Truncation with WaveOut.GetCapabilities

Feb 2, 2013 at 12:50 AM
I'm having a bit of a problem distinguishing device names due to the string truncation. I need to support XP users so I'm limited to WaveOut.GetCapabilities.

This might be a possible fix to the problem for the NAudio source.

It appears that the device name length is determined in the WaveOutCapabilities.cs file.
located in the source at ...\NAudio\Wave\MmeInterop\WaveOutCapabilities.cs
line 48 private const int MaxProductNameLength = 32;

I wonder if changing that value (say triple it = 96) will eliminate most of the device name truncation issues?

Then testing this simple code to see if the returned device names are longer.
for (int deviceId = 0; deviceId < deviceCount; deviceId++)
                var capabilities = WaveOut.GetCapabilities(deviceId);
                string name = capabilities.ProductName;
Sorry that I can't test this myself or I would. The NAudio solution source code won't build for me with VS2010-- lots of missing namespaces.
Feb 2, 2013 at 8:27 AM
unfortunately, this is a limitation of the WaveOut APIs themselves. 32 is the value of MAXPNAMELEN as used by WAVEOUTCAPS. Changing this value in NAudio will not get you a longer product name. There is only one workaround I know of and it involves using DirectSound. I'd imagine there might be a way of searching in the registry too.
Feb 2, 2013 at 10:42 AM
Ok, Thanks Mark.
The Ghost of Bill Gates strikes again. Who knew a device name would ever exceed a massive 32 bytes of memory? ;-)

I'll make do with a 32 character name. I'm trying to get away from DirectSound.
Mar 17 at 12:03 PM
Edited Apr 28 at 10:44 AM
Hi, I want to share my solution in c# to match waveout device and MMDevice, but for Windows 7.
Use GetWaveOutEndpointId() to get Guid of the waveout device.
Use this guid to get MMDevice
mmdevice = (new MMDeviceEnumerator()).GetDevice(guid);

static extern Int32 waveOutGetNumDevs();
static extern Int32 waveOutMessage(IntPtr hWaveOut, int uMsg, out int dwParam1, IntPtr dwParam2);
static extern Int32 waveOutMessage(IntPtr hWaveOut, int uMsg, IntPtr dwParam1, int dwParam2);

string GetWaveOutEndpointId(int devNumber)
    int cbEndpointId;
    string result = string.Empty;
    waveOutMessage((IntPtr)devNumber, DRV_QUERYFUNCTIONINSTANCEIDSIZE, out cbEndpointId, IntPtr.Zero);
    IntPtr strPtr = Marshal.AllocHGlobal(cbEndpointId);
    waveOutMessage((IntPtr)devNumber, DRV_QUERYFUNCTIONINSTANCEID, strPtr, cbEndpointId);
    result = Marshal.PtrToStringAuto(strPtr);
    return result;

public List<WaveOutDevice> GetWaveOutDevices()
    List<WaveOutDevice> retVal = new List<WaveOutDevice>();
    foreach (MMDevice d in (new NAudio.CoreAudioApi.MMDeviceEnumerator()).EnumerateAudioEndPoints(DataFlow.Render, DeviceState.All))
        if (d.State != DeviceState.Active) continue;
        WaveOutDevice di = new WaveOutDevice()
            EndpointGuid = d.ID,
            FullName = d.FriendlyName,
            WaveOutDeviceId = -1

        for (int waveOutIdx = 0; waveOutIdx < waveOutGetNumDevs(); waveOutIdx++)
            string guid = GetWaveOutEndpointId(waveOutIdx);
            if (guid == di.EndpointGuid)
                di.WaveOutDeviceId = waveOutIdx;
    return retVal;
Mar 17 at 12:09 PM
nice! thanks for sharing
Mar 17 at 12:42 PM
Edited Mar 17 at 12:45 PM
You are welcome Mark! It would be nice to have GetWaveOutEndpointId in WaveOut class in NAudio :) It should be also similar for WaveIn
Mar 17 at 4:25 PM
Sure, you're welcome to submit a pull request (over at Github of course) to add in GetWaveOutEndpointId. Obviously though we can't require that users have access to any MMDevice functions though, since NAudio still supports Windows XP.
Mar 18 at 1:37 PM
I solved this a different way which does not require the DllImport or Marshalling. (maybe be more efficient, not sure)

First You need to do a simple check that the computer is running Windows Vista Or Higher

I create a collection of MMDeviceCollection objects.
Each object has these properties
  • FriendlyName (which is the full device name, not limited to 32 characters)
  • ID (in this format {}.{73d4795c-5a20-4a6d-80ef-6b86e31ea1f1} , note the GUID is included)
    • so then you can just parse out the GUID and can match it to the GUID from the WaveOut.GetCapabilities snippet above
C# Partial Code snippet
  MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();

                MMDeviceCollection devCollection = DevEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);

        string guidStr;
                string friendName = "";
                int j = 0;
                foreach (var item in devCollection)

                    devId = item.ID;  // format {}.{73d4795c-5a20-4a6d-80ef-6b86e31ea1f1};

                    int dotFour = devId.LastIndexOf(".") + 1;
                    guidStr = devId.Substring(dotFour);
                    friendName = item.FriendlyName;
                    // now you have GUID and full device name, call a method to populate your device list as needed

Sep 2 at 5:44 AM
Hi theserus

I am having the same issue with two identical USB sound cards connected the PC would have the same truncated name in NAudio.

I'll be trying this workaround pretty soon.
Sep 2 at 12:04 PM
FYI, If your computer Windows OS is NOT XP (ie it is Vista or higher), you can get full names (not truncated) with this code. However, the this code will cause an error exception on XP.
 if (IsOSWindowsVistaOrHigher)
    MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
    // DataFlow.All gets all devs mics, speakers and mixer
    // DataFlow.Render is just output devices
    MMDeviceCollection devColl = DevEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);