对于服务器程序来说,这个功能能够防止不小心点击键盘鼠标导致程序关闭而使服务和数据中断。是一个很实用的小功能
废话少说,上代码:
#include<Windows.h>
#include<stdio.h>
#include<conio.h>//_getch()函数
#include<iostream>//coutC++对象
using namespace std;
/**
* 获取用户输入的密码
* @param pwd char* 保存密码的内存的首地址
* @param pwdlen int 密码的最大长度
**/
void getpwd(char* pwd, int pwdlen) {
char ch = 0;
int i = 0;
while (i < pwdlen)
{
ch = _getch();
if (ch == '\r')
{
//回车结束输入
printf("\n");
break;
}
if (ch == '\b' && i > 0)
{
//按下删除键
i--;
printf("\b \b");
}
else if (isprint(ch))
{
//输入可打印字符
pwd[i] = ch;
printf("*");
i++;
}
}
pwd[i] = 0;
}
int main(int argc, char* argv[])
{
//屏蔽控制台最小按钮和关闭按钮
HWND hwnd = GetConsoleWindow();
HMENU hmenu = GetSystemMenu(hwnd, false);
RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
LONG style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_MINIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
ShowWindow(hwnd, SW_SHOWNORMAL);
DestroyMenu(hmenu);
ReleaseDC(hwnd, NULL);
printf("ctp数据处理服务器\n");
cout << "请选择:\n0:电信1\n1:电信2\n2:移动" << endl;
char TradeFrontAdd[256];
char c = getchar();
if (c == '0')
{
strcpy_s(TradeFrontAdd, "tcp://180.168.146.187:10201");
}
if (c == '1')
{
strcpy_s(TradeFrontAdd, "tcp://180.168.146.187:10202");
}
if (c == '2')
{
strcpy_s(TradeFrontAdd, "tcp://218.202.237.33:10203");
}
cout << "选择了:"<<c<<"项:" << TradeFrontAdd << endl;
//这里可以处理CTP的相关逻辑了,登录交易接口、处理数据等等
while (true)
{
chongxin: char pwd[8];///键盘上要输入的密码
printf("Input password: \n");
getpwd(pwd, 7);//把键盘输入的字符串保存在pwd里面,只能输入7个
if (strcmp(pwd, "1234567"))//如果pwd不是1234567,密码不对
{
printf("你输入的 password 是: %s,不正确\n", pwd);
printf("我要的 password 是: %s\n", "1234567");
goto chongxin;
}
else//输入的是1234567,密码正确
{
break;//密码正确,旧跳出循环、直达return 0;结束程序
}
}
return 0;
}