volume

Joined
Feb 14, 2004
Messages
11
Location
London
Can anyone tell me why this code:

using System;
using System.Runtime.InteropServices;

namespace VolumeTest
{
public class VolumeControl
{
[DllImport("winmm.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)]
public static extern int waveOutSetVolume(int uDeviceID, int dwVolume);

[DllImport("winmm.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)]
public static extern int waveOutGetVolume(int uDeviceID);

public static void Main(string[] args)
{
waveOutSetVolume( 0, 0xFFFF );
Console.WriteLine( waveOutGetVolume( 0 ) );
}
}
}

Throws the following error:

The instruction at "0x7c9118d0" referenced memory "0x00005c00". The memory could not be "written".

Please?

I am trying to get the initial system volume and be able to control it.

Matt.
 
you need to specify a device id not a 0 , eg:

[csharp]
[DllImport("winmm.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)]

public static extern int waveOutSetVolume(int uDeviceID, int dwVolume);

[DllImport("winmm.dll", SetLastError=
true, CallingConvention=CallingConvention.Winapi)]

public static extern int waveOutGetVolume(int uDeviceID);



private void button1_Click(object sender, System.EventArgs e)

{

waveOutSetVolume( AppDomain.GetCurrentThreadId(), 0xFFFF );

Console.WriteLine( waveOutGetVolume( AppDomain.GetCurrentThreadId() ) );

}
[/csharp]


 
While the code you provide did not throw an error for me, it did not do anything to the audio either.

Could you describe what the GetCurrentThreadId in the context of an audio device id would mean?

I dont understand why the thread id of the application would indicate, for example, the system sound volume.

Matt.
 
Your class works fine to me. I just had to add
using System.Runtime.InteropServices;
to get the DLLImports to work.
However, it did not set my system volume (good thing it didnt) but the volume of wave sounds going to the sound card (next to system sound).

The problem came with the [api]waveAuxGetVolume[/api] call, which does not match the API declaration.

Code:
         [DllImport("winmm.dll", SetLastError=[color=SeaGreen]true[/color], CallingConvention=CallingConvention.Winapi)]
[color=SeaGreen]public static extern int[/color] waveOutSetVolume([color=SeaGreen]int[/color] uDeviceID, [color=SeaGreen]int[/color] dwVolume);

        [DllImport("winmm.dll", SetLastError = [color=SeaGreen]true[/color], CallingConvention = CallingConvention.Winapi)]
        [color=SeaGreen]public static extern int[/color] waveOutGetVolume([color=SeaGreen]int[/color] uDeviceID, [color=SeaGreen]out int[/color] dVolume);
Theres a Volume property which has to come out of the API call. This must be intercepted to get the volume settings value.
Code:
            [color=SeaGreen]int[/color] dv = 0;
            waveOutSetVolume(0, 0xFFFF);
            waveOutGetVolume(0, [color=SeaGreen]out[/color] dv);
            [color=LimeGreen]//Intercept outcoming value of the waveOutGetVolume function.[/color]
            Console.WriteLine(dv.ToString());
:)
 

Similar threads

Back
Top