/******************************************************************/

/* Regular DLL using shared MFC DLL 첫번째 방법

/******************************************************************/

1. 프로젝트를 생성한다.

[MFC AppWizard(dll)] -> [Regular DLL using shared MFC DLL] -> [Finish]

 

2. 새로 생성된 프로젝트의 소스에서 맨 밑에 추가

.....

 

CMFC0808App::CMFC0808App()
{
 // TODO: add construction code here,
 // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMFC0808App object

 

//추가

extern "C" __declspec(dllexport)
 double Square( double d)
 {
     return d * d;
 }

 

3. 컴파일 하면 dll파일 생성된다.

 

4. 다이얼로그 베이스의 새로운 프로젝트를 생성.

dll파일을 새로 만들 프로젝트에 복사해서 붙여 넣는다.

 

5. 에디트 박스 두개와 버튼을 만든다.

 - IDC_EDIT1 : 입력 받는 에디트 박스( 컨트롤 변수 생성 : m_dInput, double형 )

 - IDC_EDIT2 : 결과 출력할 에디트 박스( 컨트롤 변수 생성 : m_dOutput, double형 )

 - IDC_BUTTON1 : 버트 눌렀을때 Square함수 호출

 

6. OnInitDialog에서

if( (m_hDll = LoadLibrary("MFC0808.dll")) == NULL )
{
    AfxMessageBox( "dll파일이 존재하지 않습니다. " );
}

 

7. OnDestroy에서

FreeLibrary( m_hDll ); 

 

8. 해더 파일에서

HINSTANCE m_hDll; //추가

 

9. OnButton1에서 (IDC_BUTTON1)

double (*pSquare)(double);

 

if( !(pSquare = (double(*)(double))GetProcAddress(m_hDll, "Square")) )
{
    AfxMessageBox( "Square함수가 존재하지 않습니다." );
}

 

UpdateData( TRUE );

 

m_dOutput = (*pSquare)(m_dInput);

 

UpdateData(FALSE);

 

 

 

/******************************************************************/

/* Regular DLL using shared MFC DLL 두번째 방법

/******************************************************************/

1. 새로운 프로젝트를 생성한다.

[MFC AppWizard(dll)] -> [Regular DLL using shared MFC DLL] -> [Finish]

 

2. 새로 생성한 프로젝트에서 해더파일 추가

Square.h

 

3. Squart.h에서

#include <windows.h>

 

extern "C" __declspec(dllimport)
double Square( double d);

 

4. 소스에서

#include "Square.h"

 

.....

 

CMFC0808App::CMFC0808App()
{
 // TODO: add construction code here,
 // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMFC0808App object

CMFC0808App theApp;

 

//추가

extern "C" __declspec(dllexport)
double Square( double d)
{
    return d * d;
}

 

5. 컴파일 하면 dll파일과 lib파일 생성

 

6. 다이얼로그 베이스의 새로운 프로젝트를 생성.

dll파일, lib파일, Square.h파일을 복사해서 붙여 넣는다.

 

7. 에디트 박스 두개와 버튼을 만든다.

 - IDC_EDIT1 : 입력 받는 에디트 박스( 컨트롤 변수 생성 : m_dInput, double형 )

 - IDC_EDIT2 : 결과 출력할 에디트 박스( 컨트롤 변수 생성 : m_dOutput, double형 )

 - IDC_BUTTON1 : 버트 눌렀을때 Square함수 호출

 

8. 소스에 해더파일과 lib파일 링크 추가

#include "Square.h"

 

9. OnButton1에서 (IDC_BUTTON1)

UpdateData( TRUE );

 

m_dOutput = Square(m_dInput);

 

UpdateData(FALSE);


/******************************************************************/

/* MFC Extension DLL(using shard MFC DLL)

/******************************************************************/

1. 새로운 프로젝트를 생성한다.

[MFC AppWizard(dll)] -> [MFC Extension DLL(using shard MFC DLL)] -> [Finish]

 

2. 새로운 클래스를 추가한다.

Class Type : Generic Class

Class Name : CTest

 

3. CTest클래스의 해더에서

// Test.h: interface for the CTest class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TEST_H__84322227_2364_48FD_B142_DE36100C3F0C__INCLUDED_)
#define AFX_TEST_H__84322227_2364_48FD_B142_DE36100C3F0C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

 

class AFX_EXT_CLASS CTest   //AFX_EXT_CLASS 추가
{
    int num;

 

public:
    CTest();
    virtual ~CTest();

 

    void SetNum( int num );
    int GetNum() const;
    int Square( int num ); 
};

 

#endif // !defined(AFX_TEST_H__84322227_2364_48FD_B142_DE36100C3F0C__INCLUDED_)

 

4. CTest클래스의 소스에서

// Test.cpp: implementation of the CTest class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Test.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

 

CTest::CTest()
{
    num = 0;
}

 

CTest::~CTest()
{

}

 

void CTest::SetNum( int num )
{
    this->num = num;
}

 

int CTest::GetNum() const
{
    return num;
}

 

int CTest::Square( int num )
{
    return this->num * num;
}

 

5. 컴파일 하면 dll파일과 lib파일이 생성된다.

 

6. 콘솔 모드로 프로젝트를 새로 생성한다.

 

7. dll.파일과 lib파일 Test.h파일을 새로 만든 콘솔 프로젝트에 복사해서 붙여넣는다.

lib파일 링크 추가

 

8. 콘솔 모드 프로젝트에 Test.h파일 include

AFX_EXT_CLASS // <--이부분 삭제

 

9. 콘솔 모드 프로젝트에 cpp파일 추가

#include <iostream>
#include "Test.h"

 

using namespace std;

 

void main()
{
    CTest test;

    test.SetNum( 10 );

 

    cout << test.GetNum() << endl;

    cout << test.Square( 100 ) << endl;
}


 SCHOOL* GetValue()
 {
  //return값이 여러개 필요하므로 포인터로 넘긴다
  //포인터로 넘기는 방법
  return strSCHOOL;
 }

 ITEM* GetITEM()
 {
  return strITEM;
 }

+ Recent posts