我有用C++編寫的外部DLL。下面的片聲明一個結構類型和功能,這,報錯一個指針,滿足了這一類型的變量:從Delphi應用程序調用外部函數(C++)時的訪問衝突
enum LimitType { NoLimit, PotLimit, FixedLimit };
struct SScraperState
{
char title[512];
unsigned int card_common[5];
unsigned int card_player[10][2];
unsigned int card_player_for_display[2];
bool dealer[10];
bool sitting_out[10];
CString seated[10];
CString active[10];
CString name[10];
double balance[10];
bool name_good_scrape[10];
bool balance_good_scrape[10];
double bet[10];
double pot[10];
CString button_state[10];
CString i86X_button_state[10];
CString i86_button_state;
CString button_label[10];
double sblind;
double bblind;
double bbet;
double ante;
LimitType limit;
double handnumber;
bool istournament;
};
extern "C" {
SCRAPER_API int ScraperScrape(HWND hwnd, SScraperState *state);
}
我在的Delphi申請聲明相似類型並調用上述功能:
interface
type
LimitType = (NoLimit, PotLimit, FixedLimit);
SScraperState = record
title: Array [0..511] of Char;
card_common: Array [0..4] of Word;
card_player: Array [0..9, 0..1] of Word;
card_player_for_display: Array [0..1] of Word;
dealer: Array [0..9] of Boolean;
sitting_out: Array [0..9] of Boolean;
seated: Array [0..9] of String;
active: Array [0..9] of String;
name: Array [0..9] of String;
balance: Array [0..9] of Double;
name_good_scrape: Array [0..9] of Boolean;
balance_good_scrape: Array [0..9] of Boolean;
bet: Array [0..9] of Double;
pot: Array [0..9] of Double;
button_state: Array [0..9] of String;
i86X_button_state: Array [0..9] of String;
i86_button_state: String;
button_label: Array [0..9] of String;
sblind: Double;
bblind: Double;
bbet: Double;
ante: Double;
limit: LimitType;
handnumber: Double;
istournament: Boolean;
end;
pSScraperState = ^SScraperState;
function ScraperScrape(hWnd: HWND; State: pSScraperState): Integer; cdecl; external 'Scraper.dll';
implementation
var
CurState: SScraperState;
pCurState: pSScraperState;
if ScraperScrape(hWnd, pCurState) = 0 then
...
當函數被調用時,我得到調試例外通知:
項目...引發的異常類EAccessViolation'模塊'Scraper.dll'中地址爲10103F68的消息'訪問衝突。閱讀地址FFFFFFFC'。進程停止。
從同一個DLL導出的其他函數工作正常,所以我的猜測是我在類型聲明中犯了一個錯誤。任何提示將高度讚賞,因爲我死在這一點上。
您可以將調試器附加到您的Scraper.dll文件並找出'ScraperScrape'函數中的哪行代碼觸發異常? – 2009-11-10 00:38:20
您應該顯示SCRAPER_API宏是什麼,以便我們可以檢查您的調用約定是否匹配。 – 2009-11-10 04:08:21
Adam,通過評論/取消註釋C++代碼,我發現問題是由CString變量引起的,我在Delphi代碼中聲明它爲PAnsiChar數組。 – Mikhail 2009-11-10 23:30:00