0

Issue:

When I do not use the try catch block the debugger gives me an exception and I can click "Continue" in visual studio 2013 10 times and then the program appears to run correctly. but when I surround it in try/catch, nothing that changed inside the code block seems to have been executed or variables changes made inside the block of code do not carry over to the outside of the try block.

Im trying to get the code working from these questions:

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

Code:

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;
}

Output:

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

EDIT:

    [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
}
Community
  • 1
  • 1
coderguy22296
  • 61
  • 1
  • 7
  • Try `catch (Exception e) { Console.WriteLine(e.Message); }` for something more meaningful. You could also print the stack trace from `e`. Does `MMDeviceEnumerator` implement `IMMDeviceEnumerator`? – adamdc78 Aug 05 '15 at 23:23
  • What makes you think anything did change? You're getting an exception; the only place in that block that could happen is before the assignment of `deviceEnumerator`. Catching the exception doesn't magically make the rest of the statements in the block after the exception execute. They won't! You need to look at the exception (in the debugger or via a variable declared in the `catch` statement) and see why the exception is happening and fix it. Catching an exception you didn't expect is almost always the wrong thing to do; you need to make the code not cause the exception in the first place. – Peter Duniho Aug 05 '15 at 23:50
  • Im not sure if MMDeviceEnumerator implement IMMDeviceEnumerator, but I added the code for the COM Imports for those classes/interfaces. – coderguy22296 Aug 06 '15 at 09:13
  • Unable to cast object of type 'MMDeviceEnumerator' to type 'testCode.MMDeviceEnumerator'. the exception is an InvalidCastException – coderguy22296 Aug 06 '15 at 09:25

0 Answers0