我有一个基本的 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;
TCHAR FilePathPattern[] = TEXT("C:\\Users\\Bloodsucker94\\Desktop\\TestASM\\*.txt");
if ((hFile = FindFirstFile(FilePathPattern, &findFileData)) == INVALID_HANDLE_VALUE)
_tprintf(TEXT("FindFirstFile() failed with code %d\n"), GetLastError());
else {
_tprintf(TEXT("hFile=%d, addr=0x%08X\n"), hFile, &findFileData);
do {
TCHAR beginPath[] = TEXT("C:\\Users\\Bloodsucker94\\Desktop\\TestASM\\");
lstrcat(beginPath, findFileData.cFileName);
_tprintf(TEXT("%s\n"), beginPath);
//printf("%s\n", beginPath);
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
includelib \masm32\lib\crtdll.lib
_tprintf PROTO C :VARARG
.data
FolderPath TCHAR "C:\Users\Bloodsucker94\Desktop\TestASM\*.txt", 0
BeginFolderPath TCHAR "C:\Users\Bloodsucker94\Desktop\TestASM\", 0
FileName TCHAR "ta_mere.txt", 0
LstrcatFailed BYTE "lstrcat failed", 0
FormatPrintString BYTE "%s\n", 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 ?
FinalFilePath LPTSTR ?
.code
start:
;--------------------------------------------------------
invoke FindFirstFile, ADDR FolderPath, \
ADDR findFileData
mov hFile, eax
.IF hFile == INVALID_HANDLE_VALUE
invoke GetLastError
mov ErrorCode, eax
printf("%d\n", ErrorCode)
jmp _quit
.ENDIF
;--------------------------------------------------------
_loop:
invoke lstrcat, ADDR BeginFolderPath,
ADDR findFileData.cFileName
mov FinalFilePath, eax
.IF FinalFilePath == NULL
invoke MessageBox, NULL, \
ADDR LstrcatFailed, \
ADDR LstrcatFailed, \
MB_OK
.endif
;invoke crt_printf, ADDR FormatPrintString, \
; findFileData.cFileName
invoke MessageBox, NULL, \
ADDR FinalFilePath, \
ADDR FinalFilePath, \
MB_OK
;--------------------------------------------------------
invoke FindNextFile, hFile, \
ADDR findFileData
.IF eax == NULL
jmp _quit
.ELSE
jmp _loop
.ENDIF
;--------------------------------------------------------
_quit:
invoke ExitProcess, 0
end start
编译工作完美。lstrcat 函数也不会失败。但在执行时,消息框会显示字符串“-O@”。当我想使用 printf 而不是 _tprintf 打印时,我注意到 c++ 程序中的相同行为(因为打印 TCHAR Windows 类型不适用于 printf)。也许问题来自 MessageBox 函数,或者 TCHAR 类型不是好的类型。只有 c++ 程序有效。我迷路了。
有人可以帮助我吗?
在此先感谢您的帮助。