Statement: I just want to record my learning experience and may be able to help others. If they are similar, please forgive me.
DLL that can be implemented in Visual C ++ 6.0
Visual c ++ 6.0 supports automatic generation of Win32 DLL and MFC Appwizard DLL. Win32 DLL does not use the MFC class library, and the exported function is a standard C interface, non-MFC and MFC applicationsProgramAnd has a wider application scope. So the following describes the development of Win32 DLL.
Create Win32 DLL for the export Function
1. Start visual c ++ 6.0 and use Appwizard to create a "Win32 dynamic-Link Library" project named sayhello. Use the default settings to create an empty project of Win32 DLL.
2. Add the header file sayhello. h and the source file sayhello. cpp for the DLL project. In the header file sayhello. H, declare the DLL export functions say and sum to display "Hello, world! And sum. StatementCodeAs follows:
//Sayhello. h
//
/*
The modifier extern "C" is used by the C ++ compiler to process this function in C language for other languages.
*/
Extern " C " Void _ Declspec (dllexport) Say ( Char * Szwords, Int Nlen ); // Declare the say export Function
Extern " C " Float _ Declspec (dllexport) sum ( Float Fnum1, Float Fnum2 ); // Declare the sum export Function
Add the implementation code of the function say and sum in the source file sayhello. cpp. The Code is as follows:
//Sayhello. cpp
//
# Include<String. H>
# Include"Sayhello. h"
void say ( char * szwords, int nlen)
{< br> strcpy (szwords, " Hello, world! " );
strcat (szwords, " \ 0 " );
}
FloatSum (FloatFnum1,FloatFnum2)
{
ReturnFnum1+Fnum2;
}
3. [F7] key to compile and generate the DLL. In this case, the actual code file sayhello. dll and the imported and exported file sayhello. Lib are generated in the debug folder of the project.
4. There are two methods to export a function from a DLL. One is to use the _ declspec (dllexport) keyword, such as sayhello. h. def file (it is worth noting that the added file type is a text file, and the name should be entered sayhello. def), the Code is as follows:
; Sayhello. Def
;
Library"Sayhello"
Description"Export functions in DLL"
Exports
Say @1
Sum @2
5. dll loading is divided into static loading and dynamic loading. Dynamic Loading (Dynamic Link during runtime, also called display link) DLL is implemented through the loadlibrary, getprocaddress, and freelibrary API functions. The call is as follows:
Typedef Void ( * Say )( Char * , Int );
Say;
Typedef Float ( * Sum )( Float , Float );
Sum;
Hinstance hdll;
Hdll = Loadlibrary ( " .. \... \ Sayhello \ debug \ sayhello. dll " );
If (Hdll ! = Null)
{
// The getprocaddress function obtains the DLL export function address.
Say = (Say) getprocaddress (hdll, " Say " );
Sum = (SUM) getprocaddress (hdll, " Sum " );
}
Else
{
Afxmessagebox ( " Unable to load DLL! " );
Return ;
}
Updatedata (true );
Const Int Len = 20 ;
Char P [Len];
Say (p, Len );
M_strdisphello.format ( " % S " , P );
M_fresult = Sum (m_fnum1, m_fnum2 );
Updatedata (false );
Freelibrary (hdll );
Static Loading (Dynamic Link during loading, also known as implicit link) DLL is the completion of DLL loading by the compilation system and the uninstallation of DLL at the end of the application, the DLL reference library file (. lib. The call is as follows:
# Pragma comment (Lib, " Sayhello. Lib " )
Extern " C " _ Declspec (dllimport) Void Say ( Char * Szwords, Int Nlen );
Extern " C " _ Declspec (dllimport) Float Sum ( Float Fnum1, Float Fnum2 );
Now we use the say and sum functions.