Search code examples
c++loadlibrary

C++ - LoadLibrary returns 126 error for self-made DLL


I'm playing in custom project and trying to create a DLL in other project and link in runtime using LoadLibrary function, but it returns 126 error. In googled a lot, but in the most cases people debugged programs of others. But my DLL contains function string sayHello() { return "Hello"; }. Which project options should be applied to let me successfully load this dll? And call the function? Thanks

UDP:

vector<BYTE> decodedDll = base64_decode(base64dll);
string filename = string("lib.dll");
ofstream outfile(filename, ios::out | ios::binary);
outfile.write((const char*) &decodedDll[0], decodedDll.size());
outfile.close();
if (!fileExists(filename))
{
    cout << "DLL file not found " << endl;
    getchar();
    return;
}
HINSTANCE hGetProcIDDLL = LoadLibrary((LPCWSTR) filename.c_str());
int loadLibraryError = GetLastError();
if (hGetProcIDDLL == NULL)
{
    string error = "Could not load DLL\n";
    send(s, error.c_str(), error.length(), 0);
    finishSocketWork(s, address);
    cout << "Unable to load LIB, error code " << loadLibraryError << endl;
    getchar();
    return;
}

(it's a university work, I'm required to receive DLL from client, call specified function and return result). And .dll transfer works good -- files are equal


Solution

  • LoadLibrary((LPCWSTR)filename.c_str());
    

    This is a common error, trying to hide compiler error by using (LPCWSTR) cast. The cast is meaningless here. You mignt as well try to cast from integer to string.

    Use the ANSI version LoadLibraryA for ANSI parameter:

    HINSTANCE hGetProcIDDLL = LoadLibraryA(filename.c_str());
    

    Or use wide string functions std::wstring