gpt4 book ai didi

CreateFile 崩溃... GetLastError 返回 4235960

转载 作者:行者123 更新时间:2023-11-30 17:39:38 25 4
gpt4 key购买 nike

正如标题所说,我在使用 CreateFile() 时遇到问题,这对我来说没有意义。我承认我已经十多年没有在 Windows 上进行过任何编程了,所以也许我只是错过了一些简单的东西......

int makeButtons(HWND main_window) { 

HRESULT ec;
TCHAR config_file[MAX_PATH];
LPSTR config_folder;
HANDLE config_file_handle;
int i;

ec = SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,SHGFP_TYPE_DEFAULT,config_file);
if (ec != S_OK) {
DisplayError((LPTSTR)"ec = SHGetKnownFolderPath(FOLDERID_Profile,0,NULL,config_file)", ec);
return -1;
}

i = (lstrlen(config_file)+1) * sizeof(TCHAR);
config_folder = (LPSTR)LocalAlloc(LMEM_ZEROINIT,i);
StringCbCat(config_folder,i,config_file);

StringCbCat(config_file,MAX_PATH,"\\Flipperbuilt\\Sidebar\\config.data");
MessageBox(NULL,config_file,"here",MB_OK);
config_file_handle = CreateFile(config_file,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (config_file_handle == INVALID_HANDLE_VALUE) {
MessageBox(NULL,"here","yep",MB_OK);
DisplayError((LPTSTR)"CreateFile(config_file,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)",GetLastError());
}
else {
CloseHandle(config_file_handle);
}

LocalFree(config_folder);
return 0;

}

目前所有的 MessageBox() 调用几乎都只是在调试东西......试图找出它崩溃的地方。 DisplayError() 调用是另一个函数,该函数格式化并显示 GetLastError() 中的错误代码,我确信它可以正常工作。如果我在 DisplayError() 之前省略 MessageBox() ,它会崩溃...在从 GetLastError() 获取 4325960 之前调用 MessageBox()。

有什么想法吗?!?

谢谢!!

最佳答案

我觉得很愚蠢...正如 Michael Walz 所说,我的问题出在 DisplayError 例程上。我在对 StringCchPrintf() 的调用中的列表中有一个额外的变量,该变量没有与之关联的任何格式(%s)信息(自从它最后工作以来我对它进行了更改,但没有意识到它) !!)再次感谢大家的意见。

int makeButtons(HWND main_window) { 

HRESULT ec;
TCHAR config_file[MAX_PATH];
LPTSTR config_folder;
HANDLE config_file_handle;
int i;

ec = SHGetFolderPath(NULL,CSIDL_LOCAL_APPDATA,NULL,SHGFP_TYPE_DEFAULT,config_file);
if (ec != S_OK) {
DisplayError("ec = SHGetKnownFolderPath(FOLDERID_Profile,0,NULL,config_file)", ec);
return -1;
}

i = (lstrlen(config_file)+1) * sizeof(TCHAR);
config_folder = (LPSTR)LocalAlloc(LMEM_ZEROINIT,i);
StringCbCat(config_folder,i,config_file);

StringCbCat(config_file,MAX_PATH,"\\Flipperbuilt\\Sidebar\\config.data");
config_file_handle = CreateFile(config_file,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (config_file_handle == INVALID_HANDLE_VALUE) {
DisplayError("CreateFile(config_file,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)",GetLastError());
}
else {
CloseHandle(config_file_handle);
}

LocalFree(config_folder);
return 0;
}

void DisplayError(const char* lpszFunction, DWORD dw) {
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
LPTSTR error_string_in = TEXT((LPSTR)lpszFunction);
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,(lstrlen(error_string_in)+(lstrlen((LPCTSTR)lpMsgBuf) + 10)) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,LocalSize(lpDisplayBuf) / sizeof(TCHAR),TEXT("%d: %s"),dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, error_string_in, MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
return;
}

还有什么我错过的或者需要学习的吗?!?正如我之前所说,我确实有一定的了解...只是还没有掌握细节。

关于CreateFile 崩溃... GetLastError 返回 4235960,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21750243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com