Pnig0s p.s:将主进程的内核对象句柄通过CreateProcess的lpCmdLine参数传递给子进程,当然之前还要自定义一个SECURITY_ATTRIBUTES结构传递给创建内核对象的函数来将内核对象句柄设置为可继承的,然后在CreateProcess中将bInheritHandle置为TRUE使子进程可以将主进程的所有可继承的内核对象句柄复制到自己的内核对象句柄表中,从而实现不同进程间的内核对象的共享。小实践,网上代码资料比较少,贴出来方便大家吧 主进程:
- #include <Windows.h>
- #include <stdio.h>
-
- #define MAX_BUFFER_SIZE 4096
- int main(int argc,char * argv[]){
- HANDLE hFile;
- LPVOID lpFileBuffer;
- DWORD dwBytesInFile;
- int iResult;
- SECURITY_ATTRIBUTES sa;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
-
- ZeroMemory(&si,sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi,sizeof(pi));
-
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
-
- hFile = CreateFile("robots.txt",GENERIC_READ,FILE_SHARE_READ,&sa,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
- if(hFile == INVALID_HANDLE_VALUE){
- printf("Create file handle failed.(%d)\n",GetLastError());
- CloseHandle(hFile);
- return 1;
- }
- lpFileBuffer = HeapAlloc(GetProcessHeap(),0,MAX_BUFFER_SIZE);
- while(1){
- iResult = ReadFile(hFile,lpFileBuffer,MAX_BUFFER_SIZE,&dwBytesInFile,NULL);
- if(!iResult){
- printf("Read file failed.(%d)\n",GetLastError());
- CloseHandle(hFile);
- return 1;
- }
- if(dwBytesInFile > MAX_BUFFER_SIZE){
- HeapReAlloc(GetProcessHeap(),0,lpFileBuffer,dwBytesInFile);
- ZeroMemory(lpFileBuffer,dwBytesInFile);
- }else{
- break;
- }
- }
- printf("Parent process id:%d\n",GetCurrentProcessId());
- printf("[Parent]The value of the handle is %u\n",hFile);
- printf("[Parent]The index of the handle in table is:%u\n",((DWORD)hFile/4));
- printf("[Parent]The content of the robots.txt:\n%s\n\n\n",lpFileBuffer);
-
- LPSTR lpCommandLine;
- lpCommandLine = (LPSTR)HeapAlloc(GetProcessHeap(),0,1024);
- ltoa((DWORD)hFile,lpCommandLine,10);
- if(!CreateProcess("ChildProcess.exe",lpCommandLine,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)){
- printf("Create child process failed.(%d)\n",GetLastError());
- return 1;
- }
- printf("Child process created.\n\n\n");
- CloseHandle(hFile);
- WaitForSingleObject(pi.hProcess,INFINITE);
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- system("pause");
- return 0;
- }
子进程:
- #include <Windows.h>
- #include <stdio.h>
-
- #define MAX_BUFFER_SIZE 1024
-
- int main(int argc,char * argv[]){
- int iResult;
- LPSTR lpFileBuffer;
- DWORD dwBytesHasRead;
- LPSTR commandLine = GetCommandLine();
- HANDLE hChildFile = (HANDLE)atol(commandLine);
- lpFileBuffer = (LPSTR)HeapAlloc(GetProcessHeap(),0,MAX_BUFFER_SIZE);
-
- SetFilePointer(hChildFile,NULL,NULL,FILE_BEGIN);
- iResult = ReadFile(hChildFile,lpFileBuffer,MAX_BUFFER_SIZE,&dwBytesHasRead,NULL);
- if(!iResult){
- printf("Read file failed.(%d)\n",GetLastError());
- CloseHandle(hChildFile);
- return 1;
- }
- printf("Child process id:%d\n",GetCurrentProcessId());
- printf("[Child]The value of the handle is %u\n",(DWORD)hChildFile);
- printf("[Child]The index of the handle in table is:%u\n",((DWORD)hChildFile/4));
- printf("[Child]The content of the robots.txt:\n%s\n\n\n",lpFileBuffer);
- CloseHandle(hChildFile);
- return 0;
- }
运行结果:
|