線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:983
推到 Plurk!
推到 Facebook!

請問如何在DLL中使用主程式中的元件

尚未結案
agogo
一般會員


發表:2
回覆:4
積分:1
註冊:2004-01-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-12 22:39:57 IP:211.76.xxx.xxx 未訂閱
舉例: 今天有一個主程式(Uuit1)跟一個dll(test) 主程式中包含一個button跟progressbar    bar的範圍是0~10000    按下button->呼叫dll->然後在dll中要使用主程式中form的bar,讓bar可以跑 不過就錯了,不知道那出問題    以下有程式碼,可否幫我看一下    unit1.cpp -------------------
#include 
#pragma hdrstop    #include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"    void (__stdcall *test)();
HINSTANCE Operation;    TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}    void __fastcall TForm1::Button1Click(TObject *Sender)
{
 test();
}    void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Operation=LoadLibrary("test.dll");//動態載入
 (FARPROC&)test=GetProcAddress(Operation,"test");//取得位址
 ProgressBar1->Max = 10000; ProgressBar1->Min = 0; ProgressBar1->Position = 0;
}
test.cpp ------------------------------
#include 
#include 
#include "Unit1.h"
#pragma hdrstop    extern "C" __declspec(dllexport) void __stdcall test();    #pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}    void __stdcall test()
{
 int i;
 for(i=0;i<10000;i++)
  Form1->ProgressBar1->Position ++;
}
test.bpf ----------------------------
USEFORM("Unit1.cpp", Form1);
This file is used by the project manager only and should be treated like the project file    DllEntryPoint
大致上是將,可以build,但run出來會錯。 發表人 - agogo 於 2004/01/12 23:35:21
agogo
一般會員


發表:2
回覆:4
積分:1
註冊:2004-01-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-13 23:38:41 IP:211.76.xxx.xxx 未訂閱
沒人回應,我自己回應好了 經過一翻找尋,找到答案了    原文網址http://delphi.ktop.com.tw/topic.php?TOPIC_ID=35852 可以直接將物件傳入 dll 內,在 dll 內直接改變它的內容
//DLL
__declspec(dllexport) int __stdcall FPoint(TLabel *label,int x);
int __stdcall FPoint(int x)
{
  int iValueX=x;
  for(int i=0;i<100000;i  )
  {
    Application->ProcessMessages(); // 讓主表單可以更新 Label
    iValueX =i;
    label->Caption=iValueX;
  }
  return iValueX;
}    // 主表單程式
__declspec(dllimport) int __stdcall FPoint(TLabel *label,int x);
// 作一個按鈕啟動, Timer 定時更新顯示數值
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int y=20;
  FPoint(Label1,y);
}
系統時間:2024-05-18 14:47:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!