全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2527
推到 Plurk!
推到 Facebook!

如何使用DLL中的class呢?

缺席
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-06-10 20:41:01 IP:59.125.xxx.xxx 未訂閱
要怎麼做才能讓主程式能是用MYDLL(動態的)呢?

[code cpp]
MYDLL.h
class __declspec(dllexport) file
{
public:
file(){};
~file(){};
void Load(int);
};


MYDLL.cpp
#include "MYDLL.h"
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
void CTest::Load(int Int_IO)
{
MessageBox(NULL, "OK","Message from DLL", MB_OK);
}


主程式
#include
#include
using namespace std;
int main(int argc, char* argv[])
{
return 0;
}

[/code]

麻煩大大了 非常謝謝!謝謝!
------
-謝謝大大熱心的回覆!謝謝!
編輯記錄
l90425 重新編輯於 2009-06-10 20:41:35, 註解 無‧
l90425 重新編輯於 2009-06-10 20:42:07, 註解 無‧
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-06-22 18:06:23 IP:59.125.xxx.xxx 未訂閱
參考看看(給後面的人看)

[code cpp]
//-------- FooInterface.h --------//
#ifndef FOOINTERFACE_H
#define FOOINTERFACE_H

class IFoo
{
public:
int GetNumber() = 0;
void SetNumber( int & ) =0;
};

#endif // FOOINTERFACE_H


//-------- FooClass.h --------//
#ifndef FOOCLASS_H
#define FOOCLASS_H

#include "FooInterface.h"

class Fooclass :public ifoo
{
public:
FooClass();
const int& GetNumber();
void SetNumber( int & );
private:
int number;
};

#endif // FOOCLASS_H


//-------- FooClass.cpp --------//

FooClass::FooClass()
{
number = 0;
}

int FooClass::GetNumber()
{
return number;
}

void FooClass::SetNumber(int &arg)
{
number = arg;
}

//-------- DllExports.h --------//
#ifndef DLLEXPORTS_H
#defind DLLEXPORTS_H

#ifdef __dll__
#define IMPEXP __declspec(dllexport)
#else
#define IMPEXP __declspec(dllimport)
#endif // __dll__

#include "FooClass.h"


extern "C"
void* IMPEXP CreateFooClassInstance();


#endif // DLLEXPORTS_H

//-------- DllMain.cpp --------//
#define __dll__
#include "DllExports.h"

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}

void* IMPEXP CreateFooClassInstance();
{
return static_cast< void* > (new FooClass);
}


//-------- ExeMain.cpp --------//
#include "FooInterface.h"
#include
#include
#include


int main(int argc, char* argv[])
{
HINSTANCE hdll = NULL;
IFoo* piFoo = NULL;
typedef void* (*pvFunctv)();
pvFunctv CreateFoo;


hdll = LoadLibrary("DllMain.dll"); // load the dll
CreateFoo = static_cast < fpFunctv> ( // get the function pointer
GetProcAddress( hdll, "_CreateFooClassInstance" ) );

piFoo = static_cast< piFoo* > ( CreateFoo() ); // get pointer to object

piFoo->gtSetNumber(8); // start using the object
cout << "Foo::number is epual to: "
<< piFoo-> GetNumber()
<< endl;
....}

[/code]
------
-謝謝大大熱心的回覆!謝謝!
編輯記錄
l90425 重新編輯於 2009-06-22 18:07:19, 註解 無‧
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-06-22 18:08:59 IP:59.125.xxx.xxx 未訂閱
上面的出處 http://edn.embarcadero.com/tw/article/20165

To use the class in your executable:

  1. Call LoadLibrary() on the dll that contains the class.
  2. Call GetProcAddress() to gain access to the CreateClassInstance() function.
  3. Call CreateClassObject() and store the returned address in an interface pointer.

===================引 用 l90425 文 章===================
參考看看(給後面的人看)
------
-謝謝大大熱心的回覆!謝謝!
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-06-22 18:37:36 IP:118.169.xxx.xxx 訂閱
感謝分享 ^_^

請問是
dll from BCB -> BCB呼叫

還是
dll from VC -> BCB呼叫

還是沒有限制?
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-06-25 00:50:31 IP:59.125.xxx.xxx 未訂閱
沒有限制!

不客氣啦!
===================引 用 taishyang 文 章===================
請問是
dll from BCB -> BCB呼叫

還是
dll from VC -> BCB呼叫

還是沒有限制?
------
-謝謝大大熱心的回覆!謝謝!
編輯記錄
l90425 重新編輯於 2009-06-25 00:53:45, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-06-25 10:12:25 IP:118.169.xxx.xxx 訂閱
可以的話能否寫個範例放在
會員作品發表區(限本人創作發表)

供有需要的人參考?

小弟記得站上很多相關討論但都沒有結論
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-06-25 20:57:08 IP:59.125.xxx.xxx 未訂閱
恩! 等小弟有空閒的時候就會上傳作品了

最近這一兩個月沒有什麼空閒,等過了再上傳作品,謝謝!!
===================引 用 taishyang 文 章===================
可以的話能否寫個範例放在
會員作品發表區(限本人創作發表)

供有需要的人參考?

小弟記得站上很多相關討論但都沒有結論
------
-謝謝大大熱心的回覆!謝謝!
系統時間:2024-05-19 0:07:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!