0

I've started recently to work on the project that involves communication between C# and native code and thus I'm trying to understand how it works. I've loaded one of the samples from msdn, tried to launch it and I see that it works in Release mode and crashes in debug mode. Does somebody know why?

// PInvokeTest.cs
using System;
using System.Runtime.InteropServices;

class PlatformInvokeTest
{
    [DllImport("msvcrt.dll")]
    public static extern int puts(string c);
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();

    public static void Main() 
    {
        puts("Test");
        _flushall();
    }
}

Error:

Additional information: A call to PInvoke function 'ConsoleApplication6!PlatformInvokeTest::puts' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

This code is from following site: https://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx

Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

2

Quoting this well detailed answer:

A stack imbalance occurs when the data structure used to keep track of called functions, arguments, and return values becomes corrupted or misaligned.

Most times, the stack is a memory pointer that stores the address where control will resume when the current function call exits back to the caller. There are different variants on this, sometimes the arguments to a function are also appended to the stack, as well as the return value. What is most important here is that the caller and callee should agree upon how to restore it back to the prior state when the callee exits. This agreement is frequently known as the Calling Convention.

You forgot to specify the calling convention:

class PlatformInvokeTest
{
    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int puts(string c);
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();

    public static void Main()
    {
        puts("Test");
        _flushall();
    }
}

Read more about x86 calling conventions

Community
  • 1
  • 1
cbr
  • 12,563
  • 3
  • 38
  • 63
  • After adding this I don't get exception anymore, but the function doesnot out put anything. Also, why do I get into this problem in Debug mode only? –  Mar 09 '15 at 20:37
  • @levgen It does output your string, but it just closes so quickly that you probably missed it. Add a `Console.ReadLine()` to the end or run the program without debugging to have it pause after the output. – cbr Mar 09 '15 at 21:42
  • I did this but I see empty screen. Nothing is getting written. Although it really doesn't fail –  Mar 09 '15 at 21:49
  • @levgen You have an answer to the question that you asked – David Heffernan Mar 10 '15 at 07:13
  • @levgen That's interesting, because it works fine for me: http://i.imgur.com/iPl6nFu.png – cbr Mar 10 '15 at 07:23