关于windows系统的操作程序开发,本文介绍一部分重要的文本属性操作,和运行计次器。
获取系统管理员权限
#include <windows.h>
VOID ManagerRun(LPCSTR exe, LPCSTR param, INT nShow)
{ //注意:会跳出提示。SHELLEXECUTEINFO ShExecInfo; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = "runas"; ShExecInfo.lpFile = exe; ShExecInfo.lpParameters = param; ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = nShow; ShExecInfo.hInstApp = NULL; BOOL ret = ShellExecuteEx(&ShExecInfo); CloseHandle(ShExecInfo.hProcess);return;
}
int main(int argc, char *argv[])
{if(argc == 1){ShowWindow(GetConsoleWindow(),SW_HIDE);ManagerRun(argv[0],"2", SW_SHOWNORMAL);return 1;}else if(argc == 2) {/*你的程序主代码在此*/}return 0;
}
将指定路径文件隐藏属性设置为“True”
#include <windows.h>
#include <stdio.h>
int main() {char *p="D:\\a.txt";// 文件夹路径const char* folderPath = p;// 获取文件夹的属性DWORD attributes = GetFileAttributes(folderPath);if (attributes == INVALID_FILE_ATTRIBUTES) {perror("GetFileAttributes");return 1;}// 添加隐藏属性attributes |= FILE_ATTRIBUTE_HIDDEN;// 设置文件夹的新属性if (!SetFileAttributes(folderPath, attributes)) {perror("SetFileAttributes");return 1;}printf("文件夹属性已设置为隐藏\n"); return 0;
}
将指定路径文件隐藏属性设置为“False”
#include <windows.h>
#include <stdio.h>int main() {// 文件夹路径const char* folderPath = "D:\\a.txt";// 获取文件夹的属性DWORD attributes = GetFileAttributes(folderPath);if (attributes == INVALID_FILE_ATTRIBUTES) {perror("GetFileAttributes");return 1;}// 清除隐藏属性attributes &= ~FILE_ATTRIBUTE_HIDDEN;// 设置文件夹的新属性if (!SetFileAttributes(folderPath, attributes)) {perror("SetFileAttributes");return 1;}printf("文件夹的隐藏属性已取消\n");return 0;
}
文本运行的计次器
关于指定的C语言程序,我们知道特定的源代码产生特定的exe运行文件,
而且当源程序确定时,通过编译链接得到的exe运行文件,
每次运行该exe文件所产生的效果是恒定的,因为他不具备存储功能,
如果想要控制用户打开指定第几次时,才触发某些操作,则需要一些存储手段。
(放到启动文件夹里,就可以检测从存放如文件以来第n次的开机,可以指定病毒延迟效果)
我们需要向指定恒定存在路径新建一个文本文件,通过每次运行来向其中读写内容,从而控制信息的存储,来实现计次手段。
#include <stdio.h>
#include<windows.h>
#include<conio.h>
int main()
{FILE *p1 = fopen("D:\\a.txt", "a");fputs("1", p1);fclose(p1);char a[2000];FILE *p2 = fopen("D:\\a.txt", "r");fgets(a,2000,p2);int n=0;int num = 0;while(1)
{if(a[n]=='1'){n++; num++;}else{break;}
}
printf("这是你第%d次打开这个程序\n",num);
printf("按下W键关闭,\n或者按下S键重置次数");while(1)
{if(kbhit)
{if(GetAsyncKeyState('W')){break;}if(GetAsyncKeyState('S')){FILE *p3 = fopen("D:\\a.txt", "w+");fputs("", p3);printf("\n已为你重置运行次数!"); break;}}
}return 0;
}
我在其中设定了最基础的 计次 和 重置次数 功能,感兴趣的话,你可以通过编写,控制该程序在第几次的触发你预先设定好的程序,这个装置可以用来干嘛我就不多说了,配上一点C语言文本指针的内容读写操作,功能自由。
关于windows API 的核心基础,有以下几点
1. 创建和显示窗口
在 Windows 上创建和显示一个窗口是很基础的操作,会用到CreateWindow
和ShowWindow
等函数。
#include <windows.h>// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {switch (msg) {case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, msg, wParam, lParam);}return 0;
}// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {static TCHAR szAppName[] = TEXT("HelloWin");HWND hwnd;MSG msg;WNDCLASS wndclass;wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hInstance = hInstance;wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.lpszMenuName = NULL;wndclass.lpszClassName = szAppName;if (!RegisterClass(&wndclass)) {MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);return 0;}hwnd = CreateWindow(szAppName, TEXT("The Hello Program"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);ShowWindow(hwnd, iCmdShow);UpdateWindow(hwnd);while (GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;
}
2. 文件操作
可以使用CreateFile
、ReadFile
和WriteFile
等函数进行文件的创建、读取和写入操作。
#include <windows.h>
#include <stdio.h>int main() {HANDLE hFile;DWORD dwBytesWritten, dwBytesRead;char buffer[] = "Hello, Windows API!";char readBuffer[100];// 创建文件hFile = CreateFile("test.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE) {perror("CreateFile");return 1;}// 写入文件if (!WriteFile(hFile, buffer, sizeof(buffer), &dwBytesWritten, NULL)) {perror("WriteFile");CloseHandle(hFile);return 1;}CloseHandle(hFile);// 打开文件进行读取hFile = CreateFile("test.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE) {perror("CreateFile");return 1;}// 读取文件if (!ReadFile(hFile, readBuffer, sizeof(readBuffer), &dwBytesRead, NULL)) {perror("ReadFile");CloseHandle(hFile);return 1;}CloseHandle(hFile);printf("Read from file: %s\n", readBuffer);return 0;
}
3. 消息框显示
使用MessageBox
函数可以显示一个简单的消息框。
#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {MessageBox(NULL, TEXT("这是一个消息框示例!"), TEXT("消息框"), MB_OK | MB_ICONINFORMATION);return 0;
}
4. 进程创建
使用CreateProcess
函数可以创建一个新的进程。
#include <windows.h>
#include <stdio.h>int main() {STARTUPINFO si;PROCESS_INFORMATION pi;ZeroMemory(&si, sizeof(si));si.cb = sizeof(si);ZeroMemory(&pi, sizeof(pi));// 创建新进程if (!CreateProcess(NULL, "notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {perror("CreateProcess");return 1;}// 等待进程结束WaitForSingleObject(pi.hProcess, INFINITE);// 关闭进程和线程句柄CloseHandle(pi.hProcess);CloseHandle(pi.hThread);return 0;
}
本次内容到此为止,goog night !