Wednesday, April 28, 2004

Simple steps to generate and use DLL

The advantage of DLL is that we can just send them a DLL file, a Lib file a header file (without .cpp file). The following is Implicit Linkage
Generate DLL: New... MFC AppWizard(dll)...(using shared MFC DLL)...Change .cpp and .h a little bit -- extern "C" __declspec(dllexport) void UKCalibration(...) for both...Add #include "stdafx.h" to .cpp ...VC++Project...Add to Project...Files...UKCalibration.cpp UKCalibration.h ...Compile and generate xxx.dll and xxx.lib ...Copy xxx.dll to \windows\system32

Use DLL: Copy UKCalibration.h to current directory...Add #include "UKCalibration.h" to .cpp ... Project->setting->Link->object/library moduels (\Data\Examples\bb\UKCal\Debug\xxx.lib), ->setting for (all configurations) , ->gerenal (Use MFC in ShareDLL)
My programs in hard disk: C:\Data\Examples\bb\UKCal (generate DLL), C:\Data\Examples\bb\testdll (use DLL with MFC single dialog), C:\Data\Work\Mon_LUT_UL\test (use DlL in C), C:\Data\Work\Mon_LUT_UL\UKCalibration (original UKCalibration.cpp UKCalibration.h files)

For Explicit Linkage, only two file (.dll and .h) are needed.
The generate part is the same. Use DLL: the following codes need to be inserted in the main program
typedef void (SQRTPROC)(float *, float *, char *, float *, float *, int , int );
HINSTANCE hInstance;
SQRTPROC *pFunction;
hInstance=LoadLibrary("\\Data\\Examples\\bb\\UKCal\\Debug\\UKCal.DLL");
if(hInstance==NULL)
{
AfxMessageBox("Cannot load DLL file!");
}
VERIFY(pFunction = (SQRTPROC* )::GetProcAddress(hInstance, "UKCalibration"));
(*pFunction)(Xp, Yp, filename, xp, yp, p_width, p_height);


In head file: extern "C" __declspec(dllexport) void UKCalibration( float *Xp, float *Yp, char filename[512],...);
in .cpp file: #include "stdafx.h"
extern "C" __declspec(dllexport) void UKCalibration(float *Xp, float *Yp, char filename[512]...){... ...}


Reference: Inside Visual C++ 4th page 509
Engineering Picnic today.

No comments:

Post a Comment