首页
社区
课程
招聘
[旧帖] [求助]内核对象的继承问题 0.00雪花
发表于: 2011-10-12 08:51 1344

[旧帖] [求助]内核对象的继承问题 0.00雪花

2011-10-12 08:51
1344
看核心编程知道可以通过子进程继承的方式共享主进程的内核对象,我是将内核对象作为参数命令行的形式传递给子进程的,可是在子进程中却无法用继承到的内核对象读写文件,代码如下:求解答

主程序:
#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); //将HANDLE句柄转换为字符串类型储存
        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);
        return 0;
}

子程序:
#include <Windows.h>
#include <stdio.h>

#define MAX_BUFFER_SIZE 4096

int main(int argc,char * argv[]){
        int iResult;
        LPVOID lpFileBuffer;
        DWORD dwBytesHasRead;
        LPSTR commandLine = GetCommandLine();
        HANDLE hChildFile = (HANDLE)atol(commandLine);

        lpFileBuffer = HeapAlloc(GetProcessHeap(),0,MAX_BUFFER_SIZE);
        while(1){
                iResult = ReadFile(hChildFile,lpFileBuffer,MAX_BUFFER_SIZE,&dwBytesHasRead,NULL);
                if(!iResult){
                        printf("Read file failed.(%d)\n",GetLastError());
                        CloseHandle(hChildFile);
                        return 1;
                }
                if(dwBytesHasRead > MAX_BUFFER_SIZE){
                        HeapReAlloc(GetProcessHeap(),0,lpFileBuffer,dwBytesHasRead);
                        ZeroMemory(lpFileBuffer,dwBytesHasRead);
                }else{
                        break;
                }
        }
        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;
}

执行后结果如下:
E:\me\SdkTest\TestHandle\Debug>TestHandle.exe
Parent process id:7452
[Parent]The value of the handle is 48
[Parent]The index of the handle in table is:12
[Parent]The content of the robots.txt:
Keep going on,dude! byPnig0s1992t

Child process created.

Child process id:4684
[Child]The value of the handle is 48
[Child]The index of the handle in table is:12
[Child]The content of the robots.txt:
?

子进程读不出文件,求解答Thx

[培训]传播安全知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 343
活跃值: (40)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
楼下正解
2011-10-12 09:59
0
雪    币: 75
活跃值: (53)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
首先:通过过子进程继承的方式共享主进程的内核对象,是将内核对象作为参数命令行的形式传递给子进程的是对的,楼主方法正解
楼上说的:每个进程都有自己的句柄表,父进程的文件句柄索引12,通过命令行传给子进程是不对的,这句话是错误的,即通过命令行传给子进程是正确的

楼主之所以输出结果不对 ,需要修改处
1.在子程序代码  lpFileBuffer = HeapAlloc(GetProcessHeap(),0,MAX_BUFFER_SIZE);处加
ZeroMemory(lpFileBuffer,MAX_BUFFER_SIZE);清0, 防止打印输出为乱码
printf("子进程读文件位置:(%d)\n",SetFilePointer(hChildFile,0,0,FIL E_BEGIN)); 设置当前读文件指针位置,原因是:它们是指向相同的文件内核对象
2011-10-12 10:52
0
游客
登录 | 注册 方可回帖
返回