top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How DLL (Dynamic library) is linked during run time?

+2 votes
456 views

For an example:
I have a dll file liba.dll,
While compiling i am giving the path of the library file. It is compiling properly.

Now i have to load this binary/.exe file and DLL file to other system/device,
Then what about the DLL file?
How our program gets to know the path of DLL during run time?
I mean how DLL file will be linked?

posted Aug 26, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Is it a Linux case or Windows, in Linux we don't have dll we have .so, please clarify
In Linux....
Do i have to place .so file on same path
or
Is it compulsary that i have to pass .so file as command line argument?
Ex: ./a.out libc.so
Not necessary, it can be anywhere pointed out by the LD_LIBRARY_PATH.

Also no need to pass the .so in the command line.
Then how our exe/binary will get to know the path of .so file?

1 Answer

0 votes

GetModuleFileName() works fine from inside the DLL's codes. Just be sure NOT to set the first parameter to NULL, as that will get the filename of the calling process. You need to specify the DLL's actual module instance instead. You get that as an input parameter in the DLL's DllEntryPoint() function, just save it to a variable somewhere for later use when needed.

A complete example:

CStringW thisDllDirPath()
{
    CStringW thisPath = L"";
    WCHAR path[MAX_PATH];
    HMODULE hm;
    if( GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 
                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                            (LPWSTR) &thisDllDirPath, &hm ) )
    {
        GetModuleFileNameW( hm, path, sizeof(path) );
        PathRemoveFileSpecW( path );
        thisPath = CStringW( path );
        if( !thisPath.IsEmpty() && 
            thisPath.GetAt( thisPath.GetLength()-1 ) != '\\' ) 
            thisPath += L"\\";
    }
    else if( _DEBUG ) std::wcout << L"GetModuleHandle Error: " << GetLastError() << std::endl;

    if( _DEBUG ) std::wcout << L"thisDllDirPath: [" << CStringW::PCXSTR( thisPath ) << L"]" << std::endl;       
    return thisPath;
}
answer Aug 27, 2015 by Amit Kumar Pandey
Similar Questions
+2 votes

Let's say I have an exe-file (for example computer game) and need to forbid to run it until certain date or time during the day. Any 'manipulations' with file are allowed.

Could you, please, offer me a simple way of how to encode/decode such a file mostly in C or C++?

+2 votes

Below instruction will start CPU stress with maximum of 100%,

while(1);

Below instructions will start CPU stress with near to 0%.

while(1)
     sleep(1);

Is there any way to control this? I mean what if i want to start CPU stress of >100% and what if want to start CPU stress with particular number?

Can any one help in programming that? I am looking for C/C++/Python solution on Linux...

+4 votes

Lets assume I have a system with RAM of 1GB. and virtual memory is 500MB. That brings to 1.5GB i.e. 1500 MBytes.

I have read somewhere that when I process is created stack of 8MB is associated to that process. So, assuming that any of the process is not allocating any dynamic memory or anything, then does it mean that, Maximum number of process that i can create is 1500/8 and i.e. 187 Process.

Please clarify my understanding,

+1 vote

Please assume that I don't want to create library file

Lets say,
I have 2 files main.cpp and function.cpp. I have 2 methods to compile both the file?

1st) while compiling just include both files,

   g++ main.cpp function.cpp

2nd) In main.cpp file i can include function.cpp

#include "function.cpp"

As far as I know while compiling using 2nd method it will take more time, other than that any advantage/disadvantage?
Also what if i have more files lets say i have 500+ files, all are linked with each other, then which method will be preferable?
In this case if I go with 1st method then I will have to provide 500+ file names in my makefile?

Can anybody help?

...