简体   繁体   English

调用C++ dll的方法在C# app打开console window,如何抑制console?

[英]Call of a method from C++ dll opens console window in C# app, how do I suppress the console?

I have written a C++ dll with one published method which simply starts another program with a defined parameter.我已经用一个已发布的方法编写了一个 C++ dll,它只是用一个定义的参数启动另一个程序。 This method is called from a C# WinForms application.从 C# WinForms 应用程序调用此方法。

The published method is triggered from the C# application on a button click.单击按钮从 C# 应用程序触发已发布的方法。 The 2nd application is starting, as intended, but in addition a Windows Console window is opening which outputs nothing.第二个应用程序正在按预期启动,但另外一个 Windows 控制台 window 正在打开,但不输出任何内容。

I want to suppress the console window, but I can't figure out how to do this.我想抑制控制台 window,但我不知道该怎么做。 When the application I run is terminated, the console window also terminates.当我运行的应用程序终止时,控制台 window 也终止。

This is what my header and source of the C++ dll looks like:这是我的 header 和 C++ dll 的来源:

launcher.h发射器.h

#pragma once

#ifdef ILJ16_EXPORTS
#define LAUNCHER_EXPORT __declspec(dllexport)
#else
#define LAUNCHER_EXPORT __declspec(dllimport)
#endif // ILJ16_EXPORTS

const char* startParam = "--Q7t0elSDASCrpHQ";

extern "C" LAUNCHER_EXPORT void startProcess();

launcher.cpp发射器.cpp

#include "pch.h"
#include "launcher.h"
#include <process.h>
#include <stdio.h>

void startProcess()
{
    char command[256 + 1];
    snprintf(command, sizeof(command), "Test.exe %s", startParam); //Test.exe is the application which has to be started
    int retCode = system(command);
}

In my C# WinForms project I have written the following code在我的 C# WinForms 项目中,我编写了以下代码

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        [DllImport("ilj16.dll", EntryPoint = "startProcess")]
        public static extern void startProcess();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            startProcess();
        }
    }
}

From reading the windows documentation it appears that the system(const char*) call opens a cmd.exe prompt and then executes the string.从阅读 windows 文档可以看出, system(const char*)调用会打开 cmd.exe 提示符,然后执行该字符串。

system documentation 系统文档

The system function passes command to the command interpreter, which executes the string as an operating-system command.系统 function 将命令传递给命令解释器,命令解释器将字符串作为操作系统命令执行。 system uses the COMSPEC and PATH environment variables to locate the command-interpreter file CMD.exe.系统使用 COMSPEC 和 PATH 环境变量来定位命令解释器文件 CMD.exe。 If command is NULL, the function just checks whether the command interpreter exists.如果命令是 NULL,function 只是检查命令解释器是否存在。

From a quick google it looks like the best way to solve this is to use _popen , CreateProcessA , or you could try to hide/close the cmd.exe using something like this .从快速谷歌看起来解决这个问题的最好方法是使用_popenCreateProcessA ,或者你可以尝试使用类似这样的东西隐藏/关闭 cmd.exe 。

Also, if your C++ dll is only opening another program, you could eliminate the need for a dll and do it all in C# with this C# class .此外,如果您的 C++ dll 只是打开另一个程序,您可以消除对 dll 的需要,并在 C# 中使用此 C# class完成所有操作。 In the first example at that link there is a simple solution:在该链接的第一个示例中,有一个简单的解决方案:

    using (Process myProcess = new Process())
    {
        myProcess.StartInfo.UseShellExecute = false;
        // You can start any process, HelloWorld is a do-nothing example.
        myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.Start();
        // This code assumes the process you are starting will terminate itself.
        // Given that is is started without a window so you cannot terminate it
        // on the desktop, it must terminate itself or you can do it programmatically
        // from this application using the Kill method.
    }

Instead of startprocess , consider using CreateProcess with STARTUPINFO.wShowWindow set to SW_HIDE .而不是startprocess ,考虑使用CreateProcess并将STARTUPINFO.wShowWindow设置为SW_HIDE

CreateProcess

Creates a new process and its primary thread.创建一个新进程及其主线程。 The new process runs in the security context of the calling process新进程在调用进程的安全上下文中运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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