C#非托管代码调用不工作:AddConsoleAlias

C#非托管代码调用不工作:AddConsoleAlias,c#,unmanaged,C#,Unmanaged,下面的代码在GetLastError()调用中不断返回值为8的FALSE 8显然是错误\u内存不足 我当然有足够的记忆力,但过程并不这样认为,有人能告诉我哪里出了问题吗 下面的代码是我所有的,当然除了表单对象声明,但是我想没有必要看到它,因为我有两个文本框和一个按钮 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing

下面的代码在
GetLastError()
调用中不断返回值为8的FALSE

8显然是
错误\u内存不足

我当然有足够的记忆力,但过程并不这样认为,有人能告诉我哪里出了问题吗

下面的代码是我所有的,当然除了表单对象声明,但是我想没有必要看到它,因为我有两个文本框和一个按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AddConsoleAlias
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("kernel32", SetLastError = true)]
        static extern bool AddConsoleAlias(string Source, string Target, string ExeName);

        [DllImport("kernel32.dll")]
        static extern uint GetLastError();

        private void btnAddAlias_Click(object sender, EventArgs e)
        {
            if (AddConsoleAlias(txbSource.Text, txbTarget.Text, "cmd.exe"))
            {
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show(String.Format("Problem occured - {0}", GetLastError()));
            }
        }
    }
}

AddConsoleAlias
定义控制台别名。您的Windows窗体应用程序没有打开控制台。控制台应在
AddConsoleAlias
invoke之前分配。为此,可以使用
alloconsole
函数

此函数的C#绑定为:

[DllImport("kernel32.dll", 
        EntryPoint = "AllocConsole",
        SetLastError = true,
        CharSet = CharSet.Auto,
        CallingConvention = CallingConvention.StdCall)]
    private static extern int AllocConsole();
修改后的代码如下所示:

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    [DllImport("kernel32.dll", 
        EntryPoint = "AllocConsole",
        SetLastError = true,
        CharSet = CharSet.Auto,
        CallingConvention = CallingConvention.StdCall)]
    private static extern int AllocConsole();

    [DllImport("kernel32", SetLastError = true)]
    static extern bool AddConsoleAlias(string Source, string Target, string ExeName);

    [DllImport("kernel32.dll")]
    static extern uint GetLastError();

    private void btnAddAlias_Click(object sender, EventArgs e)
    {
      AllocConsole();

      if (AddConsoleAlias(txbSource.Text, txbTarget.Text, "cmd.exe"))
      {
        MessageBox.Show("Success");
      }
      else
      {
        MessageBox.Show(String.Format("Problem occured - {0}", GetLastError()));
      }
    }
  }

对我来说很好。也许这些值在某种程度上是错误的。尝试这样一个简单的例子——尝试使用——您看到的错误可能是错误的。要补充@dbc所说的内容:为了避免解码
GetLastWin32Error()
,请使用
抛出新的Win32Exception(Marshal.GetLastWin32Error())
@Jeroen Mostert——或者键入
新的System.ComponentModel.Win32Exception如果您不想抛出并捕获异常,请在即时窗口中使用toStrug()/<代码> .dBc:对,我习惯于编写非托管调用的包装函数,我甚至没有考虑过。谢谢尼基塔,您把我放在正确的轨道上。没有按预期工作,控制台确实打开了,但我无法与之交互。相反,我使用了AttachConsole(),并确保已经有一个控制台在运行,这就成功了。C#绑定是:[DllImport(“kernel32.dll”,SetLastError=true)]静态外部bool AttachConsole(uint dwProcessId);