|
저도 어디선가 보고 배운거지요....
도움이 되시길.....
먼저 File->New->DLL Wizard 를 선택해서 DLL파일을 만든다.
체크하는것이 있는데 그냥 기본으로 두고 OK버튼은 클릭
먼저 DLL의 소스화일을 저장하고, 프로젝트도 저장한다.
unit1.cpp project1.bpr --> 기본으로 했을때
그 상태에서 File-> NewForm 을 해서 폼을 생성한다
또 저장하다 unit2.cpp --> 기본으로 했은때
그 다음에 DLL의 소스화일에서 아래것을 추가한다
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "Unit2.h" // 새로 만든 폼의 헤더화일
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) void __stdcall ShowNewForm(void)
{
Form2 = new TForm2(NULL);
Form2->ShowModal();
delete Form2;
}
//---------------------------------------------------------------------------
이렇게 해서 Ctrl+F9를 누르면 DLL이 만들어집니다.
만든 DLL을 쓸려면.----
File->New Application 해서 하나 만들고,
폼에 버튼은 하나 놓고,
저장을 합니다,
main1.cpp와 main.bpr로 합니다.
main1.cpp의 소스화을 보면
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void (__stdcall *CreateNewForm)(void); // DLL화일 안에있는 함수의 프로토 타입
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE DLLInstNewForm; // dll화일의 포인터
DLLInstNewForm = LoadLibrary("project1.dll"); // dll화일을 읽는다
if( DLLInstNewForm ) {
CreateNewForm = (void(__stdcall *)(void))GetProcAddress(DLLInstNewForm, "ShowNewForm");
// dll화일 안에있는 함수이름, 위에서 만든것, 같은 이름("ShowNewForm")으로 해야죠
if( CreateNewForm ) {
CreateNewForm(); // 폼을 화면에 보여준다
}
else {
ShowMessage(SysErrorMessage(GetLastError()));
FreeLibrary(DLLInstNewForm);
}
FreeLibrary(DLLInstNewForm); // dll을 메모리에서 해제함
}
else {
ShowMessage(SysErrorMessage(GetLastError()));
ShowMessage("Unable to load the DLL");
}
}
//---------------------------------------------------------------------------
이렇게 하면 됩니다....
이해 잘되게 설명이 되었는지 모르겠네요.....
int (__stdcall *CreateNewForm)(int);
이런 색으로 해서 인수나 리턴값을 사용할 수도 있습니다.
|