c ++ loadlibrary - 导入dll

时间:2018-02-05 21:10:23

标签: c++ visual-studio winapi

你可以告诉我这个c ++代码有什么问题吗?它总是返回E_INVALIDARG

typedef HRESULT STDAPICALLTYPE AcquireDeveloperLicense(
    _In_opt_ HWND     hwndParent,
    _Out_    FILETIME *pExpiration
    );
HINSTANCE hDll = LoadLibrary(TEXT("WSClient.dll"));
AcquireDeveloperLicense *acquire_license = (AcquireDeveloperLicense*)GetProcAddress(hDll, "AcquireDeveloperLicense");
FILETIME *pExpiration = NULL;
HWND hwnd = GetConsoleWindow();
HRESULT result = acquire_license(hwnd, pExpiration);

2 个答案:

答案 0 :(得分:1)

您正在AcquireDeveloperLicense()的第二个参数中传递NULL指针。它希望您传入指向FILETIME结构的指针以接收许可证的到期日期。 FILETIME不是可选的。

请改为尝试:

typedef HRESULT (STDAPICALLTYPE *LPFN_AcquireDeveloperLicense)(
    _In_opt_ HWND     hwndParent,
    _Out_    FILETIME *pExpiration
    );

HINSTANCE hDll = LoadLibrary(TEXT("WSClient.dll"));
if (hDll)
{
    LPFN_AcquireDeveloperLicense acquire_license = (LPFN_AcquireDeveloperLicense) GetProcAddress(hDll, "AcquireDeveloperLicense");
    if (acquire_license)
    {
        FILETIME Expiration = {};
        HWND hwnd = GetConsoleWindow();
        HRESULT result = acquire_license(hwnd, &Expiration);
        ...
    }
    ...
    FreeLibrary(hDll);
}

答案 1 :(得分:0)

您需要提供指向您希望函数存储结果的变量的指针,而不是传递NULL。由于AcquireDeveloperLicense备注pExpiration不是可选的 out 参数,而且NULL不是有效值。

FILETIME expiration{};
HWND hwnd = GetConsoleWindow();
HRESULT result = acquire_license(hwnd, &expiration);