Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';无法在继承的进程中读取文件_C++_File_Winapi_Systems Programming - Fatal编程技术网

C++ Can';无法在继承的进程中读取文件

C++ Can';无法在继承的进程中读取文件,c++,file,winapi,systems-programming,C++,File,Winapi,Systems Programming,我试图在继承的进程中读取文件,通过命令行传递的文件句柄是有效的,但是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)

我试图在继承的进程中读取文件,通过命令行传递的文件句柄是有效的,但是
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("%d\n"),(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("%d\n"),(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();
}
#包括
#包括
#包括
#包括
PTSTR-buf;
处理文件;
void main()
{
int s=100;
buf=新的TCHAR[s];
TCHAR*temp=buf;
_tcscpy(buf,GetCommandLine());
而(*buf++!=_T(“”));
hFile=(句柄)\u ttoi(buf);
_tprintf(_T(“%d\n”),(int)hFile);
getch();
buf=温度;
删除[]buf;
if(hFile==无效的句柄值)
{
MessageBox(NULL,_T(“文件句柄无效”),_T(“错误”),NULL);
返回;
}
DWORD文件大小;
GetFileSize(hFile,&fileSize);
_tprintf(_T(“%d\n”),(int)文件大小);
getch();
如果(!fileSize)
{
MessageBox(NULL,_T(“文件为空”),_T(“”,NULL);
返回;
}
buf=newtchar[fileSize/sizeof(TCHAR)+1];
德沃德·瓦斯里德;
ReadFile(hFile,buf,fileSize,&wasRead,NULL);
_tprintf(buf);
如果(buf)
删除[]buf;
getch();
}

这里怎么了,,请帮助:)。

GetFileSize
需要处理大于4GB的文件,因此它应该能够处理
long
int64_t
但在windows API中
int64_t
通常用于32位整数值,因此此函数返回结果的低阶32位作为返回值,仅返回结果的高32位结果在第二个参数中,您的文件小于4GB,因此其高阶32位始终为0。所以使用
fileSize=GetFileSize(hFile,NULL)
而且一切都能按预期工作

GetFileSize
需要处理大于4GB的文件,因此它应该能够处理
long-long
int64_t
但在windows API中
int64_t
通常用于32位整数值,因此此函数返回结果的低阶32位作为返回值,并且第二个参数中只有高32位的结果,并且您的文件小于4GB,因此其高32位始终为0。因此,使用
fileSize=GetFileSize(hFile,NULL)
一切都会按预期工作