小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛
标题:
api一日一练 第9天 (点评版)赠送 Delphi版+汇编版本
[打印本页]
作者:
七液
时间:
2013-2-8 15:18
标题:
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[0..100] Of Char;
szText: Array[0..50] 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-->[url]xqmsyk.com[/url]', 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-->[url]xqmsyk.com[/url]", 0
szCaption db "xqmsyk.com", 0
.code
WinMain proc
LOCAL @hFile: DWORD
LOCAL @dwFileSize, @dwBytes: DWORD
LOCAL @szBuf[100]: BYTE
LOCAL @szText[50]: 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
复制代码
作者:
echotxl
时间:
2013-4-6 14:13
感谢楼主的补充和改正。。。。。。
欢迎光临 小七技术论坛,计算机安全和杀毒软件,编程交流于软件安全技术交流,给新手一个绿色安全的论坛 (https://xqmsyk.com/)
Powered by Discuz! X3.3