The following is how c creates and calls a DLL:
DLLTestdll. c
[Copy to clipboard]
Code:
# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>
# If building_dll
# Define dllimport _ declspec (dllexport)
# Else/* Not building_dll */
# Define dllimport _ declspec (dllimport)
# Endif/* Not building_dll */
/* DefinesDLLThe addxy function returns the sum of 2 */
Dllimport int addxy (int x, int y)
{
Return (x + y );
}
Bool apientry dllmain (hinstance hinst/* library instance handle .*/,
DWORD reason/* reason this function is being called .*/,
Lpvoid Reserved/* not used .*/)
{
Switch (reason)
{
Case dll_process_attach:/* What is executed when the DLL process is loaded */
Break;
Case dll_process_detach:/* What is executed when the DLL process is detached */
Break;
Case dll_thread_attach:/* What is executed when the DLL thread loads */
Break;
Case dll_thread_detach:/* What is executed when the DLL thread is detached */
Break;
}
/* Returns true on success, false on failure */
Return true;
}
Compile with GCC-for-win 3.3.1 to obtain a testdll. dll
Then compile rundll. C, source code
[Copy to clipboard]
Code:
# Include <stdio. h>
# Include <windows. h>
Typedef int (* lpaddfun) (INT, INT); // macro-defined function pointer type
Int main (INT argc, char * argv [])
{
Hinstance hdll; // DLL handle
Lpaddfun addfun; // function pointer
Int result;
Hdll = loadlibrary ("testdll. dll");/* load testdll. dll */
If (hdll! = NULL)
{
Addfun = (lpaddfun) getprocaddress (hdll, "addxy ");
If (addfun! = NULL)
{
Result = addfun (654,212);/* 654 + 212 */
Printf ("% d", result );
}
Freelibrary (hdll );
}
Return 0;
}
Compile and obtain rundll.exe
Put testdll in the same working directory as rundll.exe, or where $ path can be reached
Then run rundll.exe. You can see that rundll.exe loads testdll. dll and calls the addxy function in it,
Quote: D:/document/studio/C/use_c_dll> rundll
866
D:/document/studio/C/use_c_dll> rundll C ++
Write to generate testdll. dll in two ways:
[Copy to clipboard]
Code:
# Include <iostream>
Using namespace STD;
Class dllclass {
Public:
// Exported member function
_ Declspec (dllexport) void functiona (void)
{
Cout <"in function a of the exported function" <Endl;
Return;
}
};
// Exported class
Class _ declspec (dllexport) exportdllclass {
Public:
Void functionb (void)
{
Cout <"in function B of the exported class" <Endl;
Return;
}
};
// Exported instance of the dllclass
_ Declspec (dllexport) dllclass test;
Save it as testdll. cpp and compile it. Because the code is written using editplus, you must manually compile it. ^
I am using vs 2005's clcompiler interface cl.exe. Under cmd: CL/C testdll. cpp
In this case, testdll. obj is generated and then linked to link testdll. OBJ/DLL.
The testdll. dll, testdll. exp, and testdll. Lib files are generated.
The work is coming to an end section ....
Start and write the calldll.exe file used to call testdll.dll:
[Copy to clipboard]
Code:
Class dllclass {
Public:
// Imported member function
_ Declspec (dllimport) void functiona (void );
};
// Imported class
Class _ declspec (dllimport) exportdllclass {
Public:
Void functionb (void );
};
// Imported instance of the dllclass
_ Declspec (dllimport) dllclass test;
Int main (void)
{
Exportdllclass testclass;
Test. functiona ();
Testclass. functionb ();
Return 0;
}
Save it as the calldll. cpp file to start compilation.
This one-step deployment: CL calldll. cpp testdll. Lib
Now you can run calldll.exe directly ....