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

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Operator Overloading Fundamentals
C++ Basics Variables, Identifiers, Assignments, Input/Output.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Lecture 11 Operating System Interaction This lecture covers three items, thread, is the most interesting as it makes the system faster if you know how.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Saves memory and reduces swapping No recompilation on changes A DLL can provide after-market support Programs written in different languages can call the.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
111 Introduction to OGRE3D Programming: Main Loop.
Dynamic Link Libraries: Inside Out. Dynamic Link Libraries  About Dynamic-Link Libraries  Dynamic-Link Libraries Hands On  Dynamic Link Library Reference.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
THE PREPROCESSOR
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Andy Wigley Device Application Development MVP APPA Mundi Ltd SESSION CODE: WEM309.
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 22 - C++ Templates
CS505 Data Structures and Algorithms
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Reserved Words.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Variables, Identifiers, Assignments, Input/Output
Lab 1 Introduction to C++.
Programming Introduction to C++.
Fundamental Programming
Data Structures & Algorithms
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

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

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

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

Method One Using Exported Symbols

Initial Dll Project – step 1

Initial Dll Project – step 2

Initial Dll Project – step 3

Initial Dll Project – step 4

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.

Initial Dll Project – step 5

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

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 };

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 "

Initial Console Project – step 1

Initial Console Project – step 2

Initial Console Project – step 3

Initial Console Project – step 4

Initial Console Project – step 5

Initial Console Project – step 6

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 ;

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 ;

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 ;

Initial MFC Project – step 1

Initial MFC Project – step 2

Initial MFC Project – step 3

Initial MFC Project – step 4

Initial MFC Project – step 5

Initial MFC Project – step 6

Insert New Item

Add Member Variables - 1

Add Member Variables - 2

Add Member Variables - 3

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) ; }

After Executing

Method Two Using ".def " File

Initial ExplicitDLL Project – step 1

Initial ExplicitDLL Project – step 2

Initial ExplicitDLL Project – step 3

Initial ExplicitDLL Project – step 4

Initial ExplicitDLL Project – step 5

Initial ExplicitDLL Project – step 6

Edit count.h

Edit count.cpp

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

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

Initial DllMFC Project – step 1

Initial DllMFC Project – step 2

Initial DllMFC Project – step 3

Initial DllMFC Project – step 4

Add Member Variables - 1

Add Member Variables - 2

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

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"); }

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); }

After Executing – delete ExplicitDLL2.dll

After Executing – calling ExplicitDLL.dll

After Executing – calling ExplicitDLL2.dll

Calling GUI DLL

Initial MyDLL Project – step 1

Initial MyDLL Project – step 2

Initial MyDLL Project – step 3

Initial MyDLL Project – step 4

Add Dialog Class

Edit IDD_MYCLASS.rc

Edit MyDLL.h

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

Edit MyDLL.def

Initial MyCSharp Project – step 1

Initial MyCSharp Project – step 2

Edit Form1.cs - 1

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

After Executing – calling MyDLL.dll

Initial MyMFC Project – step 1

Initial MyMFC Project – step 2

Initial MyMFC Project – step 3

Initial MyMFC Project – step 4

Initial MyMFC Project – step 5

Initial MyMFC Project – step 6

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

After Executing – calling MyDLL.dll

Return GUI DLL

Initial MyDLL Project – step 1

Initial MyDLL Project – step 2

Initial MyDLL Project – step 3

Initial MyDLL Project – step 4

Edit MyDLL.rc

Add Dialog Class

Add Member Variables

Edit MyDLL.cpp

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

Initial MyCSharp Project – step 1

Initial MyCSharp Project – step 2

Edit Form1.cs

After Executing – calling MyDLL.dll

DLL Call DLL

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

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 ; } }

Initial MyCSharp Project - 1

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();

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; } }

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(); }

After Executing - 1

After Executing - 2 remove MyDLL_1.dll

After Executing - 3 remove MyDLL_2.dll

After Executing - 3 remove MyMFCDLL.dll