Skip to main content
 首页 » 编程设计

c#之使用 "static extern int system(string str)"时 PInvokeStackImbalance

2024年02月20日11insus

我正在尝试使用 system(string str) 命令在 C# 中进行 dos 操作。

namespace XYZ 
{ 
    internal class Program 
    { 
        [DllImport("msvcrt.dll")] 
        static extern int system(string str); 
 
        static void Main(string[] args) 
        { 
             string Command = Console.ReadLine(); 
             system(Command);  
             /* Excutes command, then "PInvokeStackImbalance". */ 
        } 
    } 
} 

我知道使用 static extern int system(string str) 是一个糟糕的解决方案,但我已经尝试过其他无效的解决方案。

请您参考如下方法:

您忘记指定调用约定:

[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] 
static extern int system(string str); 

我通常会尝试解释原因,但这在这里似乎没有必要;)否则这不太可能解决您的问题,无论它是什么,它与

Process.Start("cmd.exe", "/c " + str);