获取IP地址:
const char* GetLocalIpAddress()
{
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData) != 0)
return "-1";
char local[255] = { 0 };
gethostname(local, sizeof(local));
hostent* ph = gethostbyname(local);
if (ph == NULL)
return "-1";
in_addr addr;
memcpy(&addr, ph->h_addr_list[0], sizeof(in_addr)); //这里仅获取第一个ip
char* localIP;
localIP = inet_ntoa(addr);
WSACleanup();
return localIP;
}
截屏(全屏):
void Cutscreen(char* fileName)
{
HWND window = GetDesktopWindow();
HDC _dc = GetWindowDC(window);
HDC dc = CreateCompatibleDC(0);
RECT re;
GetWindowRect(window, &re);
int w = re.right,
h = re.bottom;
void* buf = new char[w * h * 4];
HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);
SelectObject(dc, bm);
StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);
void* f = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
GetObject(bm, 84, buf);
tagBITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = 0;
bi.bmiHeader.biSizeImage = 0;
CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
BITMAPFILEHEADER bif;
bif.bfType = MAKEWORD('B', 'M');
bif.bfSize = w * h * 4 + 54;
bif.bfOffBits = 54;
BITMAPINFOHEADER bii;
bii.biSize = 40;
bii.biWidth = w;
bii.biHeight = h;
bii.biPlanes = 1;
bii.biBitCount = 32;
bii.biCompression = 0;
bii.biSizeImage = w * h * 4;
DWORD r;
WriteFile(f, &bif, sizeof(bif), &r, NULL);
WriteFile(f, &bii, sizeof(bii), &r, NULL);
WriteFile(f, buf, w * h * 4, &r, NULL);
CloseHandle(f);
DeleteDC(_dc);
DeleteDC(dc);
}
检测IP正确性:
inline bool ChickIpAddress(const char* pszIPAddr)
{
if (!pszIPAddr) return false; //若pszIPAddr为空
char IP1[128], cIP[4];
int len = strlen(pszIPAddr);
int i = 0, j = len - 1;
int k, m = 0, n = 0, num = 0;
//去除首尾空格(取出从i-1到j+1之间的字符):
while (pszIPAddr[i++] == ' ');
while (pszIPAddr[j--] == ' ');
for (k = i - 1; k <= j + 1; k++)
{
IP1[m++] = pszIPAddr[k];
}
IP1[m] = '\0';
char* p = IP1;
while (*p != '\0')
{
if (*p == ' ' || *p < '0' || *p > '9') return false;
cIP[n++] = *p; //保存每个子段的第一个字符,用于之后判断该子段是否为0开头
int sum = 0; //sum为每一子段的数值,应在0到255之间
while (*p != '.' && *p != '\0')
{
if (*p == ' ' || *p < '0' || *p > '9') return false;
sum = sum * 10 + *p - 48; //每一子段字符串转化为整数
p++;
}
if (*p == '.') {
if ((*(p - 1) >= '0' && *(p - 1) <= '9') && (*(p + 1) >= '0' && *(p + 1) <= '9'))//判断"."前后是否有数字,若无,则为无效IP,如“1.1.127.”
num++; //记录“.”出现的次数,不能大于3
else
return false;
};
if ((sum > 255) || (sum > 0 && cIP[0] == '0') || num > 3) return false;//若子段的值>255或为0开头的非0子段或“.”的数目>3,则为无效IP
if (*p != '\0') p++;
n = 0;
}
if (num != 3) return false;
return true;
}
打开exe文件:
BOOL OpenExeFile(LPCSTR mode, LPCSTR filename)
{
SHELLEXECUTEINFO shell = { sizeof(shell) };
shell.fMask = SEE_MASK_FLAG_DDEWAIT;
shell.lpVerb = mode;//模式,一般为open
shell.lpFile = filename;//文件名
shell.nShow = SW_SHOWNORMAL;//显示
BOOL ret = ShellExecuteEx(&shell);//return ShellExecuteEx(&shell);
return ret;
}
|