C++ write DLL files to call methods in easy languages

  • 2020-06-15 09:49:23
  • OfStack

I have been groping for two days, and finally solved all the problems. I would like to share with you here.

Three files are required, dll_demo.h, dll_demo.cpp, and dll_dome.def

Direct code:

The header file is as follows:


#ifndef _DLL_DEMO_H_
#define _DLL_DEMO_H_
#ifdef DLLDEMO_EXPORTS
#define DLL_DEMO extern "C" __declspec(dllexport) 
#else
#define DLL_DEMO extern "C" __declspec(dllimport)
#endif
DLL_DEMO int __stdcall Add(int a, int b);
#endif

The source file:


#define DLLDEMO_EXPORTS
#include "dll_demo.h"

int __stdcall Add(int a, int b)
{
  return (a + b);
}

def file:


LIBRARY
EXPORTS
Add @ 1

Functions need to be declared as stdcall to be called in an easy language. The main purpose of the def file is to solve the problem of the function name being changed after using stdcall. Introducing the def file in vs2013 requires manual additions to the project properties, linker, input, and module definition files.

Summary: the above is about C++ DLL easy to write the language to call all the methods and code, thank you for your reading and support for this site.


Related articles: