Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
[DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Beep(uint dwFreq, uint dwDuration);
        static void Main(string[] args)
        {

            Console.WriteLine("Testing PC speaker...");
            for (uint i = 100; i <= 20000; i++)
            {
                Beep(i, 5);
            }
            Console.WriteLine("Testing complete.");
        }


My HP laptop has Windows 8.1 installed in it and I tried the above code on it but it did not work. I wonder if there is a standard library to play music or musical notes via C#.

best regards,
Posted

JBobby wrote:
Am new to C#. Please tell me in two lines what are the disadvantages of using unmanaged code?
Sure.

First of all, compared to .NET, unmanaged code is very, very error-prone. Abhinav mentioned just one problem in his comment to Solution 2: memory deallocation. But this is not the biggest problem is you are using unmanaged module in your .NET project.

The biggest problem is this: in most cases, it kills platform compatibility. .NET has one wonderful feature: you can execute your CLI code on different systems, even on different CPU instruction-set architectures, without recompilation. This is possible due to "AnyCPU" target and JIT compilation, which is performed during runtime. Moreover, there are alternative CLR implementations which allows you to do the same on many non-Microsoft OS (most used implementation is called Mono). When you use P/Invoke to use unmanaged code, you, in almost all cases, kill this possibility.

Please see:
http://en.wikipedia.org/wiki/Just-in-time_compilation[^],
http://en.wikipedia.org/wiki/Common_Language_Infrastructure[^],
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/Mono_software[^],
http://www.mono-project.com/Main_Page[^].

I'll strongly recommend to use only the pure .NET FCL (or even just BCL), possibly with pure-CLI third-party products, especially open-source. Use unmanaged (native) modules only if they are critically important and if there is absolutely no other way (with P/Invoke or C++/CLI).

See also:
http://en.wikipedia.org/wiki/Framework_Class_Library[^],
http://en.wikipedia.org/wiki/Base_Class_Library[^],
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^],
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^].

[EDIT]

After changes in Microsoft documentation, most adequate link to P/Invoke and C++/CLI implicit P/Invoke is this:
https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].


—SA
 
Share this answer
 
v3
Comments
Abhinav S 27-Mar-14 23:03pm    
5!
Sergey Alexandrovich Kryukov 28-Mar-14 1:30am    
Thank you, Abhinav.
—SA
Why are you using unmanaged code?
I would only use managed classes to produce the beep sound.

SystemSounds.Beep[^] provides the exact same feature.
 
Share this answer
 
Comments
JBobby 9-Mar-14 4:34am    
I am new to C#. Please tell me in two lines what are the disadvantages of using un-managed code ?
Abhinav S 9-Mar-14 6:04am    
If you use unmanaged code, you need to deallocate memory yourself.
Sergey Alexandrovich Kryukov 27-Mar-14 16:54pm    
This is not the most important factor, by far.
I answered in detail. Please see Solution 4.
—SA
agent_kruger 9-Mar-14 5:04am    
sir, can i ask what is the difference between solution 1 and 2 and can you explain the disadvantages of using un-managed code as asked by "Mr.JBobby"?
Sergey Alexandrovich Kryukov 27-Mar-14 16:53pm    
I answered in detail. Please see Solution 4.
—SA
Why don't you try something like this out:
C#
public void onePing()
{
    SystemSounds.Beep.Play();
}

See MSDN[^]

-KR
 
Share this answer
 
Comments
JBobby 9-Mar-14 4:42am    
thanks. It worked. :) I would to like to ask you if I want to play music like jingle bells using this standard function , what chance do I have to get succeeded ? i probably will have to use third party library to get the job done ?
Krunal Rohit 9-Mar-14 4:46am    
See this : http://www.codeproject.com/Articles/4712/Playing-wav-files-using-C
agent_kruger 9-Mar-14 5:02am    
+5 for answering the both questions.
Krunal Rohit 9-Mar-14 5:04am    
Thanks man..

-KR
agent_kruger 9-Mar-14 5:05am    
you're welcome sir, just told the truth.
C#
class Program
   {
       //[DllImport("kernel32.dll", SetLastError = true)]
      // static extern bool Beep(uint dwFreq, uint dwDuration);
       static void Main(string[] args)
       {

           Console.WriteLine("Testing PC speaker...");
           for (uint i = 100; i <= 20000; i++)
           {
               SystemSounds.Beep.Play();
           }
           Console.WriteLine("Testing complete.");
           Console.Beep();

           // Play a beep with frequency as 200 and duration as 300
           Console.Beep(200, 300);

           Console.Beep((int)523.2, 300);
           Console.Beep((int)523.2, 300);
           Console.Beep((int)523.2, 300);
           // Play the sound associated with the Asterisk event
           SystemSounds.Asterisk.Play();
       }
   }


Finally , I got I was looking for. This code can play frequencies without using third party library.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900