运行没有控制台窗口的Windows应用程序 [英] Running an Windows application without Console window

查看:117
本文介绍了运行没有控制台窗口的Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个实用程序,它将从另一个将提供命令行参数的应用程序启动,但是当它被激活时,会出现一个控制台窗口,我能够做的最好的事情就是使用互操作服务让它短暂闪存。



我需要应用程序在激活时显示表单而不是控制台窗口。我知道必须有办法做到这一点!

我还需要激活的实用程序应用程序根据用户输入返回一个值。



下面是我开始工作的代码,但现在却没有?,显示控制台窗口并且它保持正常状态然后消失了?



 [DllImport(  kernel32.dll)] 
静态 extern IntPtr GetConsoleWindow();

[DllImport( user32.dll)]
static extern bool ShowWindow( IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0 ;
const int SW_SHOW = 5 ;

public Form1( string [] args)
{
InitializeComponent();

// 隐藏
var handle = GetConsoleWindow();
ShowWindow(handle,SW_HIDE);

PositionNotifyWindow();

string html = < center>< h3>测试一下< / h3>< / center>;
string htm = < h3 style = \text-align:center \> WTF发生了什么?< / h3>;

webBrowser1.DocumentText = html + htm; ;
}





任何帮助非常感谢!

解决方案

你的逻辑在哪里?如果你不想要控制台,为什么要使用 GetConsoleWindow ?如果您使用的是表单,为什么要使用 ShowWindow ?您根本不应该使用P / Invoke,只使用纯.NET FCL,否则会损害您的平台兼容性,而不会得到任何回报。



命令-line参数与控制台无关。也许你的问题不是理解这个非常明显的事实。要在接收它们的应用程序中使用命令行参数,请参阅我的文章:枚举 - 基于命令行实用程序 [ ^ ]。



您不必使用我的实用程序或我的任何代码,您只需学习如何获取命令行参数通过入口点( Main )和 System.Environment 传递。



-SA


虽然我不确定我到底知道你在做什么,或者:),我在实现代码方面没有遇到任何麻烦像这样:



1.在主应用程序中:

  private  < span class =code-keyword> string  path = 
@ 辅助应用程序的ValidPathToExe;

private void button1_Click(对象发​​件人,EventArgs e)
{
// 必须是有效的URI ,即,从以下开始:http:// ...
Process.Start(路径, @ http://www.google.com);
}





2.在具有WebBrowser控件的辅助应用程序中:

  private   void  Form1_Load( object  sender,EventArgs e)
{
string [] args = Environment.GetCommandLineArgs();

if (args.Length > 0
{
// 仅供测试
// foreach(args中的字符串arg)MessageBox.Show(arg);

if (args.Length > 1
{
Uri uriResult;

bool result = Uri.TryCreate(args [ 1 ],UriKind。绝对, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp
|| uriResult.Scheme == Uri.UriSchemeHttps);
if (result)
{
webBrowser1.Navigate(args [ 1 ]);
}
else
{
MessageBox.Show( 无效的网址);
}
}
}
else
{
MessageBox.Show( 未收到有效的文件路径);
}
}

我假设您可以使用'Process Class的标准属性来启用从二级Process'表单接收通知事件...如EnableRaisingEvents,创建一个OutputDataReceived事件的处理程序,重定向标准输出等。



如果这个响应是离开,请告诉我,我会删除它。



用于处理.NET进程的有趣的异步覆盖:[ ^ ]。关于MedallionShell开发的文章:[ ^ ]。


I'm creating a utility that will be started from another application that will supply command line arguments but when it's activated a console window appears and the best I've been able to do is to get it to flash briefly using interop services.

I need for the application to display a form when it's activated but not the console window. I know there has to be a way to do this!
I also need for the activated utility application to return a value depending on user input.

Below is the code that I was getting to work but now it doesn't?, show the console window and it stays up was coming up briefly then disappearing?

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;

public Form1(string[] args)
{
    InitializeComponent();

    // Hide
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_HIDE);

    PositionNotifyWindow();

    string html = "<center><h3>Testing this out</h3></center>";
    string htm = "<h3 style=\"text-align: center\">WTF Happened?</h3>";

    webBrowser1.DocumentText = html + htm; ;
}



Any help much appreciated!

解决方案

Where is your logic? If you don't want the console, why using GetConsoleWindow? If you are using Forms, why using ShowWindow? You should not use P/Invoke at all, use just pure .NET FCL, otherwise you will compromise your platform compatibility, not getting anything at all in return.

Command-line parameters have nothing to do with console. Perhaps your problem was not understanding this very obvious fact. To work with command-line parameters in the application receiving them, please see my article: Enumeration-based Command Line Utility[^].

You don't have to use my utility or any of my code, you can just learn how to get the command-line parameters passed, through entry point (Main) and System.Environment.

—SA


While I'm not sure I understand exactly what you are doing, either :), I have had no trouble implementing code like this:

1. in the primary Application:

private string path =
    @"ValidPathToExe of Secondary App";

private void button1_Click(object sender, EventArgs e)
{
    // must be a valid URI, i.e., starts with: http:// ...
    Process.Start(path, @"http://www.google.com");
}



2. in the secondary Application with a WebBrowser Control:

private void Form1_Load(object sender, EventArgs e)
{
    string[] args = Environment.GetCommandLineArgs();

    if (args.Length > 0)
    {
        // for testing only
        //foreach (string arg in args) MessageBox.Show(arg);

        if (args.Length > 1)
        {
            Uri uriResult;

            bool result = Uri.TryCreate(args[1], UriKind.Absolute, out uriResult)
                          && (uriResult.Scheme == Uri.UriSchemeHttp
                              || uriResult.Scheme == Uri.UriSchemeHttps);
            if (result)
            {
                webBrowser1.Navigate(args[1]);
            }
            else
            {
                MessageBox.Show("invalid url");
            }
        }
    }
    else
    {
        MessageBox.Show("no valid file path received");
    }
}

I am assuming that you could use standard properties of the 'Process Class to enable receiving notification Events from the secondary Process' Form ... like EnableRaisingEvents, creating a handler for the OutputDataReceived Event, re-directing standard output, etc.

If this response is "way off," just let me know, and I'll delete it.

An interesting asynchronous overlay for working with .NET processes: [^]. Article about development of MedallionShell here: [^].


这篇关于运行没有控制台窗口的Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆