64,634
社区成员
发帖
与我相关
我的任务
分享
LIBRARY "DLLNAME"
EXPORTS
FUCNAME
/***************************** ParkLib.def 文件内容 ***************************/
; ParkLib.def : Declares the module parameters for the DLL.
LIBRARY "ParkLib"
DESCRIPTION 'ParkLib Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
MyCustomFunction
/****************************** ParkLib.h 文件内容 ***************************/
/*--------------------------------------------------------------------------
ParkLib.H header file
----------------------------------------------------------------------------*/
#ifdef __ParkStdFee
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif
/******************************************************************************
* 自定义一个数据结构用于返回
******************************************************************************/
typedef struct
{
int result;
double circle;
char* version;
}_MY_CUSTOM_;
/******************************************************************************
* 函数功能:根据输入的 [type] ,[name] 及 [product] 返回结果
* 输 入:[type] ,[name] 及 [product]
* 返 回:_MY_CUSTOM_ 结构数据
******************************************************************************/
EXPORT _MY_CUSTOM_ CALLBACK MyCustomFunction(int type, char* name, char* product);
/***************************** ParkLib.C 文件内容 ****************************/
/*---------------------------------------------------------------------------
ParkLib.C -- Easy Drawing Routine Library module
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include "ParkLib.h"
_MY_CUSTOM_ CALLBACK MyCustomFunction(int type, char* name, char* product); //自定义函数
int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
return TRUE ;
}
EXPORT _MY_CUSTOM_ CALLBACK MyCustomFunction(int type, char* name, char* product)
{
_MY_CUSTOM_ myCustom;
myCustom.result = type;
myCustom.circle = type * 2;
myCustom.version = name;
return myCustom;
}
/****************************** 测试程序(MFC程序) *****************************/
1. 在头文件中声明一个结构体:
struct _MY_CUSTOM_
{
int result;
double circle;
char* version;
};
2. 创建一个结构体实例:
struct _MY_CUSTOM_ myCustom;
3. 测试代码:
HINSTANCE hInst = LoadLibrary("HmPark.dll");
typedef _MY_CUSTOM_ (CALLBACK *ADDPROC)(int type, char* name, char* product);
ADDPROC MyCustomFunction = (ADDPROC)GetProcAddress(hInst, "MyCustomFunction");
if( !MyCustomFunction )
{
MessageBox("找不到动态库");
return;
}
char* name = "my";
char* product = "custom";
int type = 5;
CString str;
try
{
myCustom = MyCustomFunction(type, name, product);
}
catch(...)
{
MessageBox("Error");
}
str.Format("\n%d, %f, %s.\n", myCustom.result, myCustom.circle, myCustom.version );
MessageBox( str );
4. 输出结果: 5, 10.000000, my.
LIBRARY "GetHardInfo"
EXPORTS
GetCPUID
***************************************/
extern "C" _declspec(dllexport) void _stdcall GetCPUID(char pcCPUID[36])
{
memset(pcCPUID,0,sizeof(pcCPUID));
unsigned long s1=0,s2=0,s3=0,s4=0;
_asm
{
mov eax,01h
xor edx,edx
cpuid
mov s1,eax
mov s2,ebx
mov s3,ecx
mov s4,edx
}
sprintf_s(pcCPUID,CPUID_NUM,"%08x-%08x-%08x-%08x",s1,s2,s3,s4);
return ;
}