c++调用dll动态链接库历程
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;
}
總結
以上是生活随笔為你收集整理的c++调用dll动态链接库历程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intel MKL 多线程设置
- 下一篇: 伤钱伤感情 10件不能和亲戚一起干的事儿