当前位置:网站首页>Common windbos APIs

Common windbos APIs

2022-06-09 08:31:00 Tianwen_ Herbert555

Application memory

VirtualAlloc && VirtualAllocEx

#include<iostream>
#include<Windows.h>


int main() {
    
    //  Minimum allocation 1000
	LPVOID pp = VirtualAlloc(NULL, 1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

	// GetCurrentProcess: Get the handle of the current process 
	LPVOID pp1 = VirtualAllocEx(GetCurrentProcess(), NULL, 1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	
    //  Check the error report 
	DWORD D = GetLastError();

	return 0;
}

HeapAlloc

#include<iostream>
#include<Windows.h>


int main() {
    
	// 1. Only allocated 0x10, Mechanism to prevent overflow 
	// 2. Generally, there is no executable permission , Modify the permission api To modify heap permissions ,VirtualProtect
	LPVOID p = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0x10);
	return 0;
}

GlobalAlloc

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalalloc

#include<iostream>
#include<Windows.h>


int main() {
    
	//  The default is GMEM_FIXED, Allocate fixed memory .
	LPVOID p = GlobalAlloc(0, 0x1000);
	return 0;
}

CoTaskMemAlloc

https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemalloc

#include<iostream>
#include<Windows.h>


int main() {
    
	LPVOID p = CoTaskMemAlloc(0x100);
	return 0;
}

HeapCreate Create a heap

#include<Windows.h>

int main(void) {
    
	unsigned char buf[] = "\xfc";

	// Create a heap 
	HANDLE myHeap = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
   	return 0;
}

Move memory

memcpy

Copy bytes between buffers .

#include<iostream>
#include<Windows.h>


int main() {
    
	char name[256] = "qwertyuiop";
	char name1[256] = "";
	memcpy(name1, name, strlen(name) + 1);
	printf("%s", &name1);
	return 0;
}

copymemory

#include<iostream>
#include<Windows.h>


int main() {
    
	char name[256] = "qwertyuiop";
	char name1[256] = "";
	CopyMemory(name1, name, strlen(name) + 1);
	printf("%s", &name1);
	return 0;
}

RtlCopyMemory

#include<iostream>
#include<Windows.h>


int main() {
    
	char name[256] = "qwertyuiop";
	char name1[256] = "";
	RtlCopyMemory(name1, name, strlen(name) + 1);
	printf("%s", &name1);
	return 0;
}

RtlMoveMemory

Copy the contents of the source memory block to the target memory block , It also supports overlapping source memory blocks and target memory blocks .

#include<iostream>
#include<Windows.h>


int main() {
    
	char name[256] = "qwertyuiop";
	char name1[256] = "";
	RtlMoveMemory(name1, name, strlen(name) + 1);
	printf("%s", &name1);
	return 0;
}

Process thread correlation

Create thread CreateThread

Official documents :https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread
Create a thread to execute in the virtual address space of the calling process .CreateThread It's a kind of Microsoft in Windows API The function to create a new thread is provided in , This function creates a new thread based on the main thread . After the thread terminates , The thread object is still in the system , Must pass CloseHandle Function to close the thread object .

#include<iostream>
#include<Windows.h>

LPVOID WINAPI test(LPVOID lpThreadParameter) {
    
	printf("%s", " I'm a child thread !\n");
	return 0;
};

int main() {
    
	HANDLE hThread;
	DWORD  threadId;
	hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test, 0, 0, &threadId);
	std::cout << " I'm the main thread !\n";
}

 Insert picture description here

Threads can be declared in two ways

// 1.DWORD WINAPI  Function name  (LPVOID lpParam); // A standard format 
DWORD WINAPI  Function name  (LPVOID lpParam)
{
    
    return 0;
}
CreateThread(NULL, 0,  Function name , 0, 0, 0);
// 2.void  Function name ();
//  Use void  Function name () This thread declaration method ,lpStartAddress Need to add LPTHREAD_START_ROUTINE transformation , Such as 
void  Function name ()
{
    
    return;
}
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Function name , 0, 0, 0);

LPTHREAD_START_ROUTINE It's a function , This function points to a function , Notifies the host that a thread has started executing . LPTHREAD_START_ROUTINE Type defines a pointer

WaitForSingleObject

Wait until the specified object is in the signal state or the timeout interval passes .
The main thread waits for the child thread to execute

#include<iostream>
#include<Windows.h>

LPVOID WINAPI test(LPVOID lpThreadParameter) {
    
	printf("%s", " I'm a child thread !\n");
	return 0;
};

int main() {
    
	DWORD  threadId;
	HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)test, 0, 0, &threadId);
	WaitForSingleObject(hThread, WAIT_FAILED);
	std::cout << " I'm the main thread !\n";
}

 Insert picture description here

CreateRemoteThread

Official documents :https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createremotethread

#include<iostream>
#include<Windows.h>

LPVOID WINAPI test(LPVOID lpThreadParameter) {
    
	printf("%s", " I'm a child thread !\n");
	return 0;
};

int main() {
    
	DWORD  threadId;
	HANDLE hThread = CreateRemoteThread(GetCurrentProcess(), NULL, 0, (LPTHREAD_START_ROUTINE)test, (LPVOID)1, 0, &threadId);
	WaitForSingleObject(hThread, WAIT_FAILED);
	std::cout << " I'm the main thread !\n";
}

OpenProcess

Official documents :https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess

Open the existing local process object .

CreateProcessA Allow to inherit , The main process creates the process , Subprocesses can be reused OpenProcess obtain
![ picture .png](https://img-blog.csdnimg.cn/img_convert/bddfcae86b308e8d6950a48e301635dc.png#clientId=u667e6d6f-a7ff-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=371&id=u0f15b613&margin=[object Object]&name= picture .png&originHeight=371&originWidth=904&originalType=binary&ratio=1&rotation=0&showTitle=false&size=31579&status=done&style=none&taskId=u1d5de6c2-76fe-4914-9bf8-9448b1590e8&title=&width=904)

原网站

版权声明
本文为[Tianwen_ Herbert555]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090827290694.html