100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > c++调用dll动态链接库历程

c++调用dll动态链接库历程

时间:2022-09-30 00:21:33

相关推荐

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;

}



本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。