473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about PInvokeStackImbalance? Thanks

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.CurrentDirecto ry +
Path.DirectorySeparatorChar + "testing.wav", soundPlayer.SND_ASYNC);

Aug 22 '07 #1
2 1867
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):

<li*********@yahoo.com.hkwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>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.CurrentDirecto ry +
Path.DirectorySeparatorChar + "testing.wav", soundPlayer.SND_ASYNC);

Aug 22 '07 #2
<li*********@yahoo.com.hkwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>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.CurrentDirecto ry +
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.
Aug 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
2
by: Marek | last post by:
Hi I'm trying to call a native C++ dll using the following code (both C# and C++ code segments included). When I make the call to the method (AddTwoDoubles) that has no return value all is fine. ...
4
by: John Sudds | last post by:
The MDA appears on the call to OpenDesktop. I have tried a variety of approaches (including specifying the A and W variant with ExactSpelling=true, and PreserveSig=true) but none of them seem to...
0
by: MJKulangara | last post by:
I have an ap written in vb.net using 1.2 framework that makes a call to a c++ dll. The executable I create runs just fine, but when I run the ap in debug mode I get a PInvokeStackImbalance error....
1
by: urbansound | last post by:
Hi, I'm having trouble tracking down a PInvoke error in the MySqlDriverCS lib, which is detected even in the author's samples converted to VS v8 .Net 2.0. The calling maze is provided, but the...
1
by: raghavendra2007 | last post by:
Hi, I am trying to develop the ScreenReader in C#, I need to use IAccessibility to access the system events. Before that I should require to get the IAccesibility object using...
2
by: John M. Gamble | last post by:
I'm getting this message in Visual Studio 2005: PInvokeStackImbalance was detected Message: A call to PInvoke function 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.