Win32 WriteFile返回0字节作为写入的字节数。 lpBuffer包含4455个字节

时间:2016-08-18 17:50:15

标签: c windows debugging winapi

我有以下代码创建两个文件。当调用最后4455字节的写操作时,它返回0作为写入的字节数。这可能是由于顺序扫描的dwFlagAttributes而没有缓冲吗?

hIn = CreateFile (fIn, GENERIC_READ, 0, NULL, OPEN_EXISTING, 
                    FILE_ATTRIBUTE_NORMAL | SSF | NBF, NULL);

Desired Access: Generic Read
Disposition:    Open
Options:    Sequential Access, No Buffering, Synchronous IO Non-Alert, Non-Directory File
Attributes: N
ShareMode:  None
AllocationSize: n/a
OpenResult: Opened

hOut = CreateFile (fOut, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
                    FILE_ATTRIBUTE_NORMAL | SSF | NBF, NULL);

Desired Access: Generic Write, Read Attributes
Disposition:    OverwriteIf
Options:    Sequential Access, No Buffering, Synchronous IO Non-Alert, Non-Directory File
Attributes: N
ShareMode:  None
AllocationSize: 0
OpenResult: Created     

创建文件后,会有一个读取file1的while循环。然后将字节写入file2。 file1大约是13MB。

while (ReadFile (hIn, aBuffer, BUF_SIZE, &nIn, NULL) && nIn > 0 && WriteOK) {
        for (iCopy = 0; iCopy < nIn; iCopy++)
            ccBuffer [iCopy] = (BYTE)((aBuffer [iCopy] + shift) % 256);
        WriteOK = WriteFile (hOut, ccBuffer, nIn, &nOut, NULL);
    }

从下面的反汇编中可以明显看出最后4455个字节没有被写入file2。这可能是由于CreateFile dwFlagAttributes?

0:000> kb
 # ChildEBP RetAddr  Args to Child
00 0018bd00 00411612 00000038 0018bdf8 00001167 kernel32!WriteFile
01 0018fe64 0041144c 00273a99 00273ac9 0000000a cci!cci_f+0x162
02 0018ff44 00411bbb 00000004 00273a78 002723e8 cci!main+0x6c
03 0018ff88 7698336a 7efde000 0018ffd4 77c29f72 cci!__tmainCRTStartup+0x122
04 0018ff94 77c29f72 7efde000 76d02536 00000000 kernel32!BaseThreadInitThunk+0xe
05 0018ffd4 77c29f45 00411122 7efde000 00000000 ntdll!__RtlUserThreadStart+0x70
06 0018ffec 00000000 00411122 7efde000 00000000 ntdll!_RtlUserThreadStart+0x1b

0:000> dd 0018bd00 la
0018bd00  0018fe64 00411612 00000038 0018bdf8
0018bd10  00001167 0018fe34 00000000 0018ff44
0018bd20  0018fe78 00000000

0:000> $number of bytes to be written 0x1167
0:000> .formats 00001167
Evaluate expression:
  Hex:     00001167
  Decimal: 4455
  Octal:   00000010547
  Binary:  00000000 00000000 00010001 01100111
  Chars:   ...g
  Time:    Wed Dec 31 20:14:15 1969
  Float:   low 6.24278e-042 high 0
  Double:  2.20106e-320

0:000> gu
eax=00000000 ebx=00000000 ecx=76b2df07 edx=00000057 esi=0018bd1c edi=0018fe64
eip=00411612 esp=0018bd1c ebp=0018fe64 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
cci!cci_f+0x162:
00411612 cmp     esi,esp

0:000> $number of bytes written
0:000> dd 0018fe34 l1
0018fe34  00000000

enter image description here

1 个答案:

答案 0 :(得分:4)

由于您编写的是非缓冲的,因此您需要遵守此处记录的对齐要求:https://msdn.microsoft.com/en-us/library/windows/desktop/cc644950.aspx

具体而言,文件访问必须以卷扇区大小的倍数开始,并且其大小是卷扇区大小的倍数。您的ModelRequestCommand = new DelegateCommand(PopulateModelRequest); 大小的缓冲区不符合第二个要求。

当然,如果你想写那些最后的4455个字节而不是更多,那么你就是一个quardry。您需要将其四舍五入到卷扇区大小的倍数,并写入实际的4455字节,然后填充。将文件指针设置回那些4455字节的末尾,然后调用4455