2021SC@SDUSC
继续分析剩余的代码部分:
if (!wcscmp(L"/userdir", lpstrCmdLine))
{
CreateDirectory(WeaselUserDataPath().c_str(), NULL);
WeaselServerApp::explore(WeaselUserDataPath());
return 0;
}
if (!wcscmp(L"/weaseldir", lpstrCmdLine))
{
WeaselServerApp::explore(WeaselServerApp::install_dir());
return 0;
}
// command line option /q stops the running server
bool quit = !wcscmp(L"/q", lpstrCmdLine) || !wcscmp(L"/quit", lpstrCmdLine);
// restart if already running
{
weasel::Client client;
if (client.Connect()) // try to connect to running server
{
client.ShutdownServer();
}
if (quit)
return 0;
}
bool check_updates = !wcscmp(L"/update", lpstrCmdLine);
if (check_updates)
{
WeaselServerApp::check_update();
}
CreateDirectory(WeaselUserDataPath().c_str(), NULL);
int nRet = 0;
try
{
WeaselServerApp app;
if (IsWindowsVistaOrGreater())
{
PRAR RegisterApplicationRestart = (PRAR)::GetProcAddress(::GetModuleHandle(_T("kernel32.dll")), "RegisterApplicationRestart");
RegisterApplicationRestart(NULL, 0);
}
nRet = app.Run();
}
catch (...)
{
// bad luck...
nRet = -1;
}
_Module.Term();
::CoUninitialize();
return nRet;
}
1.wcscmp
wcscmp()函数在cwchar.h头文件中定义。 wcscmp()函数用于比较两个以null结尾的宽字符串,此比较按字典顺序进行。
用法:
int wcscmp(const wchar_t* str1, const wchar_t* str2);
参数:此方法采用以下两个参数:
- str1:这表示指向要比较的第一个字符串的指针。
- str2:这表示指向要比较的第二个字符串的指针。
返回值:该方法返回:
- 零:如果str1和str2相同。
- 正值:如果str1中的第一个不同字符大于str2中的相应字符。
- 负值:如果str1中的第一个不同字符小于str2中的相应字符。
以下示例程序旨在说明上述功能:-
示例1:
// c++ program to demonstrate
// example of wcscmp() function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize the str1
wchar_t str1[] = L"Computer";
// initialize the str2
wchar_t str2[] = L"Science";
// Compare and print results
wcout << L"Comparing " << str1 << L" and "
<< str2 << L" = " << wcscmp(str1, str2) << endl;
// Compare and print results
wcout << L"Comparing " << str2 << L" and "
<< str2 << L" = " << wcscmp(str2, str2) << endl;
// Compare and print results
wcout << L"Comparing " << str2 << L" and "
<< str1 << L" = " << wcscmp(str2, str1) << endl;
return 0;
}
输出:
Comparing Computer and Science = -1 Comparing Science and Science = 0 Comparing Science and Computer = 1
2.LPCTSTR,LPWSTR, PTSTR, LPTSTR,wchar_t区别
L表示long指针,这是为了兼容Windows 3.1等16位操作系统遗留下来的,在win32中以及其他的32位操作系统中, long指针和near指针及far修饰符都是为了兼容的作用,没有实际意义。即win32中,long,near,far指针与普通指针没有区别,LP与P是等效的。
P表示这是一个指针。
T表示_T宏,这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么这个字符或者字符串将被作为UNICODE字符串,否则就是标准的ANSI字符串。
STR表示这个变量是一个字符串。
C表示是一个常量,const。
LPTSTR:
如果定义了UNICODE宏则LPTSTR被定义为LPWSTR。typedef LPTSTR LPWSTR;
否则LPTSTR被定义为LPSTR。 typedef LPTSTR LPSTR;
下面列出一些常用的typedefs:
类型 MBCS Unicode
WCHARwchar_twchar_t
LPSTR char* char*
LPCSTR const char* const char*
LPWSTRwchar_t* wchar_t*
LPCWSTR constwchar_t* const wchar_t*
TCHAR charwchar_t
LPTSTR TCHAR*(或char*) TCHAR* (或wchar_t*)
LPCTSTR const TCHAR* const TCHAR*
所以结论,在VS2005系统中,为提高可移植性,定义字符串时用TCHAR,转化为UNICODE时用_T而不用L。
3.命令行参数
摘自《OpenCV 入门教程》 于仕琪 shiqi.yu@szu.edu.cn
C/C++语言中的 main 函数,经常带有参数 argc,argv,如下:
int main(int argc, char** argv)
或者
int main(int argc, char* argv[])
在上面代码中,argc 表示命令行输入参数的个数(以空白符分隔),argv 中
存储了所有的命令行参数。假如你的程序是 hello.exe,如果在命令行运行该程序
(如图 1.14。首先应该在命令行下用 cd 命令进入到 hello.exe 文件所在目录),
运行命令为:
hello.exe Shiqi Yu
那么,argc 的值是 3,argv[0]是"hello.exe",argv[1]是"Shiqi",argv[2]是"Yu"。
图 1.14 使用命令行参数运行程序
下面的程序演示 argc 和 argv 的使用:
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
for (i=0; i < argc; i++)
printf("Argument %d is %s.\n", i, argv[i]);
return 0;
}
假如上述代码编译为 hello.exe,那么运行
hello.exe a b c d e
将得到
Argument 0 is hello.exe.
Argument 1 is a.
Argument 2 is b.
Argument 3 is c.
Argument 4 is d.
Argument 5 is e.
运行
hello.exe lena.jpg
将得到
Argument 0 is hello.exe.
Argument 1 is lena.jpg.