我有用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