本文介绍了如何在C#中发出哔哔声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞![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."); } 我的惠普笔记本电脑安装了Windows 8.1,我在上面尝试了上面的代码,但它没有用。我想知道是否有一个标准库可以通过C#播放音乐或音符。 最好的问候,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,推荐答案 JBobby写道:是C#的新手。请用两行告诉我使用非托管代码的缺点是什么?Am new to C#. Please tell me in two lines what are the disadvantages of using unmanaged code?当然。 首先,与.NET相比,非托管代码非常,非常容易出错。 Abhinav在他对解决方案2的评论中提到了一个问题:内存释放。但这不是最大的问题,你在.NET项目中使用非托管模块。 最大的问题是:在大多数情况下,杀死平台兼容性。 .NET有一个很棒的功能:您可以在不同的系统上执行CLI代码,甚至可以在不同的CPU 指令集架构上执行,无需重新编译。这可能是由于AnyCPU目标和JIT编译,这是在运行时执行的。此外,还有其他CLR实现,允许您在许多非Microsoft OS上执行相同操作(最常用的实现称为Mono)。当您使用P / Invoke来使用非托管代码时,几乎在所有情况下都会消除这种可能性。 请参阅: 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 [ ^ ]。 我强烈建议只使用纯.NET FCL(甚至只是BCL),可能只使用纯-CLI第三方产品,特别是开源。使用非托管(本机)模块时,如果它们非常重要,并且绝对没有其他方式(使用P / Invoke或C ++ / CLI)。 参见: 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_PlatformI nvocationServices.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 [ ^ ]。 在Microsoft文档中进行更改后,最为充足链接到P / Invoke和C ++ / CLI 隐式P / Invoke 是这样的: https://msdn.microsoft.com/en-us/library/ms235282.aspx [ ^ ]。 -SASure.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[^].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[^].—SApublic void onePing(){ SystemSounds.Beep.Play();} 参见 MSDN [ ^ ] -KRSee MSDN[^]-KR 这篇关于如何在C#中发出哔哔声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 02:30