calling c++ dlls from vc++ -- with win32 dynamic-link library copyright (c) 2006 ncnu lab 202 all...

Post on 19-Dec-2015

220 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

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 OneUsing 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 DLLDWORD 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 valeMYDLL_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 = " ; cin >> num_01 ;cout << "num_02 = " ; cin >> 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 TwoUsing " .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 typeHINSTANCE hDLL = NULL ; //default valueLPGETNUMBER 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

top related