Académique Documents
Professionnel Documents
Culture Documents
Abstract
This article shows a way to utilize Delphi codes in VC applications without using a DLL. That is, first write a DLL with Delphi, then use a toolDLL to Libto convert it into an equivalent static library, and finally call the static library from VC applications.
Introduction
There are many free codes and components in the Delphi world. How can we utilize them in VC? A usual scenario will be: First, make a DLL in Delphi that exports some functions. Then, we call these exported functions in VC. But in this way you have to bundle the Delphi DLLs with your VC application whenever it is to be released. And the performance of your application will degrade if it uses a lot of tiny DLLs. To avoid these inconveniences, you can use a tool, "DLL to Lib" (downloadable at http://www.binarysoft.com/dll2lib/d2l.exe), to convert the DLLs into equivalent static libraries, and use the static libraries in your VC application instead of the original DLLs.
7.
8. 9.
You will see a dialog box titled "Import Libraries Required" telling you that five import libraries, namely KERNEL32.LIB, GDI32.LIB, ADVAPI32.LIB, USER32.LIB, and OLEAUT32.LIB, are used by JpegLib.dll. You should add them to your project settings when using the corresponding static library JpegLibst.lib. Click the "Close" button to close the dialog. For detailed information, please refer to the "Add Import Libraries to Your project" topic in "DLL to Lib"'s help document. You will see a dialog box titled "Resource names or IDs used by dll" telling you about the resources used in JpegLib.dll. Save it to JpegLibres.txt for further reference and close the dialog. Analyzing the log list, we find a warning: Warning 10: Cannot find corresponding export symbol for LoadJPEG in the import libraries; we have to assume the export symbol follows __cdecl call convention. This warning means "DLL to Lib" cannot find the export symbol of the DLL in the import library (because you have not provided any import libraries), so "DLL to Lib" will use the default __cdecl call convention to process the export symbols. Remember we define LoadJPEG in the cdecl call convention, so there won't be any problems and we can just ignore this warning. If you want to know more information about call the convention, please refer to the "__cdecl call convention" topic in "DLL to Lib"'s help document.
4. #ifdef __cplusplus 5. extern "C" { 6. #endif 7. 8. #include <windows> 9. 10. HBITMAP __cdecl LoadJPEG(LPSTR AFileName); 11. 12. #ifdef __cplusplus 13. } 14. #endif
to a file named JpegLib.h. Note the original Pascal version API of LoadJPEG uses the cdecl call convention, so you must also use __cdecl in the C declaration. If you use other call conventions, an unexpected exception will be raised because of the incompatible parameter order and stack structure. Moreover, here we use
extern "C"
in the declaration to prevent the compiler from mangling the function name (which will lead to unresolved symbol errors when linked with the static library).
Now we can open the demo project JpegTest (jpeglib_demo.zip) to test the static library: 1. 2. 3. 4. Copy the static library JpegLibst.lib to JpegTest's working directory. Copy the header file JpegLib_supp.h to JpegTest's working directory. Remember it contains the declaration for JPEGLIB_DllMain. Copy the DLL's declaration header file JpegLib.h to JpegTest's working directory. Since the code in JpegLibst.lib may use the resources in JpegLib.dll, we must add these resources into JpegTest project. To do this, we: a. Open JpegLib.dll in Visual C++ as resources and save them to a JpegLib.rc file. Visual C++ will generate a new resource.h when saving JpegLib.rc, but it is of no use, so please delete it right now. b. Copy JpegLib.rc and corresponding external resource files to JpegTest's working directory. c. Open the JpegTest project in VC. d. In VC IDE, from the View menu, choose Resource Includes and add the line
e. #include "JpegLib.rc"
to the "Compile-time directives" box. Note: a message box will pop up after you modify the resource includes; just click "OK" to close it. Modify the resources IDs or names in the JpegTest project if they conflict with those in JpegLib.dll. (You can refer to JpegLibres.txt.) Modify JpegTest.cpp by a. Add the line: f.
5.
b. #include "JpegLib_supp.h"
c. Add an initialization statement to CJpegTestApp::InitInstance():
b. #include "JpegLib.h"
c. Add the following code to the click event handler for "Show" button:
d. void CJpegTestDlg::OnShowButton() e. { f. // TODO: Add your control notification g. // handler code here h. HBITMAP hBitmap; i. j. UpdateData(); k. hBitmap = LoadJPEG((LPSTR)(LPCSTR)m_strFile); l. if(hBitmap) ((CStatic *)GetDlgItem(IDC_JPEGIMAGE)) m. ->SetBitmap(hBitmap); n. }
7. 8.
Add JpegLibst.lib, kernel32.lib, gdi32.lib, advapi32.lib, user32.lib, and oleaut32.lib to the object module list of the project. Now we can build the JpegTest project by click F7. Wait.... Okay. The project is built successfully. Run JpegTest, select a JPEG filename, and click "Show it". Okay; the JPEG image is shown in our dialog. We have ported the Delphi code to VC application JpegTest successfully. And it can run without JpegLib.dll. The screenshot is shown in the header of this article.
o.
Downloads
Download demo project - 12.4 Kb Download source - 100 Kb