Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created November 21, 2024 08:09
Show Gist options
  • Select an option

  • Save unitycoder/c734553dbdebab02f05e802d80bd92ad to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/c734553dbdebab02f05e802d80bd92ad to your computer and use it in GitHub Desktop.
c# console app, list Microphone Devices
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("winmm.dll", SetLastError = true)]
private static extern uint waveInGetNumDevs();
[DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint waveInGetDevCaps(uint deviceId, ref WAVEINCAPS waveInCaps, uint size);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct WAVEINCAPS
{
public ushort wMid; // Manufacturer ID
public ushort wPid; // Product ID
public uint vDriverVersion; // Driver version
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szPname; // Product name
public uint dwFormats; // Supported formats
public ushort wChannels; // Number of channels
public ushort wReserved1; // Reserved
public uint dwSupport; // Optional functionality supported
}
static void Main(string[] args)
{
Console.WriteLine("Listing available microphone devices:");
uint numDevices = waveInGetNumDevs();
Console.WriteLine($"Number of input devices found: {numDevices}");
if (numDevices == 0)
{
Console.WriteLine("No input devices found.");
return;
}
for (uint deviceId = 0; deviceId < numDevices; deviceId++)
{
var waveInCaps = new WAVEINCAPS();
uint result = waveInGetDevCaps(deviceId, ref waveInCaps, (uint)Marshal.SizeOf(typeof(WAVEINCAPS)));
if (result == 0) // MMSYSERR_NOERROR
{
Console.WriteLine($"Device {deviceId}: {waveInCaps.szPname}, Channels: {waveInCaps.wChannels}");
}
else
{
Console.WriteLine($"Error retrieving device information for device {deviceId}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment