CreateFile这个函数打开文件的时候总是失败,大家帮忙分析一个,这是什么原因呢?
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>HANDLE hFile;
HANDLE hFileMapping=NULL;
void *pFileBase=NULL;int main(int argc,char **argv)
{
if(argc!=2)
{
printf("Usage:\n\t %s  PE File\n",argv[0]);
exit(1);
}

/*********************** Map the file to memory *************************/
hFile=CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
FILE_ATTRIBUTE_ARCHIVE,OPEN_EXISTING,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
/*printf("Open File Error.\n");*/
printf("Open File Error\n");
exit(2);
}

hFileMapping=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);
if(!hFileMapping)
{
CloseHandle(hFile);
perror("Create File Mapping Error\n");
exit(3);
}
pFileBase=MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0);
if(!pFileBase)
{
CloseHandle(hFileMapping);
CloseHandle(hFile);
perror("Map View of File Error.\n");
exit(4);
}
/*********************** Map end *****************************************/


return EXIT_SUCCESS;
}

解决方案 »

  1.   

    参数 argv[1] 是什么? 此文件是否存在??GetLastError() 是多少?
      

  2.   

    貌似是文件不存在  你打开的时候用了OPEN_EXISTING
      

  3.   

    // fileDemo.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"#include<time.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<windows.h>
    #include "tchar.h"
    HANDLE hFile;
    HANDLE hFileMapping=NULL;
    void *pFileBase=NULL;int main(int argc,char *argv[])
    {    
    if(argc!=2)
        {
            printf("Usage:\n\t %s  PE File\n",argv[0]);
            exit(1);
        }
        
        /*********************** Map the file to memory *************************/
        hFile=CreateFile(argv[1],GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES)NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ,(HANDLE)NULL);
        if(hFile==INVALID_HANDLE_VALUE)
        {
            /*printf("Open File Error.\n");*/
            printf("Open File Error\n");
            exit(2);
        }
        
        hFileMapping=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);
        if(!hFileMapping)
        {
            CloseHandle(hFile);
            perror("Create File Mapping Error\n");
            exit(3);
        }
        pFileBase=MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0);
        if(!pFileBase)
        {
            CloseHandle(hFileMapping);
            CloseHandle(hFile);
            perror("Map View of File Error.\n");
            exit(4);
        }
        /*********************** Map end *****************************************/
        
        
        return EXIT_SUCCESS;
    }
      

  4.   

    添加 debug 参数 
    c:\1.txt你的 API 参数写错了
    晕倒。 好好看看API 怎么用