日期:2014-05-18  浏览次数:20685 次

关于C#中API调用的小问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Api
{
  class Program
  {
  [DllImport("msvcrt.dll")]
  public static extern int puts(string s);
  //[DllImport("msvcrt.dll")]
  // internal static extern int _flushall();

  static void Main(string[] args)
  {
  puts("Test");
  //_flushall();

  }
   
  }
}


我用的是vs2010,麻烦问下各位高手这段程序有什么问题?我每次调试都是“对 PInvoke 函数“Api!Api.Program::puts”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。”


------解决方案--------------------
加上CallingConvention
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int puts(string s);