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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值