0

In my current project I need some functions exported from ntdll.dll and csrsrv.dll.

There is no problem with getting handle for ntdll and pointer to functions. But when I try get handle for csrsrv.dll function fails with error code "File not found". I've tried to specify full path to file, but it dose not change a thing.

Code for my load function from dll function:

PVOID GetFunctionFromDll(const std::string& _sModuleName,const std::string& _sFnName)
{
    HMODULE hModule = NULL;
    PVOID ptrFn = NULL;

    if(!GetModuleHandleEx(0,_sModuleName.c_str(),&hModule))
    {
        return 0;
    }

    ptrFn = GetProcAddress(hModule, _sFnName.c_str());

    FreeLibrary(hModule); // preventing handle leakage

    return ptrFn;
}

Any ideas why does it fail with csrsrv.dll?

4

1 回答 1

1

GetModuleHandleEx()不加载 DLL。从链接的参考页面:

检索指定模块的模块句柄并增加模块的引用计数,除非指定了 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT。该模块必须已由调用进程加载。

csrsrv.dll调用时 必须不在内存中,并且将ntdll.dll在内存中,并且调用成功。

建议LoadLibrary()在函数外使用,以确保DLL在函数调用后仍保留在内存中,保证返回的地址GetFunctionFromDll()保持有效。

于 2013-09-16T12:18:57.677 回答