Matthew Webster Posted September 30, 2005 Posted September 30, 2005 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. Quote
Leaders dynamic_sysop Posted September 30, 2005 Leaders Posted September 30, 2005 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] Quote
Matthew Webster Posted September 30, 2005 Author Posted September 30, 2005 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 don't understand why the thread id of the application would indicate, for example, the system sound volume. Matt. Quote
Leaders Iceplug Posted October 4, 2005 Leaders Posted October 4, 2005 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 didn't) 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. [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); There's a Volume property which has to come out of the API call. This must be intercepted to get the volume settings value. [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()); :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.