1

I want to get user-friendly names of sound inputs with this code, but it can give me only first 32 chars of name, but I want it whole.

[DllImport("winmm.dll", SetLastError = true)]
static extern uint waveInGetNumDevs();

[DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint waveInGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WAVEOUTCAPS
{
    public ushort wMid;
    public ushort wPid;
    public uint vDriverVersion;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string szPname;
    public uint dwFormats;
    public ushort wChannels;
    public ushort wReserved1;
    public uint dwSupport;
}

public static string[] GetSoundDevices()
{
    uint devices = waveInGetNumDevs();
    string[] result = new string[devices];
    WAVEOUTCAPS caps = new WAVEOUTCAPS();
    using (StreamWriter sw = new StreamWriter("appdata/audio/name"))
    {
        for (uint i = 0; i < devices; i++)
        {
            waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
            result[i] = caps.szPname;
            sw.WriteLine(caps.szPname);
        }
        return result;
    }
}

I need this names of sound inputs: enter image description here

but this code give me only this:

enter image description here

Thank you guys!

  • Did you try to change the `64`in `[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]`? – DrKoch Apr 30 '15 at 12:20
  • @DrKoch it should be 32 according to the specification for that data structure, I think he tried changing it to 64 to see if it would get more data, but unfortunately it may corrupt the rest of the structure, he's just not seeing it because he's not using the rest. – Ron Beyer Apr 30 '15 at 12:22
  • It bothers me that you're using `TStr`, this is not a wide function. – Blindy Apr 30 '15 at 12:39

3 Answers3

0

You may have to use the management interface:

ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
           "SELECT * FROM Win32_SoundDevice");

    ManagementObjectCollection objCollection = objSearcher.Get();

    foreach (ManagementObject obj in objCollection)
    {
        foreach (PropertyData property in obj.Properties)
        {
            Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
        }
    }

Source: How to enumerate audio out devices in c#

Community
  • 1
  • 1
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
0

You can use WASAPI to get full name of sound devices. It's only limit is that it is not available in Windows XP or older OS. You need to use IMMDeviceEnumerator::EnumAudioEndpoints method to achieve the goal, but since the library is COM, you need to wrap it to be able to use it in C#. A sample project is available on CodeProject.

I also couldn't find a way to get full name using WinMM library and finally end up writing a C++ wrapper around WASAPI and used that wrapper in C# through pinvoke!

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
0

Why screenshots in foreign and totally not understandable language? There you go. English screenshot 1 You can copy it, that is what the second one means.

The number stands for the usb bus that is used. When changing the plug keep that in mind.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32214621) – inwerpsel Jul 15 '22 at 13:04