且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运行没有控制台窗口的Windows应用程序

更新时间:2023-02-19 19:13:30

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



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



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



-SA


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



1.在主应用程序中:
  private   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: [^].