在C#中控制应用程序卷的问题(InvalidCastException)

时间:2015-08-05 23:13:51

标签: c# try-catch

问题:

当我不使用try catch块时,调试器给了我一个例外,我可以在visual studio 2013中单击“Continue”10次,然后程序似乎正常运行。但当我在try / catch中包围它时,代码块内部没有任何变化似乎已被执行,或者代码块内的变量不会转移到try块的外部。

我试图从这些问题中获取代码:

Controling Volume Mixer  和 Controlling Application's Volume: By Process-ID

代码:

private static ISimpleAudioVolume GetVolumeObject(int pid)
{
        // get the speakers (1st render + multimedia) device
        IMMDeviceEnumerator deviceEnumerator = null;
        string str = "I havent changed :(";
        try
        {
            deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            str = "good to go";
        }
        catch
        {
            Console.WriteLine("ohh nooo, there was an exception");
        }
        if (deviceEnumerator == null)
        {
            Console.WriteLine("ouch, its null");
            Console.WriteLine(str);
            return null;
        }

        IMMDevice speakers;
        deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);

        // activate the session manager. we need the enumerator
        Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
        object o;
        speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
        IAudioSessionManager2 mgr = (IAudioSessionManager2)o;

        // enumerate sessions for on this device
        IAudioSessionEnumerator sessionEnumerator;
        mgr.GetSessionEnumerator(out sessionEnumerator);
        int count;
        sessionEnumerator.GetCount(out count);

        // search for an audio session with the required name
        // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2)
        ISimpleAudioVolume volumeControl = null;
        for (int i = 0; i < count; i++)
        {
            IAudioSessionControl2 ctl;
            sessionEnumerator.GetSession(i, out ctl);
            int cpid;
            ctl.GetProcessId(out cpid);

            if (cpid == pid)
            {
                volumeControl = ctl as ISimpleAudioVolume;
                break;
            }
            Marshal.ReleaseComObject(ctl);
        }
        Marshal.ReleaseComObject(sessionEnumerator);
        Marshal.ReleaseComObject(mgr);
        Marshal.ReleaseComObject(speakers);
        Marshal.ReleaseComObject(deviceEnumerator);
        return volumeControl;
}

输出:

    ohh nooo, there was an exception
    ouch, its null
    I havent changed :(

编辑:

    [ComImport]
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
internal class MMDeviceEnumerator
{
}

internal enum EDataFlow
{
    eRender,
    eCapture,
    eAll,
    EDataFlow_enum_count
}

internal enum ERole
{
    eConsole,
    eMultimedia,
    eCommunications,
    ERole_enum_count
}

[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMMDeviceEnumerator
{
    int NotImpl1();

    [PreserveSig]
    int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppDevice);

    // the rest is not implemented
}

0 个答案:

没有答案