以动态库链接库 .dll 探索结构体参数

发布于:2024-04-08 ⋅ 阅读:(176) ⋅ 点赞:(0)

Dev c++ C语言实现第一个 dll 动态链接库 创建与调用-CSDN博客

 在写dll 插件中发现的函数指针用途和 typedef 的定义指针的用法-CSDN博客

两步之后,尝试加入结构体实现整体数据使用。

注意结构体 Ak

是相同的

代码如下

DLL文件有两个,dll.dll是上面提到的链接里的

dllv2.dll是这个代码里的

dllv2.cpp

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <iostream>

using namespace std;

void mainDll(){
	ak.count++;
	ak.number=1; 
	ak.x=0;
	ak.y=0;
	cout<<"atking"<<endl;
	for(int i=0;i<10;i++){
		ak.x++;
	}
	cout<<"count: "<<ak.count<<endl;
}
// dll.h
#ifndef _DLL_H_
#define _DLL_H_

typedef struct Ak{
	int x;								// 位置坐标 
	int y;
	int number;							// 类型 
	int time;							// 时长上限 
	int count;							// 计时 
}; 

Ak ak;

extern "C" 
{
	void mainDll();
}

#endif

 主程序代码如下

#include <windows.h>
#include <iostream>


typedef struct Ak
{
	int x;								// 位置坐标
	int y;
	int number;							// 类型
	int time;							// 时长上限
	int count;							// 计时
};

using namespace std;

int main()
{

	HINSTANCE hDLL = LoadLibrary("dll.dll"); 								// 填文件名
	HINSTANCE hDLLv2 = LoadLibrary("dllv2.dll");

	typedef void (*func)(double a, double b, double c[], double* aplusb); 	// 填调用的输入参数
	typedef void (*kk)(double a, double b, double c[], double* aplusb) ;

	void (*atk)()=(void (*)())GetProcAddress(hDLLv2,"mainDll"); 			// 强制类型转换为函数指针,然后成为atk 的替身
	typedef void(*ATK)();													// 定义函数指针类型,类型名为 ATK ,返回值类型是 void, 参数是 void
	ATK ky=(ATK)GetProcAddress(hDLLv2,"mainDll");

	func callDll =(func)GetProcAddress(hDLL, "mainDll"); 					// 填调用的 dll 函数名
	kk callDllv2= (kk) GetProcAddress(hDLL, "mainDll");

	double a = 1, b = 2, c[3] = {4, 5, 6}, result;

	callDll(a, b, c, &result);
	cout << a << endl;
	cout << b << endl;
	cout << result << endl;
	cout << c[0] << c[1] << c[2] << endl;
	cout<<endl;

	callDllv2(a, b, c, &result);
	cout << a << endl;
	cout << b << endl;
	cout << result << endl;
	cout << c[0] << c[1] << c[2] << endl;

	int i=5;
	while(1)
	{
		atk();
		if(i>=5)
		{
			cout<<"数据是否共享: ";
			ky();
			cout<<endl;
			i=0;
		}
		i++;

		Sleep(200);
	}

}


网站公告

今日签到

点亮在社区的每一天
去签到