我想要从一个文件中 读出标准头32字节
 为何出错了!
 代码如下:
const SIGNNAME_LENGTH=32;nReal:Longint;
b:Cardinal;
SignName: array [0..SIGNNAME_LENGTH-1] of byte;
 
b:=ReadFile(hFile,@SignName,SIGNNAME_LENGTH,@nReal,nil); //(出错行,hfile是createfile的返回)

解决方案 »

  1.   

    提示出错:constant object cannot be passed as var parameter
      

  2.   

    function ReadFile(hFile: THandle; var Buffer; nNumberOfBytesToRead: DWORD;
      var lpNumberOfBytesRead: DWORD; lpOverlapped: POverlapped): BOOL; stdcall;
    http://www.emutalk.net/archive/index.php/t-4009.htmlCybermanApril 7th, 2002, 04:44
    Originally posted by Mayco 
    I have a problem: when I use the readfile api function in delphi it returns everything what i need except the first byte (when the file begins with "123..." it just returns "23...").This is the piece of code I use:procedure openfile();
    var
    dwread:LongWord;
    hFile: THandle;
    data: Shortstring;
    begin 
    hFile := createfile(PChar('c:\file.bin'), GENERIC_READ, FILE_SHARE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL + FILE_FLAG_RANDOM_ACCES, 0);
    SetFilePointer(hFile, 0, nil, FILE_BEGIN);
    Readfile(hFile, data, 64, dwread, nil);
    Closehandle(hFile);
    end;c:\file.bin is the file I want to read of course.
    data must contain the first 64 bytes of a binary file but it doesn't add the first byte. 
    I suggest not setting the file pointer the first time by commenting it out. Then run the code. Also if you have a 'file' position function I would dump that information to a debug text object in some window of your application. Useful for figuring out such things.Cyb
      

  3.   

    你没看人家的例子?
    ReadFile前先用SetFilePointer,你引用参数又不正确,将@SignName,@nReal的@去掉
      

  4.   

    看过了!我的代码如下,在编译时不通过
    var
    Result: Longint;
    bKernelExist,nStrategyCount,nKernelStrategyCount: Longint;
    reportFlow:array [0..8191] of byte;
    pReportFlow:PByte;
    nDefaultStrategyCookie: Longint;
    Password:array [0..6] of byte;
    pPassword:PByte;
    pParamCheck:Longint;
    hFile:Integer;
    nFlowID:Longint;
    pBuf,pBufTemp:PByte;
    i:Integer;
    pTEFSInfoFlowLen:Longint;
    nFileSize,nReal,b:Longint;
    SignName: array [0..SIGNNAME_LENGTH-1] of byte;
    pSignName:pchar;
    begin
        OpenDialog1.Execute;
        if trim(OpenDialog1.FileName)<>'' then begin
            showmessage(OpenDialog1.FileName);
            result:=DSF_Driver_FileCanServe(pchar(OpenDialog1.FileName));
            if result<>1 then exit;
            hFile:=DSF_Driver_CreateFile(pParamCheck,pchar(OpenDialog1.FileName),GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
            nFileSize:= GetFileSize(hfile,nil);
            if (nFileSize=0) and ((nFileSize mod TEFS_ALIGN_CONST)<>0) then exit;//不为0,或是不是256的倍数,一定不是加密文件
            //加密文件的尾部是配置块,配置块的最后32字节是签名
             b:=SetFilePointer(hFile,-SIGNNAME_LENGTH,nil,FILE_END); //seek到要求的位置
             if (b<>(nFileSize-SIGNNAME_LENGTH)) then begin CloseHandle(hFile);exit;end;  //出错
            //读出标准头32字节
            b:=ReadFile(hFile,SignName,SIGNNAME_LENGTH,nReal,nil); //从原文件中读      end;
      

  5.   

    ReadFile返回的是Boolean型,nReal改成DWORD,这才是一直不能编译的原因。我的测试代码:
    const SIGNNAME_LENGTH=32;
    var
      SignName: array [0..SIGNNAME_LENGTH-1] of byte;
      hFile:THandle;
      nReal:DWORD;
      i:integer;
      Result:string;
    begin
      hFile:=CreateFile(PChar('c:\1.mp3'),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0);
     if ReadFile(hFile,SignName,SIGNNAME_LENGTH,nReal,nil) then;
      for i:=0 to SIGNNAME_LENGTH-1 do
       Result:=Result+Format('%2.2x ',[SignName[i]]);
       ShowMessage(Result);
    end;