VC ++programming of the dynamic link library DLL typical example

Author:Anonymous    Updated:2008-2-26 22:29:41
DLL library DLL realized the sharing of the code reuse ideas. We can be extensive, are common, and can be used many times and the function definition in the database category. Thus, in the use of these functions, and again when the category, and no longer need to add these functions and the relevant category code. A common problem which generally? I summarized as follows:

(1) general algorithm

Image processing, audio and video decoding, compression and decompression, encryption and decryption of certain commonly used algorithms, and fixed these algorithms more often in such procedures often been used.

(2) pure resource DLL

We can DLL in access to resources, support for a multi-lingual applications, we can determine the language of the operating system, and automatically with the OS Apps corresponding language. This is a multi-language support applications, the general practice.

(3) Communication Control DLL

Serial, LAN communications control if the DLL function can be provided to many applications easily. In the industrial control, or even modem socket communication procedures, regular use of communications control DLL.

This section will be given the DLL three typical examples.

Algorithm DLL 7.1

We readers with a direct question as an example.

Songbaohua, Hello!

I see you on the series dev.yesky.com "VC + + DLL programming," feel very good. I used mainly by Delphi, and C / C + + learn, and the Win32 VCL more familiar with, but never contacted the VC + +, MFC very strange. This time the cooperation between the students and a computer simulation of optical imaging, used Fourier transform, there are routines hand VC + + is written. Our interface is developed with Delphi, which needs to be done by the Fourier transform of a DLL calls for Delphi. Troubled by the fact that non-MFC, tried many methods do not succeed, only a final compromise, the simple amend the procedures-into a parameter, as exe call, did not delay the follow-up process.

……

Thank you!

Of

Ceremony!

XX

Learning higher-level mathematics (probability and statistics and stochastic processes), signal and linear systems, and digital signal processing readers should be aware that the Fourier transform is a commonly used in signal analysis algorithms for the time domain and frequency domain conversion. FFT transform algorithm GM have in common, we appropriate to integrate it in a DLL.

Subsequently, the reader has provided such a function:

/ * Function: FFT ()
* Parameters:
* * TD complex <double> - at the time domain array of indicators
* * FD complex <double> - at the frequency domain array of indicators
* R-2 for power, that is, number of iterations
* Return value: None.
* Description: This function used to achieve fast Fourier transform
* /

Void FFT (complex <double> * TD, FD complex <double> *, int r)
(
LONG count; / / Fourier Transform points
Int i, j, k; / / variable cycle
Int bfsize, p; / / intermediate variables
Double angle; / / perspective
Complex <double> * W *, X1, X2 * * X;
Count = 1 <<r; / / Fourier Transform points

/ / Allocate memory for computing

W = new complex <double> [count / 2];
X1 = new complex <double> [count];
X2 = new complex <double> [count];

/ / Calculate weighted coefficient

For (i = 0; i <count / 2; i + +)
(
Angle = PI-i * 2 * / count;
W [i] = complex <double> (cos (angle), sin (angle));
)

/ / Point in time domain into X1

Memcpy (X1, TD, sizeof (complex <double>) * count);

/ / Use butterfly Fast Fourier Transform algorithm

For (k = 0; k <r, k + +)
(
For (j = 0; j <1 <<k; j + +)
(
Bfsize = 1 <<(r-k);
For (i = 0; i <bfsize / 2; i + +)
(
P = j * bfsize;
X2 [i + p] = X1 [i + p] + X1 [i + p + bfsize / 2];
X2 [i + p + bfsize / 2] = (X1 [i + p] - X1 [i + p + bfsize / 2]) * W [i * (1 <<k)];
)
)
X = X1;
X1 = X2;
X2 = X;
)

/ / Sort

For (j = 0; j <count; j + +)
(
P = 0;
For (i = 0; i <r; i + +)
(
If (j & (1 <<i))
(
P + = 1 <<(r-i-1);
)
)
FD [j] = X1 [p];
)

/ / Release memory

Delete W;
Delete X1;
Delete X2;
)

Since this has FFT function, we want to do it in the DLL, as a DLL interface is very simple, includes the following steps:

(1) the use of the establishment of a non-wizard MFC MFC DLL;

(2) in the project and add fft.h fft.cpp two documents;

Fft.h the source code for:

# Ifndef FFT_H
# Define FFT_H

# Include <complex>

Using namespace std;
Extern "C" void __declspec (dllexport) __stdcall FFT (complex <double> * TD, FD complex <double> *, int r);

# Define PI 3.1415926
# Endif

Fft.cpp the source code for:

/ * Filename: fft.cpp * /

# Include "fft.h"
Void __stdcall FFT (complex <double> * TD, FD complex <double> *, int r)
(
… / / Function code readers
)

In any programming language used in the Win32 API LoadLibrary can be loaded the DLL, and the use of GetProcAddress (hDll, "FFT") can function FFT address, mentioned by the readers of Delphi certainly no exception.

There are two points in the DLL needs attention:

(1) use extern "C" modified function declaration, otherwise, can only be generated DLL calls for C + +;

(2) __ stdcall modified function declaration and definition of __ stdcall is the Windows API function calls manner.


Net resource DLL 7.2

We have procedures in the application shown in Figure 18 of the resources (dialog),


Figure 18 Chinese dialog

In this application with the same work area, using MFC guide the establishment of two simple DLL, the application of engineering resources in the whole post-election and were copied to ChineseDll EngLishDll in EnglishDll engineering resources in the document search following statement:

////////////////////////////////////////////////// ///////////////////////////

/ / Chinese (P.R.C.) resources

# If! Defined (AFX_RESOURCE_DLL) | | defined (AFX_TARG_CHS)
# Ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
# Pragma code_page (936)
# Endif / / _WIN32

To read:

////////////////////////////////////////////////// ///////////////////////////
/ / English (U.S.) resources

# If! Defined (AFX_RESOURCE_DLL) | | defined (AFX_TARG_ENU)
# Ifdef _WIN32

LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

# Pragma code_page (1252)
# Endif / / _WIN32

And all of them Chinese translated into English. The DLL provides us with a dialog box as shown in Figure 19 of resources.


Figure 19 English dialog

Modify application works InitInstance () function,

CResourceDllCallDlg dlg;
M_pMainWnd = &dlg;
Int nResponse = dlg.DoModal ();

Before (ie, before the dialog), insert the following code:

/ / Get the language of the operating system

WORD wLangPID = PRIMARYLANGID (GetSystemDefaultLangID ());
If (LANG_CHINESE == wLangPID)
(
HLanguageDll = LoadLibrary ( "ChineseDll.dll"); / / load the Chinese resources
)
Else
(
HLanguageDll = LoadLibrary ( "EnglishDll.dll"); / / load the English resources
)

If (NULL == hLanguageDll)
(
AfxMessageBox ( "Load DLL failure");
Return FALSE;
)
AfxSetResourceHandle (hLanguageDll) / / set the current resources handle

Such applications will be of adaptive nature of the Chinese display Chinese OS resources, in the English showed in the English OS resources.


Communication Control DLL 7.3

We are here to give you an example of serial communication.

Maybe you need to understand that the serial communications background knowledge, in fact serial seen everywhere, such as PC-COM I shall serial communication port (serial). Figure 20, open the Windows Device Manager, we have seen COM mouth.

In the Windows operating system, to go through DCB (Device Control Block) on the serial port configuration. Using Windows API GetCommState function can access the serial port configuration; use SetCommState function can be set up serial communication parameters.

Serial communication is usually by the following four steps:

(1) Open Serial;

(2) serial port configuration;

(3) data transfer;

(4) closure of the serial.
Previous:Windows of multithreaded programming preliminary
Next:Implementation of DirectSound audio recording
User Reviews
Related Articles
Recommended article
AD