Question about PInvokeStackImbalance? Thanks

L

linuxfedora

I wrote a class for playing wav file, the code:
class SoundPlayer
{
// flag values for SoundFlags argument on PlaySound
public int SND_SYNC = 0x0000; // play synchronously
(default)
public int SND_ASYNC = 0x0001; // play asynchronously
public int SND_NODEFAULT = 0x0002; // silence (!default) if
sound not found
public int SND_MEMORY = 0x0004; // pszSound points to a
memory file
public int SND_LOOP = 0x0008; // loop the sound until next
sndPlaySound
public int SND_NOSTOP = 0x0010; // don't stop any currently
playing sound

public int SND_NOWAIT = 0x00002000; // don't wait if the
driver is busy
public int SND_ALIAS = 0x00010000; // name is a registry
alias
public int SND_ALIAS_ID = 0x00110000; // alias is a predefined
ID
public int SND_FILENAME = 0x00020000; // name is file name
public int SND_RESOURCE = 0x00040004; // name is resource name
or atom
public int SND_PURGE = 0x0040; // purge non-static events for
task
public int SND_APPLICATION = 0x0080; // look for application
specific association n

[DllImport("WinMM.dll")]
public static extern bool PlaySound(byte[] wfname, int
fuSound);

public void Play(string wfname, int SoundFlags)
{
byte[] bname = new Byte[256]; //Max path length
bname = System.Text.Encoding.ASCII.GetBytes(wfname);
PlaySound(bname, SoundFlags);
}

public void Stop()
{
PlaySound(null, SND_PURGE);
}
}


then when i try to use this class, i got the error of
PInvokeStackImbalance, can anyone tell me how to solve it? Thanks

SoundPlayer soundPlayer = new SoundPlayer();
soundPlayer.Play(System.Environment.CurrentDirectory +
Path.DirectorySeparatorChar + "testing.wav", soundPlayer.SND_ASYNC);
 
P

pedrito

PlaySound takes 3 parameters, not 2. The second parameter is an HMODULE
handle.

You need to change the signature to:
[DllImport("WinMM.dll")]
public static extern bool PlaySound(byte[] wfname, IntPtr hModule, int
fuSound);

and add this:

[DllImport("Kernel32.dll"]
public static extern IntPtr GetModuleHandle(string filename);

And I think you can just do to get the module handle for an .EXE:

IntPtr hModule = GetModuleHandle(null):
 
W

Willy Denoyette [MVP]

I wrote a class for playing wav file, the code:
class SoundPlayer
{
// flag values for SoundFlags argument on PlaySound
public int SND_SYNC = 0x0000; // play synchronously
(default)
public int SND_ASYNC = 0x0001; // play asynchronously
public int SND_NODEFAULT = 0x0002; // silence (!default) if
sound not found
public int SND_MEMORY = 0x0004; // pszSound points to a
memory file
public int SND_LOOP = 0x0008; // loop the sound until next
sndPlaySound
public int SND_NOSTOP = 0x0010; // don't stop any currently
playing sound

public int SND_NOWAIT = 0x00002000; // don't wait if the
driver is busy
public int SND_ALIAS = 0x00010000; // name is a registry
alias
public int SND_ALIAS_ID = 0x00110000; // alias is a predefined
ID
public int SND_FILENAME = 0x00020000; // name is file name
public int SND_RESOURCE = 0x00040004; // name is resource name
or atom
public int SND_PURGE = 0x0040; // purge non-static events for
task
public int SND_APPLICATION = 0x0080; // look for application
specific association n

[DllImport("WinMM.dll")]
public static extern bool PlaySound(byte[] wfname, int
fuSound);

public void Play(string wfname, int SoundFlags)
{
byte[] bname = new Byte[256]; //Max path length
bname = System.Text.Encoding.ASCII.GetBytes(wfname);
PlaySound(bname, SoundFlags);
}

public void Stop()
{
PlaySound(null, SND_PURGE);
}
}


then when i try to use this class, i got the error of
PInvokeStackImbalance, can anyone tell me how to solve it? Thanks

SoundPlayer soundPlayer = new SoundPlayer();
soundPlayer.Play(System.Environment.CurrentDirectory +
Path.DirectorySeparatorChar + "testing.wav", soundPlayer.SND_ASYNC);


Your declaration of the PlaySound does not match the real library function
signature, change it into:
[DllImport("winmm.DLL", SetLastError = true)]
private static extern bool PlaySound(string szSound, IntPtr hMod,
int flags);

and call it just like this:

public void Play(string wfname, int SoundFlags)
{
PlaySound (dialog1.FileName, IntPtr.Zero, SoundFlags);
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

play sound file 1
Problem plaing Wav on CE5 7
Sound 8
how To Play sound 4
Sound play problem! 2
Play message 3

Top