Dynamic Link Library - Implementation

xiaoxiao2021-03-06  52

Static link

Platform: Windows2000 / XP

Tools: VC 6.0

Create the steps:

1. New project: Win32 static library;

2 Select pre-compliled header;

3 Edit the header file and implement the file;

4 Compile, generate a lib file;

Reference Code:

//sum.h

#ifndef mysum

#define mysum

EXTERN "C"

{

Int sum (int N);

}

#ENDIF

//sum.cpp

#include "stdafx.h"

#include "sum.h"

Int sum (int N)

{

INT IRET = 0;

For (INT i = 1; i <= n; i )

{

IRET = I;

}

Return IRet;

}

Test steps:

1 Create a new Win32 Console project;

2 Add function call;

3 Add header files and lib files to the project;

4 compile, run;

Reference Code:

//main.cpp

#include "sum.h"

#include

int main ()

{

Printf ("% D", sum (n));

Return 0;

}

Win32 dynamic link

Create the steps:

1 New project: Win32 DLL;

2 Edit the header file and implement the file;

3 Compile, generate lib files and DLL files;

Reference Code:

//sum.h

#ifdef __cplusplus

EXTERN "C"

{

#ENDIF

#ifdef Sum_Exports

#define sum_api __declspec (dllexport)

#ELSE

#define sum_api __declspec (dllimport)

#ENDIF

SUM_API INT SUM (INT N);

#ifdef __cplusplus

}

#ENDIF

//sum.cpp

#define sum_exports

#include "sum.h"

Int sum (int N)

{

INT IRET = 0;

For (INT i = 1; i <= n; i )

{

IRET = I;

}

Return IRet;

}

Test steps:

1 Create a new Win32 Console project;

2 Add function call;

3 Add header files and lib files to the project;

4 put the DLL file in the same directory in the executable

5 compile, run;

Static load reference code:

//main.cpp

#include "sum.h"

#include "stdio.h"

Int main (void)

{

Printf ("% D", sum (100));

Return 0;

}

Dynamically loaded reference code:

//main.cpp

#include "sum.h"

#include "stdio.h"

#include

Typedef int (* pfunc) (int);

Int main (void)

{

Hinstance hlib = :: loadLibrary ("dll");

IF (HLIB! = NULL)

{

Pfunc Pfunc = (PFUNC) :: getProcaddress (HLIB, "SUM");

IF (PFUNC! = NULL)

{

Printf ("% D", PFUNC (100));

}

Else

PRINTF ("Function Not Found!");

}

Else

{

Printf ("Library Not Found!");

}

Return 0;

}

Guide the class and member functions from the DLL

Create the steps:

1 New project: Win32 DLL;

2 Edit the header file and implement the file;

3 Compile, generate lib files and DLL files;

//cfoo.h

#ifdef cdll_exports

#define cdll_api __declspec (dllexport)

#ELSE

#define cdll_api __declspec (dllimport)

#ENDIF

Class CDL1_API Cfoo {

PUBLIC:

Void Hello (Void);

}

//cfoo.cpp

#include "stdafx.h"

#include "cfoo.h"

#include

Void cfoo :: hello ()

{

Std :: cout << "Hello DLL!" << std :: endl;

}

Test steps:

1 Create a new Win32 Console project;

2 Add function call;

3 Add header files and lib files to the project;

4 put the DLL file in the same directory in the executable

5 compile, run;

//main.cpp

#include "cfoo.h"

Int main (void)

{

Cfoo F;

f.hello ();

Return 0;

}

Extend DLL using MFC

Suppose you want to export a dialog, you want to modify the code:

#ifndef _afxext

#define IDd_ext_name 129

#ELSE

#include "resource.h"

#ENDIF

Class AFX_EXT_CLASS CNAMEDLG: Public CDIALOG

{...}

转载请注明原文地址:https://www.9cbs.com/read-116932.html

New Post(0)