无法读取继承进程中的文件
Can't read file in inherited process
我正在尝试在继承进程中读取文件,我通过命令行传递的文件句柄有效,但GetFileSize(HANDLE,LPDWORD)
返回0
。
#include"mainClass.h"
MainClass* MainClass::ptr = NULL;
MainClass::MainClass()
{
ptr = this;
}
BOOL CALLBACK MainClass::DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hwnd,WM_CLOSE,ptr->OnClose);
HANDLE_MSG(hwnd,WM_COMMAND,ptr->OnCommand);
HANDLE_MSG(hwnd,WM_INITDIALOG,ptr->OnInitDialog);
}
return false;
}
BOOL MainClass::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES),0,true};
hFile = CreateFile(_T("D:/mutex.txt"),GENERIC_READ,0,&sa,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
return true;
}
void MainClass::OnClose(HWND hwnd)
{
DestroyWindow(hwnd);
PostQuitMessage(0);
}
void MainClass::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
if(codeNotify == BN_CLICKED)
{
if(id == IDC_BUTTON_READ_ENCRYPT)
{
TCHAR* cmd = new TCHAR[100];
_stprintf(cmd,_T("readFromFile.exe %d"),(int)hFile);
STARTUPINFO si = {sizeof(STARTUPINFO)};
PROCESS_INFORMATION pi = {0};
if(CreateProcess(NULL,cmd,NULL,NULL,true,NULL,NULL,NULL,&si,&pi))
{
//CloseHandle(hFile);
//CloseHandle(pi.hThread);
//CloseHandle(pi.hProcess);
}
delete[]cmd;
}
}
}
继承过程是控制台应用程序:
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<tchar.h>
PTSTR buf;
HANDLE hFile;
void main()
{
int s = 100;
buf = new TCHAR[s];
TCHAR* temp = buf;
_tcscpy(buf,GetCommandLine());
while(*buf++ != _T(' '));
hFile = (HANDLE)_ttoi(buf);
_tprintf(_T("%dn"),(int)hFile);
getch();
buf = temp;
delete[]buf;
if(hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,_T("file handle invalid"),_T("Error"),NULL);
return;
}
DWORD fileSize;
GetFileSize(hFile,&fileSize);
_tprintf(_T("%dn"),(int)fileSize);
getch();
if(!fileSize)
{
MessageBox(NULL,_T("file is empty"),_T(""),NULL);
return;
}
buf = new TCHAR[fileSize/sizeof(TCHAR)+1];
DWORD wasRead;
ReadFile(hFile,buf,fileSize,&wasRead,NULL);
_tprintf(buf);
if(buf)
delete[]buf;
getch();
}
这里有什么问题,请帮助:)。
GetFileSize
需要处理大于 4GB 的文件,因此它应该能够处理long long
或int64_t
但在 Windows API 中int64_t
通常用于 32 位整数值,因此此函数返回低阶 32 位结果作为返回值,并且在第二个参数中只有高 32 位结果,并且您的文件小于 4GB, 所以它的高阶 32 位总是 0。所以使用fileSize = GetFileSize(hFile, NULL)
,一切都按预期工作
相关文章:
- c++多进程编写一个唯一的文件
- 避免使用 boost::进程间::消息队列创建文件
- 从根权限进程创建可访问的文件
- Windows 将标准的 C 文件结构注入到正在运行的进程中
- gcc 找不到 -lgcc, g++.exe: 错误: 创建进程: 没有这样的文件或目录
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 如何在 Linux 下使用 c++ 知道文件是否被其他进程使用?
- 多个线程/进程是否可以在不同步的情况下同时从/写入文件的非重叠区域?
- 如何检测哪些进程更改,重命名或创建文件?
- 如何:监视Windows中另一个进程的文件访问?
- 提升进程间共享内存删除、权限和输出文件
- 使用文件锁定的进程之间的条件变量
- 当多个进程尝试同时替换同一文件时,Linux 如何处理这种情况?
- COM 客户端连接 COM 服务器所需的文件是什么(进程外方案)
- 如何在我的进程中运行的二进制文件中注册操作和内核
- 为什么我的 Redhat 服务器上的 QuickFIX 进程没有将其核心文件写入应有的位置?
- Growing Boost.使用单个写入器的进程间内存映射文件
- C++ Mingw32 创建进程() 失败,错误代码 2:系统找不到指定的文件
- 如何从安卓原生二进制文件运行单独的进程
- Python 子进程.Popen 在 g++ 中编译,自动添加.cpp文件