1、控制台应用程序DLL的生成

(1)dllexport.h

#ifdef WIN32
#ifdef DLL_TEST_EXPORT
#define DLL_TEST_API __declspec(dllexport)
#else
#define DLL_TEST_API __declspec(dllimport)
#endif
#endif


(2)ncDllTest.h

// ncDllTest.h
// #设置预处理器
// ADD_DEFINITIONS("-DLL_SAMPLE_EXPORT")


#define DLL_TEST_EXPORT
#include "dllexport.h"

extern "C" DLL_TEST_API void TestDLL(int);


(3)ncDllTest.cpp

// ncDllTest.cpp
#include "stdafx.h"
#include "ncDllTest.h"
#include <stdio.h>

void TestDLL(int arg)
{
printf("DLL output arg %d\n", arg);
}


2、在控制台应用程序中调用DLL




#include "stdafx.h"


#include <iostream>
#include <windows.h>
#include <tchar.h>
typedef void(*DLLFunc)(int);

int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hInstLibrary = LoadLibrary(_T("ConsoleApplication1")); // dll的相对路径
if (hInstLibrary == NULL)
{
FreeLibrary(hInstLibrary);
printf("LoadLibrary err\n");
getchar();
return 1;
}
DLLFunc dllFunc = (DLLFunc)GetProcAddress(hInstLibrary, "TestDLL");
if (dllFunc == NULL)
{
FreeLibrary(hInstLibrary);
printf("GetProcAddress err\n");
getchar();
return 1;
}
dllFunc(123);
FreeLibrary(hInstLibrary);
getchar();


return 0;
}