简体   繁体   English

在Visual Studio中不起作用,但作为.exe起作用

[英]Does not work in Visual studio but works as .exe

I have a problem with Visual Studio. 我对Visual Studio有问题。 My code crashes when run using Visual Studio, but works correctly if I compile it and run it as a exe. 使用Visual Studio运行时,我的代码崩溃,但是如果我对其进行编译并将其作为exe运行,则可以正常工作。

I am using Visual Studio Professional 2013 Version 12.0.3.1101.00 Update 4 我正在使用Visual Studio Professional 2013版本12.0.3.1101.00更新4

I am pretty sure it is a Visual Studio setting problem. 我很确定这是Visual Studio设置问题。 It is only in some Visual Studio installation I have experienced it in, in other installations this code works fine. 它仅在我经历过的某些Visual Studio安装中,在其他安装中,此代码可以正常工作。

class Program
{

    [DllImport("winmm.dll")]
    public static extern int waveOutGetNumDevs();

    static void Main(string[] args)
    {
        try
        {
            int numOfDevices = waveOutGetNumDevs(); //<--- throws SEHException 
            Console.WriteLine("Number Of Audio Out Devices: " + numOfDevices);
        }
        catch (Exception exception)
        {
            Console.WriteLine("Error: " + exception.ToString());
        }
        Console.ReadLine();
    }
}

The waveOutGetNumDevs() function throws an exception when I run it from visual Studio, but returns the correct value if I run it as a .exe. 当我从Visual Studio运行它时,waveOutGetNumDevs()函数会引发异常,但是如果我以.exe运行它,则会返回正确的值。 It doesn't matter if I run in debug mode or release mode. 是在调试模式下还是在释放模式下运行都没有关系。 Both fail. 都失败了。

The exception I get is: 我得到的异常是:

System.Runtime.InteropServices.SEHException occurred
  _HResult=-2147467259
  _message=An external component has thrown an exception.
  HResult=-2147467259
  IsTransient=false
  Message=An external component has thrown an exception.
  ErrorCode=-2147467259
  InnerException: 

Another observation, it depends on what version of .net I am using. 另一个观察结果,这取决于我使用的.net版本。 If I am using .NET 2.0 it actually works correctly, but if I use .NET 4.0 it fails. 如果我使用的是.NET 2.0,则它实际上可以正常运行,但是如果使用的是.NET 4.0,则它将失败。

My goal is to run the code in .NET 4.0 (later in .NET 4.5). 我的目标是在.NET 4.0中(稍后在.NET 4.5中)运行代码。

Edit 1: 编辑1:

The Error code and HResult in HEX is: 0x80004005 十六进制中的错误代码和HResult为:0x80004005

Edit 2: 编辑2:

I have now upgraded to Visual Studio 2013 Version 12.0.40629.00 Update 5 And the problem still persists. 我现在已升级到Visual Studio 2013版本12.0.40629.00更新5而且问题仍然存在。

This way works for me, in Visual Studio 2012 and .Net Framework 4.5.1: 在Visual Studio 2012和.Net Framework 4.5.1中,这种方式对我有效:

Player.cs Player.cs

class Player
{
    [DllImport("winmm.dll")]
    private static extern uint waveOutGetNumDevs();

    public uint GetNumDevs
    {
        get
        {
            return waveOutGetNumDevs();
        }
    }
}

Program.cs Program.cs

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Player player = new Player();
            uint numOfDevices = player.GetNumDevs;
            Console.WriteLine("Number Of Audio Out Devices: " + numOfDevices);
        }
        catch (Exception exception)
        {
            Console.WriteLine("Error: " + exception.ToString());
        }
        Console.ReadLine();
    }
}

Notice that I'm using a property to call the method and th return value's type is UINT , here the documentation: waveOutGetNumDevs . 注意,我正在使用属性来调用方法,返回值的类型是UINT ,在这里是文档: waveOutGetNumDevs

If still not working consider to reinstall your VS2013. 如果仍然无法正常工作,请考虑重新安装VS2013。

good luck (°-°) 祝你好运(°-°)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM