#include <string.h>#include <windows.h>
using namespace std;
#define DLL extern "C" __declspec(dllexport)//I defined DLL for dllexport functionDLL main (){MessageBox(NULL,"Hi from DLL","DLL",MB_OK);}
EXE
#include <string.h>#include <windows.h>
using namespace std;
typedef LPVOID (WINAPI*Function)();//make a placeholder for function from dllFunction mainDLLFunc;//make a variable for function placeholder
int main(){char winDir[MAX_PATH];//will hold path of above dllGetCurrentDirectory(sizeof(winDir),winDir);//dll is in same dir as exestrcat(winDir,"\\exmple.dll");//concentrate dll name with pathHINSTANCE DLL = LoadLibrary(winDir);//load example dllif(DLL==NULL){FreeLibrary((HMODULE)DLL);//if load fails exitreturn 0;}mainDLLFunc=(Function)GetProcAddress((HMODULE)DLL, "main");//defined variable is used to assign a function from dll//GetProcAddress is used to locate function with pre defined extern name "DLL"//and matcing function nameif(mainDLLFunc==NULL){FreeLibrary((HMODULE)DLL);//if it fails exitreturn 0;}mainDLLFunc();//run exported functionFreeLibrary((HMODULE)DLL);}
8: 0000000000000000 7 FUNC GLOBAL DEFAULT 1 _Z1fv9: 0000000000000007 7 FUNC GLOBAL DEFAULT 1 ef10: 000000000000000e 17 FUNC GLOBAL DEFAULT 1 _Z1hv11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _Z1gv13: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND eg
#include <cassert>
#include "c.h"
int main() {assert(f() == 1);}
c. h
#ifndef C_H#define C_H
/* This ifdef allows the header to be used from both C and C++* because C does not know what this extern "C" thing is. */#ifdef __cplusplusextern "C" {#endifint f();#ifdef __cplusplus}#endif
#endif
#ifndef CPP_H#define CPP_H
#ifdef __cplusplus// C cannot see these overloaded prototypes, or else it would get confused.int f(int i);int f(float i);extern "C" {#endifint f_int(int i);int f_float(float i);#ifdef __cplusplus}#endif
#endif
cpp.cpp
#include "cpp.h"
int f(int i) {return i + 1;}
int f(float i) {return i + 2;}
int f_int(int i) {return f(i);}
int f_float(float i) {return f(i);}
#include<iostream>using namespace std;
extern "C"{#include<stdio.h> // Include C Headerint n; // Declare a Variablevoid func(int,int); // Declare a function (function prototype)}
int main(){func(int a, int b); // Calling function . . .return 0;}
// Function definition . . .void func(int m, int n){////}
#include <stdio.h>
// Two functions are defined with the same name// but have different parameters
void printMe(int a) {printf("int: %i\n", a);}
void printMe(char a) {printf("char: %c\n", a);}
int main() {printMe('a');printMe(1);return 0;}