一些函数的使用
删除文件
#include<stdio.h> #include<Windows.h>
int main() { BOOL a; char Path_File[] = "C:\\Users\\政政真厉害\\Desktop\\111.txt";
a = DeleteFileA(Path_File);
printf("返回值:%d\n扩展错误信息%d\n", a, GetLastError());
return 0; }
|
这里的DeleteFileA函数
BOOL类型的返回值
PS:这里写路径的时候不是”/“,这里要用”//“
PS:GetLastError函数没有参数,所以括号内不填
DLL程序1
#include "pch.h"
void ccc() { MessageBox(NULL, "导出函数被调用成功!", "信息:", MB_YESNO); }
void aaa() { MessageBox(NULL, "加载成功!", "信息:", MB_YESNO); }
void jiewei() { MessageBox(NULL, "卸载成功!", "信息:", MB_YESNO); }
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: aaa(); case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
|
MessageBox函数
第一个参数一般是NULL
第二个参数为对话框要显示的内容
第三个是对话框的标题
第四个相当于取消和确定(一般选用画圈的)
DLL程序2
#include "pch.h"
extern "C" __declspec(dllexport) void ccc();
void ccc() { MessageBox(NULL, "导出函数调用成功!", "信息:", MB_YESNO); }
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
|
一般我们用c++写代码的时候,有时需要用到c的格式,并且这个工程要作为一个动态库输出,这是后就要用到extern “C” __declspec(dllexport),这里也就是用C的形式声明导出函数->把这个ccc函数导出去
DLL程序3
#include<Windows.h>
typedef void (*PMessage_Box)();
int main() { HMODULE hModule = LoadLibrary("FirstDll.dll"); if(hModule == NULL) { MessageBox(NULL, "句柄出错!", "标题:", MB_OK); return -1; } PMessage_Box pMsg = (PMessage_Box)GetProcAddress(hModule, "ccc"); pMsg();
return 0; }
|
GetProcAddress函数
第一个参数是HMODULE类型的DLL模块中的句柄
第二个参数是函数或变量名称
LoadLibrary函数
返回值类型为HMODULE
关于通讯
注入DLL
#include<Windows.h>
void Inject(int pID, char* Path) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);
LPVOID pReturnAddress = VirtualAllocEx(hProcess, NULL, strlen(Path) + 1, MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(hProcess, pReturnAddress, Path, strlen(Path) + 1, NULL);
HMODULE hModule = LoadLibrary("Kernel32.dll"); LPTHREAD_START_ROUTINE lpStartAddress = (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "LoadLibraryA");
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, lpStartAddress, pReturnAddress, 0, NULL);
WaitForSingleObject(hThread, 2000);
CloseHandle(hThread); CloseHandle(hProcess);
}
int main() { const char* a = "D:\\删除文件\\SecondDll\\x64\\Debug\\SecondDll.dll"; Inject(19604, (char*)a);
return 0; }
|