您的位置:首页 > 编程语言 > C语言/C++

C++如何屏蔽双击运行程序功能?

2013-07-18 14:55 197 查看

问题描述: 我们开发过程中可能会经常遇到,需要屏蔽EXE的双击运行功能,只能通过宿主程序(Service或EXE)来启动。比如腾讯的迷你弹窗,就只能通过主程序来启动,而不能直接通过双击来运行。

实现原理:宿主程序可以通过向子程序命令行参数可以来启动,而子程序在初始化的时候,会去检测当前的命令行参数是否匹配;如果匹配,就启动;如果不匹配,就不启动。

      因为宿主程序可以通过CreateProcess来传递命令行参数,所以可以顺利执行;而双击程序,没有传递任何命令行参数,自然程序也就没法启动了。

 

程序示例:#define CMD_LINE _T("CommandLine")

子程序在程序初始化中进行判断,判断部分源码如下:(返回TRUE-运行程序; 返回FALSE-不运行程序)

BOOL IsExecuteWhenDbClk()
{
BOOL bRt = FALSE;

int cmdLineCount = 0;
LPWSTR* pCmdLine = ::CommandLineToArgvW(GetCommandLine(), &cmdLineCount);

for ( int i = 0; i < cmdLineCount; i++ )
{
if ( 0 == CString(pCmdLine[i]).CompareNoCase( CString(CMD_LINE) ) )
{
bRt = TRUE;
break;
}
}

GlobalFree( HGLOBAL(pCmdLine) );

return bRt;
}

上面的例子只支持MFC框架,下面提供一个跨平台使用的版本:

#ifndef TOOLS_H_
#define TOOLS_H_

#include <Windows.h>
#include <string>
#include <algorithm>
using namespace std;

#define CMD_LINE    L"AUTOINSTALL"

BOOL IsExecuteWhenDbClk()
{
BOOL bRt = FALSE;

int cmdLineCount = 0;
LPWSTR* pCmdLine = ::CommandLineToArgvW(GetCommandLine(), &cmdLineCount);

for ( int i = 0; i < cmdLineCount; i++ )
{
wstring cmdLine(pCmdLine[i]);
transform(cmdLine.begin(), cmdLine.end(), cmdLine.begin(), toupper);
if ( 0 == cmdLine.compare(CMD_LINE) )
{
bRt = TRUE;
break;
}
}

GlobalFree( HGLOBAL(pCmdLine) );

return bRt;
}

#endif // TOOLS_H_

 

 

 

 

宿主程序中,通过CreateProcess来启动,代码如下:

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

// Start the child process.
wchar_t cmdLine[] = CMD_LINE;

if( !CreateProcess( NULL,        // No module name (use command line)
         cmdLine,        // Command line
          NULL,           // Process handle not inheritable
         NULL,           // Thread handle not inheritable
         FALSE,          // Set handle inheritance to FALSE
          0,              // No creation flags
         NULL,           // Use parent's environment block
          NULL,           // Use parent's starting directory
          &si,            // Pointer to STARTUPINFO structure
          &pi )           // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

// Wait until child process exits.
//WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐