我想在 MASM 中重现 C++ 代码的行为:
C++ 代码:
#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
int _tmain(int ac, TCHAR **av)
{
HANDLE hFile;
WIN32_FIND_DATA findFileData;
BOOL retFindNextFile;
if ((hFile = FindFirstFile(TEXT("C:\\Users\\Bloodsucker94\\Desktop\\TestFolder\\*.txt"), &findFileData)) == INVALID_HANDLE_VALUE)
else {
do {
_tprintf(TEXT("%s\n"), findFileData.cFileName);
retFindNextFile = FindNextFile(hFile, &findFileData);
} while (retFindNextFile == TRUE);
}
getchar();
return (EXIT_SUCCESS);
}
和 MASM 代码:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\include\masm32rt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib
.data
FolderPath BYTE "C:\Users\Bloodsucker94\Desktop\TestASM\*.txt", 0
FindFirstFileError BYTE "FindFirstFile() failed with code %d", 0
FindFirstFileSuccess BYTE "First file found with success - hfile=%d", 0
PrintStructAddr BYTE "addr=Ox%08X", 0
PrintFileName BYTE "%s", 0
.data?
hFile HANDLE ?
findFileData WIN32_FIND_DATA <>
retFindNextFile BOOL ?
ErrorCode DWORD ?
.code
start:
;--------------------------------------------------------
invoke FindFirstFile, ADDR FolderPath, \
ADDR findFileData
mov hFile, eax
.IF hFile == INVALID_HANDLE_VALUE
invoke GetLastError
mov ErrorCode, eax
invoke crt_printf, ADDR FindFirstFileError, \
ErrorCode
jmp _quit
.ENDIF
;--------------------------------------------------------
mov ebx, OFFSET findFileData
mov al, [ebx].WIN32_FIND_DATA.cFileName
print str$(findFileData.cFileName)
;print str$([ebx].WIN32_FIND_DATA.cFileName)
;print str$(al)
;INVOKE crt_printf, ADDR PrintFileName, \
; findFileData.cFileName
;--------------------------------------------------------
_quit:
invoke ExitProcess, 0
end start
目前,如您所见,我只想打印目录“TestASM”中的第一个文件名。在执行时 FindFirstFile() 似乎执行得很好,并且 hFile 句柄似乎也是正确的。但是我不明白为什么我所有的打印调用都会导致一个显示错误消息的消息框。我认为这是一个分段错误。但我尊重 C++ 代码行为。我徒劳地尝试了几种代码组合。
有人可以帮助我吗?