Presentation is loading. Please wait.

Presentation is loading. Please wait.

Calling C++ DLLs from VC++ -- with Win32 Dynamic-Link Library Copyright (c) 2006 NCNU Lab 202 All Rights Reserved.

Similar presentations


Presentation on theme: "Calling C++ DLLs from VC++ -- with Win32 Dynamic-Link Library Copyright (c) 2006 NCNU Lab 202 All Rights Reserved."— Presentation transcript:

1 Calling C++ DLLs from VC++ -- with Win32 Dynamic-Link Library Copyright (c) 2006 NCNU Lab 202 All Rights Reserved

2 Files Required To Use A DLL Programmer : Normal User : .dll

3 Loading Method Loading DLL Implicitly  A DLL that exports some symbols. Loading DLL Explicitly  Using ".def " file. Calling GUI DLL  Using MFC DLL  Call by C# Application  Call by MFC Application Return Input DLL Call DLL

4 Method One Using Exported Symbols

5 Initial Dll Project – step 1

6 Initial Dll Project – step 2

7 Initial Dll Project – step 3

8 Initial Dll Project – step 4

9 Initial Define #pragma message("automatic link to MyDll.lib") #pragma comment(lib, "MyDll.lib")  Ensure when a project include the DLL’s header file, it will automatically link to the DLL’s Lib file.

10 Initial Dll Project – step 5

11 Understanding DllMain() BOOL APIENTRY DllMain ( HMODULE hModule,//Identify DLL DWORD ul_reason_for_call,//Why Call ?? LPVOID lpReserved//pointer )

12 Edit MyDll.h extern MYDLL_API float num_01 ; extern MYDLL_API float num_02 ; class MYDLL_API Plus { public: Plus(void) ;//Constructor float static myplus(float m, float n); //function float static dimension_1(float m, float n) ; //function };

13 Edit MyDll.cpp MYDLL_API float num_01 = 0 ;//set default vale MYDLL_API float num_02 = 0 ;//set default vale float Plus::myplus(float n, float m) { return (num_01 + num_02) ; } float Plus::dimension_1(float m, float n) { return ((num_01 + num_02) + (num_01 + num_02)) ; } Compile project “MyDLL" to get “MyDll.dll" & "method_01.lib". It will be under "..\debug "

14 Initial Console Project – step 1

15 Initial Console Project – step 2

16 Initial Console Project – step 3

17 Initial Console Project – step 4

18 Initial Console Project – step 5

19 Initial Console Project – step 6

20 Edit AppConsole.cpp - 01 float num_a = num_01 ; float num_b = num_02 ; cout << "Default Value : " << endl ; cout << "num_a = " << num_a << ", num_b = “<< num_b << endl ;

21 Edit AppConsole.cpp - 02 cout << "Keyin New Value : " << endl ; cout > num_01 ; cout > num_02 ; cout << "Update Value : " << endl ; cout << "num_01 = " << num_01 << ", num_02 = " << num_02 << endl ;

22 Edit AppConsole.cpp - 03 doplus = Plus::myplus(num_01, num_02) ; dimension_01 = Plus::dimension_1(num_01, num_02) ; cout << num_01 << " + " << num_02 << " = " << doplus << endl ; cout << "Result dimension_01 = " << dimension_01 << endl ;

23 Initial MFC Project – step 1

24 Initial MFC Project – step 2

25 Initial MFC Project – step 3

26 Initial MFC Project – step 4

27 Initial MFC Project – step 5

28 Initial MFC Project – step 6

29 Insert New Item

30 Add Member Variables - 1

31 Add Member Variables - 2

32 Add Member Variables - 3

33 Edit AppMFCDlg.cpp void CAppMFCDlg::OnBnClickedButton1() { CString mystr_a = string_a ; CString mystr_b = string_b ; CString mystr_c = string_c ; m_static.SetWindowText(mystr_a) ; m_edit.SetWindowText(mystr_b) ; m_button.SetWindowText(mystr_c) ; }

34 After Executing

35 Method Two Using ".def " File

36 Initial ExplicitDLL Project – step 1

37 Initial ExplicitDLL Project – step 2

38 Initial ExplicitDLL Project – step 3

39 Initial ExplicitDLL Project – step 4

40 Initial ExplicitDLL Project – step 5

41 Initial ExplicitDLL Project – step 6

42 Edit count.h

43 Edit count.cpp

44 Compile project "ExplicitDLL" to get “ExplicitDLL.dll" & "ExplicitDLL.lib". It will be under "..\debug " Edit count.def

45 Initial ExplicitDLL2 Project Follow page35 ~ page 42 to make ExplicitDLL2 After compile, you will get ExplicitDLL2.dll and ExplicitDLL2.lib

46 Initial DllMFC Project – step 1

47 Initial DllMFC Project – step 2

48 Initial DllMFC Project – step 3

49 Initial DllMFC Project – step 4

50 Add Member Variables - 1

51 Add Member Variables - 2

52 Edit DllMFCDlg.cpp - 1 typedef double (*LPGETNUMBER)(double Nbr) ;//define type HINSTANCE hDLL = NULL ;//default value LPGETNUMBER lpGetNumber ;//set pointer

53 Edit DllMFCDlg.cpp - 2 hDLL = AfxLoadLibrary("ExplicitDLL");//load DLL if( hDLL == NULL ) { AfxMessageBox("Can't load ExplicitDLL");//show message m_button1.ShowWindow(SW_HIDE) ;//hide button1 } else { lpGetNumber = (LPGETNUMBER)GetProcAddress(hDLL, "GetNumber"); }

54 Edit DllMFCDlg.cpp - 3 void CDllMFCDlg::OnBnClickedGetNumberBtn1() { double Number, GetNbr; UpdateData(); Number = atof(m_Number); GetNbr = lpGetNumber(Number); m_GetNumber.Format("%.2f", GetNbr); UpdateData(FALSE); }

55 After Executing – delete ExplicitDLL2.dll

56 After Executing – calling ExplicitDLL.dll

57 After Executing – calling ExplicitDLL2.dll

58 Calling GUI DLL

59 Initial MyDLL Project – step 1

60 Initial MyDLL Project – step 2

61 Initial MyDLL Project – step 3

62 Initial MyDLL Project – step 4

63 Add Dialog Class

64 Edit IDD_MYCLASS.rc

65 Edit MyDLL.h

66 Compile project "MyDLL" to get “MyDLL.dll" & " MyDLL.lib". It will be under "..\debug " Edit MyDLL.cpp

67 Edit MyDLL.def

68 Initial MyCSharp Project – step 1

69 Initial MyCSharp Project – step 2

70 Edit Form1.cs - 1

71 Edit Form1.cs - 2 private void OnClick(object sender, EventArgs e) { ShowHTBDialog() ; } private void button2_Click(object sender, EventArgs e) { Application.Exit() ; }

72 After Executing – calling MyDLL.dll

73 Initial MyMFC Project – step 1

74 Initial MyMFC Project – step 2

75 Initial MyMFC Project – step 3

76 Initial MyMFC Project – step 4

77 Initial MyMFC Project – step 5

78 Initial MyMFC Project – step 6

79 Edit MyMFCDlg.cpp void CMyMFCDlg::OnClick() { CMyDLLApp::ShowHTBDialog() ; }

80 After Executing – calling MyDLL.dll

81 Return GUI DLL

82 Initial MyDLL Project – step 1

83 Initial MyDLL Project – step 2

84 Initial MyDLL Project – step 3

85 Initial MyDLL Project – step 4

86 Edit MyDLL.rc

87 Add Dialog Class

88 Add Member Variables

89 Edit MyDLL.cpp

90 Edit MyDLL.def Compile project "MyDLL" to get “MyDLL.dll" & " MyDLL.lib". It will be under "..\debug "

91 Initial MyCSharp Project – step 1

92 Initial MyCSharp Project – step 2

93 Edit Form1.cs

94 After Executing – calling MyDLL.dll

95 DLL Call DLL

96 Initial Two DLL Project Like last example, we add two DLL :  “MyDLL_1” and “MyDLL_2”

97 Initial MyMFC DLL – edit MyMFCDLL.cpp int CMyMFCDLLApp::Dll_call_Dll() { h1DLL = LoadLibrary(_T("MyDLL_1.dll")); h2DLL = LoadLibrary(_T("MyDLL_2.dll")); if(h1DLL == NULL && h2DLL == NULL ) { return 1 ; } else if(h1DLL != NULL && h2DLL == NULL) { return 2 ; } else if(h1DLL == NULL && h2DLL != NULL ) { return 3 ; } else { return 4 ; } }

98 Initial MyCSharp Project - 1

99 Edit Form1.cs - 1 [DllImport("MyDLL_1.dll")] public static extern int ShowHTBDialog_1(); [DllImport("MyDLL_2.dll")] public static extern int ShowHTBDialog_2(); [DllImport("MyMFCDLL.dll")] public static extern int Dll_call_Dll();

100 Edit Form1.cs - 2 private void Form1_Load(object sender, EventArgs e) { int num; num = Dll_call_Dll(); if (num == 1) { MyButton_1.Enabled = false;MyButton_2.Enabled = false; } else if (num == 2) { MyButton_1.Enabled = true;MyButton_2.Enabled = false; } else if (num == 3) { MyButton_1.Enabled = false;MyButton_2.Enabled = true; } else { MyButton_1.Enabled = true;MyButton_2.Enabled = true; } }

101 Edit Form1.cs - 3 private void MyButton_1_Click(object sender, EventArgs e) { int num; num = ShowHTBDialog_1(); MyLebel_1.Text = num.ToString(); } private void MyButton_2_Click(object sender, EventArgs e) { int num; num = ShowHTBDialog_2(); MyLebel_2.Text = num.ToString(); }

102 After Executing - 1

103 After Executing - 2 remove MyDLL_1.dll

104 After Executing - 3 remove MyDLL_2.dll

105 After Executing - 3 remove MyMFCDLL.dll


Download ppt "Calling C++ DLLs from VC++ -- with Win32 Dynamic-Link Library Copyright (c) 2006 NCNU Lab 202 All Rights Reserved."

Similar presentations


Ads by Google