Strange DLL + InterropServices problem

I am trying to learn the basics of linking unmanaged C ++ and .NET. So, I have a DLL compiled and called from C #. Fine. Now I am facing this strange problem:

Here is my C ++ file Main.cpp

:

#include <stdio.h>

extern "C" __declspec(dllexport) void DisplayHelloFromDLL()
{
    printf ("Hello from the World of 1986!\n");
}

      

and file C # Program.cs

:

using System; using System.Runtime.InteropServices;

namespace FancyApp {
    class Program
    {
        [DllImport("ConsoleApp.dll")]
        public static extern void DisplayHelloFromDLL();

        static void Main()
        {
            Console.WriteLine("Hello form the World of 2008!");
            DisplayHelloFromDLL();

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();


       }
    }
}

      

Simple enough. When building, I am getting ConsoleApp.dll

from the C ++ side and FancyApp.exe

from C #.

On startup, it issues

Hello world in 2008!

Hello from the world of 1986!

Press any key to exit

as it should be, except for Release mode from VS2008 (press F5), I get

Hello world in 2008!

Press any key to exit

Now if I go to Explorer and run build of the release FancyApp.exe

without VS, it works fine.

Ideas?

I downloaded the solutions folder here (180kb).

+1


source to share


1 answer


Well I see the same behavior here and I cannot fully explain it. However, I think that when trying to run in debug mode (F5) against a Release build, you should expect undefined behavior. If I use ctrl-F5 it works correctly.

Since then, we can deduce that the binary is correctly built, so there is no problem with the compiler, but instead you see some strange debugger artifact.



This can be verified by going to the FancyApp properties and under the Debug tab unchecking the "Enable Visual Studio Hosting Process" checkbox. If you do this, it will work as you expect. Why, exactly, I cannot say. The tutorial here is not trying to debug a release build.

+3


source







All Articles