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

BCB 呼叫DLL的問題

答題得分者是:syntax
Patil
一般會員


發表:4
回覆:7
積分:2
註冊:2010-02-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-03-10 20:57:42 IP:114.42.xxx.xxx 訂閱
我的DLL檔案是由LabVIEW產生的,同時也產生了H與Lib檔案。

H檔的內容如下:
[code cpp]
#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif

int16_t __cdecl LED(float UpperBound, float LowerBound, float LED_ON_Limit,
int32_t Coordinates[], int32_t GlobalRectangle[], int32_t len, int32_t len2);

long __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);
#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)[/code]

我使用的方式是在主專案內的H檔先宣告函數,如下:

[code cpp]
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br />#include "MyLED.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TListBox *ListBox1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//底下這行是我定義的DLL檔中函數的內容
int16_t __declspec(dllimport) __cdecl LED(float UpperBound, float LowerBound, float LED_ON_Limit,int32_t Coordinates[], int32_t GlobalRectangle[], int32_t len, int32_t len2);

[/code]

以下是main檔

[code cpp]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int global[4]={146,233,168,257};
int *ptr1=global;
int contour[4]={146,233,168,257};
int *ptr2=contour;


float upper=50,lower=0;
bool out;
out=LED(upper,lower,0,ptr1,ptr2,sizeof(global),sizeof(contour));
if(out==true)
{
ListBox1->Color=clGreen;
}
else
{
ListBox1->Color=clRed;
}
}

[/code]

但是卻出現如下的Error,但是我在我的專案中卻有發現MAIN.OBJ,所以想請教問題是出在哪? 或是我在call函數有錯誤引用方式呢?
[Linker Error] Unresolved external '_LED' referenced from C:\PROGRAM FILES\BORLAND\CBUILDER6\PROJECTS\TEST\MAIN.OBJ

syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-03-11 07:58:40 IP:59.125.xxx.xxx 訂閱
你自己已經說出答案了

--->我的DLL檔案是由LabVIEW產生的,同時也產生了H與Lib檔案。
--->H檔的內容如下:

extern "C" <------- 加了嗎?
===================引 用 Patil 文 章===================
我的DLL檔案是由LabVIEW產生的,同時也產生了H與Lib檔案。

H檔的內容如下:
[code cpp]
#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif

int16_t __cdecl LED(float UpperBound, float LowerBound, float LED_ON_Limit,
int32_t Coordinates[], int32_t GlobalRectangle[], int32_t len, int32_t len2);

long __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);
#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)[/code]

我使用的方式是在主專案內的H檔先宣告函數,如下:

[code cpp]
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include
#include
#include
#include <forms.hpp><br />#include "MyLED.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TButton *Button1;
TListBox *ListBox1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormClick(TObject *Sender);
private:// User declarations
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//底下這行是我定義的DLL檔中函數的內容
int16_t __declspec(dllimport) __cdecl LED(float UpperBound, float LowerBound, float LED_ON_Limit,int32_t Coordinates[], int32_t GlobalRectangle[], int32_t len, int32_t len2);</forms.hpp></stdctrls.hpp>

[/code]

以下是main檔

[code cpp]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int global[4]={146,233,168,257};
int *ptr1=global;
int contour[4]={146,233,168,257};
int *ptr2=contour;


float upper=50,lower=0;
bool out;
out=LED(upper,lower,0,ptr1,ptr2,sizeof(global),sizeof(contour));
if(out==true)
{
ListBox1->Color=clGreen;
}
else
{
ListBox1->Color=clRed;
}
}

[/code]

但是卻出現如下的Error,但是我在我的專案中卻有發現MAIN.OBJ,所以想請教問題是出在哪? 或是我在call函數有錯誤引用方式呢?
[Linker Error] Unresolved external '_LED' referenced from C:\PROGRAM FILES\BORLAND\CBUILDER6\PROJECTS\TEST\MAIN.OBJ

Patil
一般會員


發表:4
回覆:7
積分:2
註冊:2010-02-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-03-11 09:23:58 IP:114.42.xxx.xxx 訂閱
不好意思,我不是很了解您的意思
我應該在哪邊加上 Extern "C" 呢?
第一次試著要將DLL引用進來,對於一些方式還不太清楚
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2010-03-12 15:07:01 IP:59.125.xxx.xxx 訂閱

如果你已經 include 該 lib 的 h 檔,應該不需要在定義一次
如果你覺得不安心,要再定義一次,那請直接複製
因為其有使用 Extern "C"
所以你要「照抄」

#ifdef ...
Extern "C"
{...
你想要的定義,不須要的可刪去
}...

===================引 用 Patil 文 章===================
不好意思,我不是很了解您的意思
我應該在哪邊加上 Extern "C" 呢?
第一次試著要將DLL引用進來,對於一些方式還不太清楚
編輯記錄
syntax 重新編輯於 2010-03-12 15:08:08, 註解 無‧
lgl_debug
一般會員


發表:0
回覆:1
積分:0
註冊:2008-09-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2011-03-07 00:22:28 IP:222.248.xxx.xxx 訂閱
 (1).impdef.exe
用法:impdef.exe deffile.def yourdll.dll
生成指定DLL文件的def文件,可以用它来查看DLL中的函数声明。例如,BCB使用VC的DLL时,
可能需要查看一下VC中导出函数的函数名;或者未使用extern "C"调用约定时,可以用它来
查看DLL中导出函数的C 命名方式,从而可以正确调用。
(2).implib.exe
"注意RAD2010 用-a 才可以生成被BCB调用的LIB"
/*CodeGear Implib Version 3.2.0 Copyright (c) 1991-2009 CodeGear
Syntax: IMPLIB [options] libname[.lib] [@respfile | srcname] [srcname ...]
Options:
-a Add '_' alias for MS flavor cdecl functions
-aa Force the alias even if the function already starts with '_'
-c Case sensitive symbols
-f Force imports by name (with hints)
-w No Warnings
Respfile may contain a list of source files to process.
Wildcards are ok for .DLL and .DEF file names.*/
系統時間:2024-04-16 18:04:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!