VC应用程序快捷方式带参数

源码:下载

以前写过一个短信平台的客户端程序,程序写好的时候老板帮我测试,提了个要求说:你的程序必须要避免能够运行多次,而且又要根据不同的配置文件必须能够运行多个。听了这话就有点晕,我当时也是,不知道应该怎么办。后来经老板提醒说可以在快捷方式(他以前使用过一个软件有这个功能)上考虑怎么做。于是我考虑并且苦心寻找,终于实现了此功能。现写出来分享给大家。

步骤一:首先简单的来谈谈CCommandLineInfo类

 
类CcommandLineInfo,它是用来处理命令行信息的类。其类原型定义如下:

代码
以下是代码片段:
class CCommandLineInfo : public Cobject
{
public:
// Sets default values
CCommandLineInfo();
//plain char* version on UNICODE for source-code backwards compatibility
virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast);
#ifdef _UNICODE
virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
#endif
BOOL m_bShowSplash;
BOOL m_bRunEmbedded;
BOOL m_bRunAutomated;
enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE,
AppUnregister, FileNothing
= -1 } m_nShellCommand;
// not valid for FileNew
Cstring m_strFileName;
// valid only for FilePrintTo
Cstring m_strPrinterName;
Cstring m_strDriverName;
Cstring m_strPortName;
~CCommandLineInfo();
// Implementation
protected:
void ParseParamFlag(const char* pszParam);
void ParseParamNotFlag(const TCHAR* pszParam);
#ifdef _UNICODE
void ParseParamNotFlag(const char* pszParam);
#endif
void ParseLast(BOOL bLast);
};

CCommandLineInfo类具体的参数和函数都是什么功能,我就不说了,具体的请参看MSDN吧(我怕自己说的不好,呵呵~)。

步骤二:得到快捷方式(是一个EXE文件)的参数

 


关键代码如下:

代码
以下是代码片段:
//begin 给EXE带上参数
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
strFile.Format(
"%s",cmdInfo.m_strFileName);
if (strFile == "")
{
strFile
= "configfile.ini";
}
//end 给EXE带上参数

步骤三:从配置文件中得到公司名

 


关键代码如下:

代码
以下是代码片段:
//begin 从配置文件中得到公司名
char databuf[32];
char filepath[1024];
memset(databuf,
0,32);
memset(filepath,
0,1024);
GetCurrentDirectory(
1024,filepath);
strcat(filepath ,
"\\" );
strcat(filepath ,strFile);
Cfile file;
if (!file.Open(filepath,Cfile::modeRead | Cfile::shareDenyWrite))
{
Cstring str
="%文件不存在!";
str.Replace(
"%",strFile);
AfxMessageBox(str);
return false;
}
memset(databuf,
0,32);
GetPrivateProfileString(
"main","company","",databuf,32,filepath);
strCompany
= databuf;
memset(databuf,
0,32);
Cstring strTitle
= strCompany + "CMPP3短信平台客户端";
//end 从配置文件中得到公司名

步骤四:避免同一个应用程序运行多次


关键代码如下:

代码
以下是代码片段:
//begin 避免同一个应用程序运行多次
HWND hWnd = ::FindWindow(NULL, (LPCSTR)strTitle);
if (hWnd)
{
AfxMessageBox(
"系统检测到您已经运行了该个程序。\r\n如确实要运行请退出另外一个程序再运行该程序!");
return false;
}
//end 避免同一个应用程序运行多次

步骤五:根据不同的公司显示不同的短信平台程序


可能有些人看了上面的还不是太明白。下面我再简单描述一下具体的操作步骤。

1、下载文章附带的源代码到你机子,用VC6编译运行。

2、如果你是在debug下编译运行的,你打开debug文件夹。把configfile.ini和configfile1.ini文件放到debug文件夹下。如果你是在release下编译运行,同理。

3、现在debug或release文件夹中只有一个ShortcutParameter.exe应用程序,你可以一直点它,但它只能运行一次。现给它创建快捷方式,给快捷方式带参数让它根据配置文件中的不同的公司名都显示应用程序。点ShortcutParameter.exe应用程序右键创建快捷方式,在创建的快捷方式右键->属性->快捷方式->目标 里面的ShortcutParameter.exe后面空格再加上configfile1.ini。例如:D:\ShortcutParameter\Debug\ShortcutParameter.exe configfile1.ini


示例图如下:

 


4、分别运行两个ShortcutParameter.exe应用程序,会发现打开了两个ShortcutParameter.exe应用程序,但你会发现是两个公司的了,看看标题栏。

呵呵~你运行成功后,是不是很开心。

具体程序还是请看源代码吧。可能我描述的不太清楚,如有不解之处,请联系我。

原文地址:https://www.cnblogs.com/joinclear/p/1882217.html