C++ visualc&x2B+;检查文件是否存在的DLL
我制作了这个dll文件,试图检查文件是否存在。但即使我手动创建文件,我的dll仍然找不到它 My dll检索正在运行的程序的进程id,并查找以pid命名的文件 谁能告诉我我错过了什么( 代码:C++ visualc&x2B+;检查文件是否存在的DLL,c++,visual-c++,ifstream,C++,Visual C++,Ifstream,我制作了这个dll文件,试图检查文件是否存在。但即使我手动创建文件,我的dll仍然找不到它 My dll检索正在运行的程序的进程id,并查找以pid命名的文件 谁能告诉我我错过了什么( 代码: #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int clientpid=GetCurrentProcessId(); ifstreamclientfile; 字符串clientpids,clientfilepath; VOID LoadDLL(){ allocsole()
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int clientpid=GetCurrentProcessId();
ifstreamclientfile;
字符串clientpids,clientfilepath;
VOID LoadDLL(){
allocsole();
freopen(“CONOUT$”、“w”、stdout);
std::cout假设您正在使用UNICODE
我认为问题在于以下几行:
ostr您做了什么来调试这个问题?您能准确地描述发生了什么,以及这与您期望的有什么不同吗?如果您在常规程序中使用它,而不是作为DLL使用它,它会工作吗?仅仅因为您无法打开一个文件,并不意味着该文件不存在。使用GetFileAttributes(szFile)!=无效的\u文件\u属性
以测试是否存在。好的,我尝试更改了一点永久循环,但出现了一个错误\u无效\u名称错误。我是否将文件路径从字符串转换为LPCTSTR错误?我在启动项目时将属性设置为多字节:(是的,我仔细检查了它。我真的不明白为什么他找不到它:(无论如何,我会尝试切换到unicode并尝试你的建议,因为这比根本没有解决方案要好。在commnet中快速回答一下……你在GetSystemDirectory
旁边的ostr
中插入了一条换行符,看:ostr就是这样!这就是错的地方!哈哈哈,上帝,这个错误太基本了,我不想麻烦了。)反复检查:O这个错误出现了,因为我只是出于懒惰而复制我的cout行。谢谢@spamdark这就像是对人们隐藏一样(所有回答你问题的人都没有注意到…。我认为这是一个大失败,无论如何,欢迎:)!
#include <Windows.h>
#include <winbase.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int clientpid = GetCurrentProcessId();
ifstream clientfile;
string clientpids, clientfilepath;
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
ostr << tempcvar << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}
VOID LoadDLL() {
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Debug Start" << std::endl;
std::ostringstream ostr;
ostr << clientpid;
clientpids = ostr.str();
ostr.str("");
TCHAR tempcvar[MAX_PATH];
GetSystemDirectory(tempcvar, MAX_PATH);
// Convertion between tchar in unicode (wide char) and multibyte
wchar_t * tempcvar_widechar = (wchar_t*)tempcvar;
char* to_convert;
int bytes_to_store = WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,NULL,0,NULL,NULL);
to_convert = new char[bytes_to_store];
WideCharToMultiByte(CP_ACP,
0,
tempcvar_widechar,
-1,to_convert,bytes_to_store,NULL,NULL);
// Using char* to_convert that is the tempcvar converted to multibyte
ostr << to_convert << "\\" << clientpids << ".nfo" << std::endl;
clientfilepath = ostr.str();
//clientfile.c_str()
ostr.str("");
std::cout << "Start search for: " << clientfilepath << std::endl;
FOREVER {
clientfile.open(clientfilepath,ios::in);
if(clientfile.good()) {
std::cout << "Exists!" << std::endl;
}
Sleep(10);
};
}