简体   繁体   中英

c++ loadlibrary - import dll

could you please tell me what is wrong with this c++ code? it always returns 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);

You are passing a NULL pointer in the 2nd parameter of AcquireDeveloperLicense() . It expects you to pass in a pointer to a FILETIME struct to receive the license's expiration date. The FILETIME is not optional.

Try this instead:

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

You need to provide a pointer to a variable in which you want the function to store the result, not pass NULL . As the AcquireDeveloperLicense notes pExpiration is an not optional out parameter and that meas that NULL is not a valid value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM