-
-
[已解决]win32底下用_tfopen_s打开文件的时候会把传进去的参数flush掉?
-
发表于:
2017-4-16 16:04
3816
-
[已解决]win32底下用_tfopen_s打开文件的时候会把传进去的参数flush掉?
#include "stdafx.h"
HINSTANCE hInst;
INT_PTR CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
struct _PE_INFO{
TCHAR* szFileName;
};
class PEFile{
public:
_PE_INFO pe_info;
public:
PEFile();
virtual ~PEFile();
public:
int ReadPEFile(_PE_INFO& pe);
VOID PEFile::OpenFile(LPOPENFILENAME lpOpenFileName, HWND hWndDlg);
};
PEFile::PEFile(){}
PEFile::~PEFile(){}
int PEFile::ReadPEFile(_PE_INFO& pe){
FILE* fp = NULL;
errno_t t = _tfopen_s(&fp, pe.szFileName, TEXT("rb"));
return t;
}
VOID PEFile::OpenFile(LPOPENFILENAME lpOpenFileName, HWND hWndDlg){
TCHAR szPeFileExt[100] = TEXT("*.exe;*.dll;*.scr;*.drv;*.sys");
TCHAR szFileName[256];
memset(szFileName, 0, 256);
memset(lpOpenFileName, 0, sizeof(*lpOpenFileName));
lpOpenFileName->lStructSize = sizeof(*lpOpenFileName);
lpOpenFileName->Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
lpOpenFileName->hwndOwner = hWndDlg;
lpOpenFileName->lpstrFilter = szPeFileExt;
lpOpenFileName->lpstrFile = szFileName;
lpOpenFileName->nMaxFile = MAX_PATH;
GetOpenFileName(lpOpenFileName);
}
INT_PTR CALLBACK MainDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam, LPARAM lParam){
PEFile test;
OPENFILENAME stOpenFileName = { 0 };
int t = -1
switch (uMessage)
{
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
case WM_INITDIALOG:
test.OpenFile(&stOpenFileName,hDlg);
test.pe_info.szFileName = stOpenFileName.lpstrFile;
t = test.ReadPEFile(test.pe_info);
break;
default:
break;
}
return 0;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
//test.pe_info.szFileName = TEXT("C:\\putty.exe"); 用常量传进去的时候正常
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, MainDlgProc);
getchar();
return 0;
}
不知道是不是c++代码写的问题,跟进去_tfopen_s的时候发现,在堆栈空间增长的时候,会把传进去的内存部分冲掉,然后真正打开文件的时候,就返回“找不到文件”的错误了。后来只能用Handle的方式解决打开文件的问题,但是不明白这个程序为什么不行。
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)