api一日一练 第9天 (点评版)赠送 Delphi版+汇编版本
废话:首先我敢说小七写这代码绝对没有编译,想着就写了
memset(buf,0,sizeof(buf));--这句是无法编译的应该
memset(&buf,0,sizeof(buf));
ReadFile(hFile,buf,cc,&dd,NULL);这里也是一样的问题
应该传入指针&buf,
而且使用的是静态栈空间,如果文件大于100字节将造成栈溢出
懒得修改了,直接翻译吧不多说了
Delphi版本:
program Sdk9;
uses
Windows;
function wsprintf(Output: PChar; Format: PChar): Integer; cdecl varargs;
external user32 name 'wsprintfA';
var
hFile: THandle;
dwFileSize, dwBytes: DWORD;
szBuf: Array Of Char;
szText: Array Of Char;
begin
hFile := CreateFile('c:\1.exe', GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then
begin
MessageBox(0, '文件不存在或者无法打开', 'xqmsyk.com', 0);
Exit;
end;
ZeroMemory(@szBuf, SizeOf(szBuf));
dwFileSize := GetFileSize(hFile, nil); //获取文件大小
ReadFile(hFile, szBuf, dwFileSize, dwBytes, nil); //读取文件的内容
wsprintf(szText, 'Size:字节 %d-->xqmsyk.com', dwBytes);
MessageBox(0, szBuf, szText, 0);
CloseHandle(hFile);
end.
汇编版本:
.686
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data
szFile db "c:\1.exe", 0
szError db "文件不存在或者无法打开", 0
szFmt db "Size:字节 %d-->xqmsyk.com", 0
szCaption db "xqmsyk.com", 0
.code
WinMain proc
LOCAL @hFile: DWORD
LOCAL @dwFileSize, @dwBytes: DWORD
LOCAL @szBuf: BYTE
LOCAL @szText: BYTE
invoke CreateFile, offset szFile, GENERIC_WRITE, FILE_SHARE_WRITE, \
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
.if (eax == INVALID_HANDLE_VALUE)
invoke MessageBox, 0, offset szError, offset szCaption, 0
ret
.endif
mov @hFile, eax
invoke RtlZeroMemory, addr @szBuf, sizeof @szBuf
invoke GetFileSize, @hFile, NULL
mov @dwFileSize, eax
invoke ReadFile, @hFile, addr @szBuf, @dwFileSize, addr @dwBytes, NULL
invoke wsprintf, addr @szText, offset szFmt, @dwBytes
invoke MessageBox, 0, addr @szBuf, addr @szText, 0
invoke CloseHandle, @hFile
ret
WinMain endp
start:
invoke WinMain
ret
end start
感谢楼主的补充和改正。。。。。。
页:
[1]