Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Leaders
Posted

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]

 

 

Posted

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.

  • Leaders
Posted

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());

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...