usr021986
usr021986

Reputation: 3511

Detecting Audio Input & output devices connected to system

I want to findout how many audio input and output devices connected to my system in C#.net

Please suggest any.

Thanks.

Upvotes: 1

Views: 6126

Answers (1)

Nikki Locke
Nikki Locke

Reputation: 146

You need to access the necessary Windows API functions.

This class should get you started - Win32.GetSoundDevices returns a list of device names. Look up WAVEOUTCAPS in the Windows SDK for details of the other information you can get.

public class Win32 {
    [DllImport("winmm.dll", SetLastError=true)]
    static extern uint waveOutGetNumDevs();

    [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern uint waveOutGetDevCaps(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 = 32)]
        public string szPname;
        public uint dwFormats;
        public ushort wChannels;
        public ushort wReserved1;
        public uint dwSupport;
    }

    public static string [] GetSoundDevices() {
        uint devices = waveOutGetNumDevs();
        string [] result = new string[devices];
        WAVEOUTCAPS caps = new WAVEOUTCAPS();

        for(uint i = 0; i < devices; i++) {
            waveOutGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
            result[i] = caps.szPname;
        }
        return result;
    }


}

Upvotes: 8

Related Questions