Вопрос

I'm trying to read the file in inherited process, the file handle which I passed through command line is valid, but GetFileSize(HANDLE,LPDWORD) returns 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;
        }
    }
}

Inherit process is console application:

#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();
}

What is wrong here, please help :).

Это было полезно?

Решение

GetFileSize is need to handle files large than 4GB and thus it should be able to work with long long or int64_t but in windows API int64_t usually used as to 32 bit integral value so this function return low order 32 bit of result as return value and only high 32 bit of result in second argument and your file is less than 4GB, so its high order 32 bit is always 0. so use fileSize = GetFileSize(hFile, NULL) and every thing work as expected

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top