Win32类型的 dll工程:
App.h/***************************
#pragma once
#define extern_c_declspec extern "C" _declspec(dllexport)
class App
{
public:
App(void);
~App(void);
int _Data;
int GetValue(int* content);
};
extern_c_declspec int DisPlay(int num);
App.cpp/***************************
#include "StdAfx.h"
#include "App.h"
App::App(void)
{
_Data=0;
}
App::~App(void)
{
}
int App::GetValue(int* content)
{
return (*content)*2;
}
extern_c_declspec int DisPlay(int num)
{
App *app=new App();
app->_Data=num;
int rt=app->GetValue(&(app->_Data));
return rt;
}
windowsApplication应用(调用dll工程)/***************************
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "tchar.h"
#include "windows.h"
typedef int (*Func)(int a);
Func _function;//函数指针定义为全局变量
HMODULE hModule=NULL;//句柄定义为全局变量
void Initial()
{
hModule=LoadLibrary(_TEXT("..//Debug//NewAPI.dll"));
_function=(Func)GetProcAddress(hModule,"DisPlay");
}
void Release()
{
FreeLibrary(hModule);//释放指针必须调用函数结束后才可释放
}
int _tmain(int argc, _TCHAR* argv[])
{
Initial();
while(true)
{
int a;
scanf("%d",&a);
int temp=_function(a);
printf("temp=%d\n",temp);
}
Release();
return 0;
}