arjun
arjun

Reputation: 645

platform pinvoke tutorial msdn

The following is a tutorial from msdn. The ouput of _flushall is "Test" in the tutorial but I got "2" by displaying output using console.write(). Can somebody explain please?

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();
    }
}

Upvotes: 4

Views: 666

Answers (1)

Hans Passant
Hans Passant

Reputation: 941485

That code doesn't work anymore on modern Windows versions. The "msvcrt.dll" version you get is a private CRT implementation for Windows executables that has been tinkered with in otherwise undiagnosable ways, probably having something to do with security.

You'll need to find another one that is still friendly. You'll have one present on your machine if you have Visual Studio 2010 or later installed. Have a look-see in the c:\windows\syswow64 directory and look for msvcrxxx.dll where xxx is 100, 110 or 120. Change the declaration accordingly. On my machine, with VS2013 installed:

[DllImport("msvcr120.dll")]
public static extern int puts(string c);
[DllImport("msvcr120.dll")]
internal static extern int _flushall();

Output:

Test

Upvotes: 6

Related Questions