Method for generating and using DLL in DEV-CPP

xiaoxiao2021-03-06  92

First, create a new project, choose DLL, here we use C language

DEV-CPP will automatically generate two files: filename.h, filename.c

FILENAME.C

==============================================

/ * In the next row, you can replace DLL.h to your DLL header file name * / # incrude "dll.h" #include #include #include

// If the add function is added here

Dllimport void HelloWorld () {MessageBox (0, "Hello World from DLL! / N", "Hi", MB_ICONInInformation;

BOOL APIENTRY DllMain (. HINSTANCE hInst / * Library instance handle * /, DWORD reason / * Reason this function is being called * /, LPVOID reserved / * Not used * /..) {Switch (reason) {case DLL_PROCESS_ATTACH: break;

Case DLL_PROCESS_DETACH: BREAK;

Case DLL_THREAD_ATTACH: BREAK;

Case DLL_THREAD_DETACH: BREAK;

/ * Returns True on success, false on fiveure * / return true;

FILENAME.H

==============================================

#ifndef _dll_h_ # define _dll_h_

#if building_dll # define dllimport __declspec (dllexport) #ELSE / * NOT building_dll * / # define dllimport __declspec (dllimport) #ENDIF / * NOT building_dll * /

// If you add a function declaration to add a DLLIMPORT VOID HELLOWORLD (VOID);

#ENDIF / * _DLL_H_ * /

Then compile the generation of DLL files

Second, use the DLL file

You can create a new .C file or select the console program in the new project.

code show as below:

#include #include

// Windows Head file # include "windows.h" int main () {hmodule hinst; void (* caver);

Hinst = loadingLibrary ("dllname.dll");

(FarProc *) (CallDLL) = getProcaddress (HinSt, "HelloWorld");

Calldll (); freeelibrary (hinst); Return 0;

If there is no mistake, compile, run.

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

New Post(0)