1. 程式人生 > 其它 >程序相關API

程序相關API

ID與控制代碼

控制代碼每一個程序都有一張自己的私有的表,這張表儲存著私有的核心物件.
系統控制代碼表

oenProcess

Opens an existing local process object.
HANDLE WINAPI OpenProcess(
  DWORD dwDesiredAccess,//開啟程序的的許可權問題
  BOOL bInheritHandle,//允不允許子程序繼承呢
  DWORD dwProcessId  //全域性PID
);

以掛起的形式建立程序

Creates a new process and its primary thread. The new process runs in the security context of the calling process.
建立一個新程序和它的主要執行緒。這個新程序跑再一個安全正在呼叫的程序上下文中

If the calling process is impersonating another user, the new process uses the token for the calling process, not the impersonation token. To run the new process in the security context of the user represented by the impersonation token, use the CreateProcessAsUser or CreateProcessWithLogonW function.

BOOL WINAPI CreateProcess(
  LPCTSTR lpApplicationName,
  LPTSTR lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes,
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL bInheritHandles,
  DWORD dwCreationFlags,//建立一個新的控制檯
  LPVOID lpEnvironment,
  LPCTSTR lpCurrentDirectory,
  LPSTARTUPINFO lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation
);

回顧

1.程序的建立

1.任何程序都是別的程序建立的:CreateProcess()
2.程序建立過程
1.對映EXE檔案
2.建立核心物件EPROCESS
3.對映系統DLL(ntdll.dll)
4.建立執行緒核心物件ETHREAD
5.系統啟動執行緒
對映DLL(ntdll.KdrInitialize Thunk)
執行緒開始執行

2.程序的建立

1.對映EXE檔案
2.建立核心物件EPROCESS
3.對映系統DLL(ntdll.dll)
4.建立執行緒核心物件ETHREAD
5.如果是掛起的方式建立的:
.....
6.恢復以後再繼續執行
對映DLL(ntdll.KdrInitializeThunk)
執行緒開始執行

模組與工作目錄

char strModule[256];
GetModuleFileName(NULL,strModule,256);//模組目錄
char strWork[1000];
int i = 1000;
GetCurrentDirectory(1000,buf);//工作目錄
printf("某塊目錄:%s\n 工作目錄:%s\n",strModule,strWork);

GetModuleFileName

獲取當前模組的路徑

GetCurrentDirectory

獲取現有目錄

其他相關API

  • 獲取程序PID
    GetCurrentProcessId
  • 獲取程序控制代碼
    GetCurrentProcess
  • 獲取命令列
    GetCommandLine
  • 獲取啟動資訊
    GetStartupinfo
  • 遍歷程序ID
    EnumProcesses
  • 快照
    CreateToolhelp32napshot