Home >Backend Development >C++ >How to Dynamically Load and Call a Function from a DLL in C ?
Question:
You have created a DLL with a function named funci() and are attempting to load and access this function from C . However, you encounter the error "'funci' was not declared in this scope." How can you use the DLL's pointer to access the function?
Answer:
Step 1: Load the DLL
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\path\to\test.dll");
This loads the DLL into the process's memory.
Step 2: Resolve Function Address
You need to get the function's address using GetProcAddress.
typedef int (__stdcall *f_funci)(); // Define function pointer f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "funci");
Step 3: Verify Function Address
Check if the function address was obtained successfully.
if (!funci) { std::cout <p><strong>Step 4: Call the Function</strong></p><p>Once you have the function pointer, you can call it.</p><pre class="brush:php;toolbar:false">std::cout <p><strong>Step 5: Freeing the DLL (Optional)</strong></p><p>Release the DLL handle using FreeLibrary() to unload the DLL from memory.</p><pre class="brush:php;toolbar:false">FreeLibrary(hGetProcIDDLL);
Additional Tips:
The above is the detailed content of How to Dynamically Load and Call a Function from a DLL in C ?. For more information, please follow other related articles on the PHP Chinese website!