码迷,mamicode.com
首页 > 编程语言 > 详细

vs2008下C++开发问题汇总

时间:2016-06-12 10:52:00      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

让控制台程序后台运行

【转】C++ 让 Win32 Console Application 程序后台运行

方法一:(无闪现)

添加  
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

方法二:(这个会有闪现)

#include "windows.h"

void main()

{

HWND hwnd;

if(hwnd=::FindWindow("ConsoleWindowClass",NULL)) //找到控制台句柄
{
::ShowWindow(hwnd,SW_HIDE); //隐藏控制台窗口
}

//加入你的代码。程序运行之后,窗口会自动隐藏,只有在任务管理器中的进程中可以看到。

}

      使用以上代码,可以达到隐藏当前控制台窗口的效果。但是,如果系统开机时自动加载此程序,就会发现:控制台窗口没有自动隐藏,如果关闭此窗口,双击此控制台程序,发现窗口隐藏了。要解决此问题,可以使用以下代码:

#include "windows.h"

void main()

{
//开机自动隐藏窗口
HWND hwnd;
hwnd=FindWindow("ConsoleWindowClass",NULL);//找到当前窗口句柄
if(hwnd)
{
   ShowOwnedPopups(hwnd,SW_HIDE);//显示或隐藏由指定窗口所有的全部弹出式窗口
   ShowWindow(hwnd,SW_HIDE);//控制窗口的可见性
   //WinExec 函数: 控制窗口的显示形式
   //假如开机自动运行: C:\\WINDOWS\\SYSTEM32\\KeyboardRec.exe
   WinExec("C:\\WINDOWS\\SYSTEM32\\KeyboardRec.exe",SW_HIDE);
}

//你的其他代码

}

   注意:隐藏窗口的代码,一定要放在主函数的最前面;否则控制台窗口有可能无法隐藏。

方法三:

写一个简单的.vbs文件就可以。实现方法如下:
如,我的.exe文件是HKServer.exe。可以用文本文档写如下代码
[vb]
set wscriptObj = CreateObject("Wscript.Shell")  
wscriptObj.run "C:\Users\HK\Desktop\HKServerEditVersion2.4\Debug\HKServer.exe",0 
保存成.vbs文件,直接运行.vbs文件即可实现.exe文件后台运行。

Windows中查找命令的路径 (类似Linux中的which命令)

  1. where is a direct equivalent:

    C:\Users\Joey>where cmd
    C:\Windows\System32\cmd.exe

    Note that in PowerShell where itself is an alias for Where-Object, thus you need to usewhere.exe in PowerShell.

  2. In cmd you can also use for:

    C:\Users\Joey>for %x in (powershell.exe) do @echo %~$PATH:x
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  3. In PowerShell you have Get-Command and its alias gcm which does the same if you pass an argument (but also works for aliases, cmdlets and functions in PowerShell):

    PS C:\Users\Joey> Get-Command where
    
    CommandType     Name          Definition
    -----------     ----          ----------
    Alias           where         Where-Object
    Application     where.exe     C:\Windows\system32\where.exe

    The first returned command is the one that would be executed.

vs2008下C++开发问题汇总

标签:

原文地址:http://www.cnblogs.com/feilv/p/5576706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!