Basically, if the VCL software component does not talk, write and use the DLL in C Builder is consistent with the traditional Windows SDK, but such a C Builder will lose its proud The advantage is. So in this chapter I will introduce you how to write a DLL that uses VCL components, but also a comprehensive discussion on matters that should be paid attention to in DLL use between Visual C , VBs.
Write dynamic chain knots (DLL) with C Builder
Figure 1 About Dialog written in C Builder
What is shown in Figure is what I have to write is About Dialog that is combined with VCL components. It seems that it is not a commercial software?
C Builder has a dense design in the visual design in the contextual design. However, in a realistic working environment, maybe in your hand is not written by C Builder, but use other programs such as Visual C , VB or Borland C , if you want to rewrite the original program, not only When you waste, and maybe the boss is not allowed, then what should I do? By the way, it is to use the Way to write a DLL to reach the purpose of the program. In order to allow the traditional Windows SDK programmer to enjoy this advantage, you can complete the part of the visual design part with DLL, and then provide External Chinese is called for others, so you can take into account the two, "to hold both ends, used there", and solve problems smoothly.
All right! Not much nonsense, now start to enter the topic!
Establish a DLL project
Establish a DLL task and general application. Similarly, you can build a new item by [file / new] and select a DLL type.
As shown in Figure 2:
Figure 2 Select DLL Project Type
After you have selected the project type, it automatically generates a relevant file. Unlike the application, it only produces a Project file without including a table file, and the file is just an empty shell containing the DLL enterpoint, the program is approximately as follows:
Int WinAPI DLLENTRYPOINT (Hinstance Hinst, Unsigned Long Reason, Void *)
{
Return 1;
}
DLLENTRYPOINT is a DLL-constant program entry point because there is no processing in this system, so it is directly Return 1.
Join TFORM form
In order to write about Dialog as shown, there is no doubt that we have to join a TForm table because it is not automatically generated when establishing a DLL project, so you have to join. At this point you can [File / New Form] to join a table. Let us use the same way as the general application to add the necessary soft components, as shown in the figure.
Figure TForm in Design Time.
You can see that I use a TPANEL component in the program (except for the marked, there is another platform for placing all components). And a TIMAGE element, the picture of the PAANEL component is different, that is to modify its Bevelinner, Bevelouter, BevelWidth, you can try to modify it, see if you can make a better effect . As for TIMAGE, it is used as a component that displays the Athena graphic. After the position of all components is arranged, we set the onclick event handle of all components, let it turn off the conversation window when the user presses the mouse. This event handles a function of simple, only a short line.
Void __fastcall tform1 :: image1click (TOBJECT * SENDER)
{
CLOSE ();
}
Ok, so I have completed the program that joined the table.
Write an output function (Export Function)
After the design of the completed table, we will write the output function, which can display this form using the call to the form. Our output correspondence is as follows:
Extern "C" void _stdcall showimage (void);
Where Extern "C" is used to tell the compiler, name the C named method, because the C namefind will add the parameter type or other decorative word after the letter name, this will result Other programs such as VC , VB, etc. can't use it. Also __stdcall is used to indicate the parameter incoming method it uses. We will introduce more in-depth in the following two.
Let's come to see the freewheel itself, this card is very simple, just use the new dynamic to generate a table, then use ShowModal to display the table, ShowModal will wait until the user turns off the table after pressing Click, then we will use Delete The instruction is released to release the memory.
Void_export _stdcall showimage (void)
{
Form1 = new TFORM1 (NULL);
FORM1-> ShowModal ();
DELETE FORM1;
}
After completing the above, you can compile the program. At this point C Builder generates a DLL file, which will generate a Dllsamp.dll file in this peripheral, and this is a dynamic chain junction library for external calls.
Use DLL in C Builder
Let me tell you how to use dynamic chain junction libraries. We take the DLL produced in front as an example. There are two ways to use the DLL, respectively, for clear calls and not clear calls.
I will first explain the way you don't clearly call. Uncleary call refers to, in the program, there is no line in the program to load the DLL, but use the link to enter a letter library file (lib) that records the input function, so that the system will automatically The DLL is loaded, and it is released after the use is completed, and it is not necessary to use the user (that is, the call is a function) to load and release the action.
First, a lib file must be generated, you can use implib.exe in the C Builder program to generate this file, do not use Visual C Implib.exe, because Microsoft is used by the format of the Coff format, The format used by Borland is the LIB file in the OMF format. (Similarly, if your LIB file is to give Visual C link, then use it attached to Implib.exe, not in use). Therefore, we can produce a Dllsamp.lib file with the following instructions. Implib Dllsamp.lib Dllsamp.dll
So you can get a DLLSamp.lib file with a row.
Then let's write the example of using the DLL. This program is quite simple, I only put a button in the table, then write the Button's OnClick event handle, make it call the showImage function.
One thing to note is that you must add the previous DLLSAMP.LIB to this item, use the [Project / Add to Project] to select the lib type file, you can add it.
Finally, we can link the program, the following is the result of its execution.
Figure 4 execution result.
Void dllname01 (void) @ dllname01 $ qv @ dllname01 $ qv because it is a CPP program void _stdcall dllname02 (void) @ dllname02 $ qsv so the letter name is revoid _cDecl dllname03 (void) @ dllname03 $ qv decorated. Void _Pascal Dllname04 (Void) @ dllname04 $ qv void _fastcall dllname05 (void) @ dllname05 $ qqrv
The above results make you twilight two gold steel, you can't touch your hair. This is because our program name is expressed in CPP, C Builder will name the C unique naming method, this name mode will add the nature of the use parameters after the letter name, such as Parameter category, etc. This has a special name in C , called Mangled Name, which is a naming rule issued to make multiple load functions. (Note: Add (int) and add (double) in C can exist at the same time, so you must distinguish between Object Code). At the same time, this naming method is different because of the various compiler manufacturers, so avoid use when writing DLLs. In order to avoid these problems, we have to change the way the following declaration: #define _DLLNAME01_H_ #ifndef DLLNAME #define EXTERN __declspec (dllimport) #else #define EXTERN __declspec (dllexport) #endif extern "C" {EXTERN void DllName011 (void) ; eXTERN void _stdcall DllName022 (void); eXTERN void _cdecl DllName033 (void); eXTERN void _pascal DllName044 (void); eXTERN void _fastcall DllName055 (void);}; #endif wherein extern "C" {} Pinbing;. with To tell the compiler to use C naming, do not use C mangled name. If there is only one version, you can declare the following: Extern "C" void __stdcall showimage (); now we can view the letter name after removal of mangled name: Version DLL Summary Description void DllName01 (void) _DllName01 name is underlined void _stdcall DllName02 (void) DllName02 name changed void _cdecl DllName03 (void) _DllName03 name is underlined void _pascal DllName04 (void) DLLNAME04 name in uppercase void _fastcall DllName05 (void) @ DllName05 name with @ We can know above that the name when using the _cDecl modifies when not adding the word. _Pascal modified words generated by the word name and the 16-bit standard DLL letter name (which is not accepted in VC ), __ fastcall's word name plus @. Among them, the most used in Win32 is _stdcall modifies, this is also the modified word you want to write when you can use it together with other languages, followed by __cdecl modifier, which is used to transmit non-parameter type Letters such as Printf, Sprintf, etc. are used. The other two have no chance to use in the DLL. Conclusion: By it is known that you must pay attention to the following items when writing a DLL in C Builder: Use the __Declspec (dllimport) and the standard type of __Declspec (dllexport).
Note C 's Mangled Name. Pay attention to the use of modified words. The __stdcall modifier word must be used unless the use of the uncertain parameters is used. (4) Do not confuse the use of __Declspec and __stdcall. This two has no absolute correlation. Even the program may be planted here, remember, remember! How, after reading the above introduction, is there a feeling of swaying aware. After understanding the above rules, you should not fall you when you write or use the DLL, you should not fall! Finally, we will declare the standard DLL way out in order to deepen your impression: #ifndef _SHOWIMG_H_ #define _SHOWIMG_H_ #ifndef IMGDLL #define EXTERN __declspec (dllimport) #else #define EXTERN __declspec (dllexport) #endif extern "C" Extern void __stdcall showimage (void); #ENDIF language Double male 'C Builder and Visual C link We have already paid attention to C Builder Write DLL, now let's talk about another focus - C Builder and Visual C links. If you don't use Visual C , you can ignore this part. If you need to use the Visual C DLL or you must provide DLL to VC or VB, you may bring you unexpected gain. VC uses C Builder's DLL in Visual C to use C Builder's DLL in Visual C , using Datong Small in C Builder, only a few things must be noted. (1) Visual C LIB file format and C Builder's lib format, so you have to re-generate a lib. However, unfortunately, the version of VC did not provide an Implib.exe in the 32-bit version (this has not been made to many people, so you can't make it easy to generate lib files. Solutions There are two: one of them writes an empty DLL function in VC , which makes it generated by LIB files, which is the use of LoadLibrary, GetProcAddress, clear call mode. (2) Use the standard readings mentioned earlier. C Builder uses VC DLL Verstries to use VC DLL in C Builder to note the special naming rules used in Visual C when C Builder. In addition to the few items mentioned earlier in VC , it also uses a special parameter nomenclature. In short, it is the size of the parameter after the function name, this naming method will cause C The troubles of Builder, VB, Delphi use.