1. 程式人生 > >vs 2017 製作Dll檔案的兩種方法,以及呼叫Dll檔案的兩種方法。

vs 2017 製作Dll檔案的兩種方法,以及呼叫Dll檔案的兩種方法。

近來學習製作Dll檔案,看了幾個視訊教程,看了網上的例子,看了msdn上的例子。現在做個總結,以便來日回顧,同時也希望以大家相互交流學習。

注意1:用 method 1 named "Using Load - Time Dynamic Linking" 呼叫dll檔案,需要將相應的.lib新增到資原始檔中,另外dll檔案的原始檔(two kinds method of create Dll.cpp和dllmain.cpp)需要新增到現在呼叫檔案的原始檔中,感覺比較麻煩。
注意2:用method 2 named "Using Run - Time Dynamic Linking"呼叫dll檔案,需要用到“two kinds method of create Dll.dll“的絕對路徑,即:"G:\\軟體學習練習空間\\windows program\\two kinds method of create Dll\\Debug\\two kinds method of create Dll.dll"。


1.呼叫程式如下。

// two kinds method of call Dll file.cpp: 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <windows.h>
#include<iostream>
#include <stdio.h>
//method 1 named "Using Load - Time Dynamic Linking"
//Because this example calls the DLL function explicitly, 
//the module for the application must be linked with
//the import library Myputs.lib.

using std::cout;
using std::endl;
extern "C" int __cdecl mySub1(int x,int y); // a function from a DLL
extern "C" int __cdecl mySub2(int x,int y); // a function from a DLL

int main2(void);
int main(VOID)
{
	std::cout << "1. call " << "extern \"C\"int __cdecl mySub1(int x,int y) ="<< mySub1(5,3) << std::endl;
	std::cout << "2. call " << "extern \"C\" int __cdecl mySub2(int x,int y) =" << mySub2(5, 3) << std::endl;
	main2();
	return 1;
}


//method 2 named "Using Run - Time Dynamic Linking"
//Because the program uses run - time dynamic linking, 
//it is not necessary to link the module with an import
//library for the DLL.

// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from Myputs.dll.

typedef int(__cdecl *mySub)(int x,int y);
//typedef int(__cdecl *mySub2)(int x, int y);
int main2(void)
{
	HINSTANCE hinstLib;
	mySub ProcAdd1, ProcAdd2;

	BOOL fFreeResult, fRunTimeLinkSuccess1, fRunTimeLinkSuccess2= FALSE;
	// Get a handle to the DLL module.
	hinstLib = LoadLibrary(L"G:\\軟體學習練習空間\\windows program\\two kinds method of create Dll\\Debug\\two kinds method of create Dll.dll");
	// If the handle is valid, try to get the function address.
	if (hinstLib != NULL)
	{
		ProcAdd1 = (mySub)GetProcAddress(hinstLib, "mySub1");
		ProcAdd2 = (mySub)GetProcAddress(hinstLib, "mySub2");
		// If the function address is valid, call the function.
		
			if (NULL != ProcAdd1)
			{
				fRunTimeLinkSuccess1 = TRUE;
				cout << "typedef int(__cdecl *mySub)(int x,int y) ,mySub1=" << ProcAdd1(7, 3)<<endl;
			}
			if (NULL != ProcAdd2)
			{
				fRunTimeLinkSuccess2 = TRUE;
				cout<<"typedef int(__cdecl *mySub)(int x,int y) ,mySub2="<<ProcAdd1(7, 2)<<endl;
			}
		// Free the DLL module.
		fFreeResult = FreeLibrary(hinstLib);
	}
	// If unable to call the DLL function, use an alternative.
	if (!fRunTimeLinkSuccess1)
		printf("mySub1 failed\n");
	if (!fRunTimeLinkSuccess2)
		printf("mySub2 failed\n");
	return 0;
}

2. 在vs2017建立中建立dll檔案“two kinds method of create Dll”後,生成了兩個可以寫原始碼的.ccp檔案。它 們  是 “ dllmain.cpp”和“two kinds method of create Dll.cpp ”,下面分別在這兩個.ccp檔案中寫方法。

2.1 dll檔案,在“dllmain.cpp ”寫程式。

// dllmain.cpp : 定義 DLL 應用程式的入口點。
#include "stdafx.h"

#include <windows.h>
#define EOF (-1)
#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif
	//__________method 1,create dll file in dllmain.cpp.write code as follow:
	__declspec(dllexport) int __cdecl mySub1(int a,int b)
	{
		int c;
		c = a + b;
		return c;
	}
#ifdef __cplusplus
}
#endif
//__________method 1,create dll file in dllmain.cpp.write code as above:

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

2.2 Dll程式,在“ two kinds method of create Dll.cpp”中寫Dll程式。

// two kinds method of create Dll.cpp: 定義 DLL 應用程式的匯出函式。
//

#include "stdafx.h"


#include <windows.h>
#define EOF (-1)
#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif
			 //__________method 2,create dll file in dllmain.cpp.write code as follow:
	__declspec(dllexport) int __cdecl mySub2(int a, int b)
	{
		int c;
		c = a + b;
		return c;
	}
#ifdef __cplusplus
}
#endif
//__________method 2,create dll file in dllmain.cpp.write code as above:

3. 輸出結果如下: