I believe it is not necessary to introduce the role of DLL. Below we will provide an example of DLL for C and D.
Exp: C version
File where the entry function is located
/*
BenProgramTest the use of dynamic link library files
*/
# Include <windows. h>
# Include <tchar. h>
# Include " Use_dll_dlltest.h "
Lresult callback wndproc (hwnd, uint, wparam, lparam );
Int Winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline,Int Nshowcmd)
{
Static Tchar szappname [] = text ( " Strprog " );
Hwnd;
MSG;
Wndclass;
Wndclass. cbclsextra = 0 ;
Wndclass. cbwndextra = 0 ;
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hinstance = hinstance;
Wndclass. lpfnwndproc = wndproc;
Wndclass. lpszclassname = szappname;
Wndclass. lpszmenuname = NULL;
Wndclass. Style = cs_hredraw | cs_vredraw;
If (! Registerclass (& wndclass ))
{
MessageBox (null, _ T ( " You need Windows NT to run this program " ), _ T ( " Warring " ), Mb_ OK );
Return 0 ;
}
Hwnd = createwindow (szappname,
Text ( " DLL demo " ),
Ws_overlappedwindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Null,
Null,
Hinstance,
Null
);
Showwindow (hwnd, nshowcmd );
Updatewindow (hwnd );
While (Getmessage (& MSG, hwnd, 0 , 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return MSG. wparam;
}
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
HDC;
Paintstruct pS;
Rect;
Switch (Message)
{
Case Wm_paint:
HDC = beginpaint (hwnd, & PS );
Getclientrect (hwnd, & rect );
Edrcentertext (HDC, & rect, text ( " This string was displayed by a DLL " ));
Endpaint (hwnd, & PS );
Return 0 ;
Case Wm_destroy:
Case Wm_close:
Postquitmessage ( 0 );
Return 0 ;
}
Return Defwindowproc (hwnd, message, wparam, lparam );
}
Header file: dlltest. h
/*
Dlltest. h header file
*/
# Ifdef _ cplusplus
# DefineExport extern "C" _ declspec (dllexport)
# Else
# DefineExport _ declspec (dllexport)
# Endif
Export bool callback edrcentertexta (HDC, prect, pcstr );
Export bool callback edrcentertextw (HDC, prect, pcwstr );
# Ifdef Unicode
# DefineEdrcentertext edrcentertextw
# Else
# DefineEdrcentertext edrcentertexta
# Endif
Below is the c file for creating the DLL file
# Include <windows. h>
# Include " Dll_test_dlltest.h "
Int Winapi dllmain (hinstance, DWORD fdwreason, pvoid pvreserved)
{
Return True;
}
Export bool callback edrcentertexta (HDC, prect PRC, pcstr pstring)
{
Int Ilength;
Size size;
Ilength = lstrlena (pstring );
Gettextextentpoint32a (HDC, pstring, ilength, & size );
Return Textouta (HDC, (PrC-> right-PRC-> left-size.cx )/ 2 , (PrC-> bottom-PRC-> top-size. Cy )/ 2 , Pstring, ilength );
}
Export bool callback edrcentertextw (HDC, prect PRC, pcwstr pstring)
{
Int Ilength;
Size size;
Ilength = lstrlenw (pstring );
Gettextextentpoint32w (HDC, pstring, ilength, & size );
Return Textoutw (HDC, (PrC-> right-PRC-> left-size.cx )/ 2 , (PrC-> bottom-PRC-> top-size. Cy )/ 2 , Pstring, ilength );
}
The following are the header files required to create a DLL file:
/*
Dlltest. h header file
*/
# Ifdef _ cplusplus
# DefineExport extern "C" _ declspec (dllexport)
# Else
# DefineExport _ declspec (dllexport)
# Endif
Export bool callback edrcentertexta (HDC, prect, pcstr );
Export bool callback edrcentertextw (HDC, prect, pcwstr );
# Ifdef Unicode
# DefineEdrcentertext edrcentertextw
# Else
# DefineEdrcentertext edrcentertexta
# Endif
When using VC for testing, you must specify the reference relationship between the two projects.
Exp: Version d
The following is the main program used for testing.
Program Project1;
{ $ Apptype Console }
Uses
Sysutils,
Unit1 In ' Unit1.pas ' ;
VaR
Int1: integer;
Int2: integer;
Begin
Writeln ( ' Enter two integers: ' );
Readln (int1, int2 );
Writeln ( ' Int1 = ' , Int1, ' Int2 = ' , Int2 );
Swap_int (int1, int2 );
Writeln ( ' Int1 = ' , Int1,' Int2 = ' , Int2 );
Readln;
End .
Unit files used:
UnitUnit1;
Interface
ProcedureSwap_int (VaRInt1, int2: integer );
Implementation
ProcedureSwap_int (VaRInt1, int2: integer );External'Swap. dll'
End.
Create a library file for the DLL file project:
Library Swap;
{ Important Note about dll Memory Management: sharemem must be
First unit in your library's uses clause and your project's (select
Project-View Source) uses clause if your DLL exports any procedures or
Functions that pass strings as parameters or function results. This
Applies to all strings passed to and from your DLL -- even those that
Are nested in records and classes. sharemem is the interface unit
The borlndmm. dll Shared Memory Manager, which must be deployed along
With your DLL. To avoid using borlndmm. dll, pass string Information
Using pchar or parameter string parameters. }
Uses
Sysutils,
Classes;
{ $ R *. Res }
Procedure Swap_int ( VaR Int1, int2: integer );
VaR
Temp: integer;
Begin
Temp: = int1;
Int1: = int2;
Int2: = temp;
End ;
Exports
Swap_int;
End .
The dependency between two projects is not required for the DLL Implementation of version D.
At the same time, we can find that D's rad technology is indeed easier than C's rad technology, and if Win32 API is also used, the performance and
There is basically no difference in the performance of the C version. The C version has the MFC framework (C ++ here), and the D version has the VCL framework. after encapsulation, both of them are huge. For ease of use
Development has obvious advantages. For syntax, C is simpler than D, but C ++ is comparable to D.