unit1.cpp

来自「电脑编程技巧和源码。很不错的。」· C++ 代码 · 共 86 行

CPP
86
字号
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DllTestClick(TObject *Sender)
{
	typedef UINT (CALLBACK* LPFNDLLFUNC1)(int, float *);
	typedef float (CALLBACK* LPFNDLLFUNC2)(float);
	typedef float (CALLBACK* LPFNDLLFUNC3)(float, float);

	HINSTANCE hDLL;               // Handle to DLL
	LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
	LPFNDLLFUNC2 lpfnDllFunc2;    // Function pointer
	LPFNDLLFUNC3 lpfnDllFunc3;    // Function pointer

//	hDLL = LoadLibrary("F:/Projects/Test/Debug/Test.dll");
	hDLL = LoadLibrary("E:/Projects/Test/Debug/Test.dll");

	if (hDLL != NULL)
	{
		lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "SUM");       //"DLLFunc1"
		lpfnDllFunc2 = (LPFNDLLFUNC2)GetProcAddress(hDLL, "MYABS");     //"DLLFunc2"
		lpfnDllFunc3 = (LPFNDLLFUNC3)GetProcAddress(hDLL, "ADD");     	//"DLLFunc3"

		if (!lpfnDllFunc1)
		{
			// handle the error
			FreeLibrary(hDLL);
			// return SOME_ERROR_CODE;
                        MessageDlg("The function does not exist.", mtWarning, TMsgDlgButtons() << mbYes, 0);
		}
		else
                {
			// call the function
       	                for(int i=1;i<10;i++)
			{
				lpfnDllFunc1(i,&res1);
               	                Label1->Caption=FloatToStr(res1);
			}
		}

		if (!lpfnDllFunc2)
		{
			// handle the error
			FreeLibrary(hDLL);
            	        MessageDlg("The function does not exist.", mtWarning, TMsgDlgButtons() << mbYes, 0);
		}
		else
		{
			// call the function
		        for(float i=-21.5;i<-11.5;i++)
			{
				res2 = lpfnDllFunc2(i);
                	        Label2->Caption=FloatToStr(res2);
			}
		}

		if (!lpfnDllFunc3)
		{
			// handle the error
			FreeLibrary(hDLL);
            	        MessageDlg("The function does not exist.", mtWarning, TMsgDlgButtons() << mbYes, 0);
		}
		else
		{
			// call the function
        		res3 = lpfnDllFunc3(-8.5, 15.8);
            	        Label3->Caption=FloatToStr(res3);
		}
	}
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?