简体   繁体   中英

C# unmanaged code call not working: AddConsoleAlias

I have the following code which keeps returning FALSE with a value of 8 from the GetLastError() call.

8 apparently is ERROR_NOT_ENOUGH_MEMORY .

I of course have enough memory, but the process doesn't think so, can anyone enlighten me as to what could be going wrong?

The code below is all I have except for the Forms objects declarations of course, but I guess there is no need to see this as I have 2 text boxes and 1 button.

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 defines console alias. You have Windows Forms application without opened console. Console should be allocated before AddConsoleAlias invoke. To do that you can use AllocConsole function.

C# binding for this function is:

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

Your modified code will look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM