C++ Dll Win32 Console Project

1. สร้างโปรเจ็กส์ C++ แบบ Dll ชื่อ CDll เป็นแบบ Win32 Console Application > Dll

2. สร้างโปรเจ็กส์ C++ เพื่อใช้งาน Dll ชื่อ WinConsole เป็นแบบ Win32 Console Application > Console Application 

 

 เพิ่ม Code

// CDll.cpp : Defines the exported functions for the DLL application. // 
#include "stdafx.h"

extern "C" _declspec(dllexport) int Calc(int no1,int no2)
{
    int result;
    result = no1 + no2;
    return result;
} 

 

 

  เพิ่ม Code

// WinConsole.cpp : Defines the entry point for the console application. // 
#include "stdafx.h"

extern "C" _declspec(dllimport) int Calc(int no1,int no2);

int _tmain(int argc, _TCHAR* argv[])
{
    int res;
    res = Calc(10,20);
    printf("%d",res);

    return 0;
}

ให้โปรเจ็กส์ WinConsole ทำการ Add reference project ไปที่โปรเจ็กส์ CDLL

ที่มา: Creation of a Simple DLLWalkthrough: Creating and Using a Dynamic Link Library