简体   繁体   中英

Does not work in Visual studio but works as .exe

I have a problem with Visual Studio. My code crashes when run using Visual Studio, but works correctly if I compile it and run it as a exe.

I am using Visual Studio Professional 2013 Version 12.0.3.1101.00 Update 4

I am pretty sure it is a Visual Studio setting problem. It is only in some Visual Studio installation I have experienced it in, in other installations this code works fine.

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. 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. If I am using .NET 2.0 it actually works correctly, but if I use .NET 4.0 it fails.

My goal is to run the code in .NET 4.0 (later in .NET 4.5).

Edit 1:

The Error code and HResult in HEX is: 0x80004005

Edit 2:

I have now upgraded to Visual Studio 2013 Version 12.0.40629.00 Update 5 And the problem still persists.

This way works for me, in Visual Studio 2012 and .Net Framework 4.5.1:

Player.cs

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

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

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 .

If still not working consider to reinstall your VS2013.

good luck (°-°)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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