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 << "Could not locate the function" << std::endl; return EXIT_FAILURE; }
Step 4: Call the Function
Once you have the function pointer, you can call it.
std::cout << "funci() returned " << funci() << std::endl;
Step 5: Freeing the DLL (Optional)
Release the DLL handle using FreeLibrary() to unload the DLL from memory.
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!