D
ddev
Unregistered / Unconfirmed
GUEST, unregistred user!
问题:关于 DLL 的调用问题<br><br>我在 VC5 中做了一个关于 ACCESS 的处理函数。但在 Delphi 中调用时,却总出现外部调用异常。<br>能指导一下吗?<br><br>daoCreateDatabase()调用正常;<br>但daoCreateTable()就出错,问题可能出现在 DAOFIELDDEF** 的调用参数上。对于这种指向数组的<br>指针,不知在 Delphi 中应该处理?<br><br>源代码如下:<br><br>//****************************************************************************************//<br>VC5 DLL<br>//****************************************************************************************//<br>#include <afxdao.h><br><br>typedef struct tagDaoFieldDef{<br><br> char fldName[40];<br> int fldType;<br> int fldSize;<br> bool fldRequired;<br> bool fldAllowZeroLength;<br><br>}DAOFIELDDEF;<br><br>extern "C"{<br><br> __declspec(dllexport) bool __stdcall daoCreateDatabase(LPCTSTR);<br> __declspec(dllexport) bool __stdcall daoCreateTable(LPCTSTR, LPCTSTR, DAOFIELDDEF**, int);<br>};<br><br>//-----------------------------------------------------------------------------------------//<br>#include "DaoAPI.h"<br><br>extern "C"{<br><br> __declspec(dllexport) bool __stdcall daoCreateDatabase(LPCTSTR lpszDatabaseName)<br> {<br> bool bSuccess = FALSE;<br> CDaoDatabase* lpDatabase = new CDaoDatabase();<br> try{<br><br> lpDatabase->Create(lpszDatabaseName);<br> if (lpDatabase->IsOpen()) lpDatabase->Close();<br> bSuccess = TRUE;<br> }<br> catch(CDaoException& E){<br> bSuccess = FALSE;<br> E.Delete();<br> }<br> catch(CMemoryException & E){<br> bSuccess = FALSE;<br> E.Delete();<br> } <br> delete lpDatabase;<br> lpDatabase = NULL;<br><br> return bSuccess;<br> }<br><br> __declspec(dllexport) bool __stdcall daoCreateTable(LPCTSTR lpszDatabaseName, <br> LPCTSTR lpszTableName, DAOFIELDDEF** lpFieldDefs, int nCount)<br> {<br> bool bSuccess = FALSE;<br> CDaoDatabase* lpDatabase = new CDaoDatabase();<br> try{<br><br> lpDatabase->Open(lpszDatabaseName);<br><br> CDaoTableDef* lpTableDef = new CDaoTableDef(lpDatabase);<br> CDaoFieldInfo FieldInfo;<br> try{<br> lpTableDef->Create(lpszTableName);<br> for(int i = 0; i < nCount; i++)<br> {<br> DAOFIELDDEF* lpFieldDef = (DAOFIELDDEF*)(lpFieldDefs);<br> if (lpFieldDef != NULL)<br> {<br> FieldInfo.m_strName = lpFieldDef->fldName;<br> FieldInfo.m_nType = lpFieldDef->fldType;<br> FieldInfo.m_lSize = lpFieldDef->fldSize;<br> FieldInfo.m_bRequired = lpFieldDef->fldRequired;<br> FieldInfo.m_bAllowZeroLength = lpFieldDef->fldAllowZeroLength;<br> <br> lpTableDef->CreateField(FieldInfo);<br> }<br> }<br><br> lpTableDef->Append();<br> bSuccess = TRUE;<br> }<br> catch(CDaoException& E){<br> bSuccess = FALSE;<br> E.Delete();<br> }<br> catch(CMemoryException & E){<br> bSuccess = FALSE;<br> E.Delete();<br> } <br> lpTableDef->Close();<br><br> delete lpTableDef;<br> lpTableDef = NULL;<br> }<br> catch(CDaoException& E){<br> bSuccess = FALSE;<br> E.Delete();<br> }<br> catch(CMemoryException & E){<br> bSuccess = FALSE;<br> E.Delete();<br> } <br> delete lpDatabase;<br> lpDatabase = NULL;<br><br> return bSuccess;<br> }<br>}<br>//-----------------------------------------------------------------------------------------//<br><br>//****************************************************************************************//<br>Delphi Call<br>//****************************************************************************************//<br>unit DaoAPI;<br><br>interface<br><br>uses<br> windows;<br><br>const<br> dbBoolean = 1;<br> dbByte = 2;<br> dbInteger = 3;<br> dbLong = 4;<br> dbCurrency = 5;<br> dbSingle = 6;<br> dbDouble = 7;<br> dbDate = 8;<br> dbBinary = 9;<br> dbText = 10;<br> dbLongBinary = 11;<br> dbMemo = 12;<br> dbGUID = 15;<br> dbBigInt = 16;<br> dbVarBinary = 17;<br> dbChar = 18;<br> dbNumeric = 19;<br> dbDecimal = 20;<br> dbFloat = 21;<br> dbTime = 22;<br> dbTimeStamp = 23;<br><br>type<br> PDaoFieldDef = ^TDaoFieldDef;<br> TDaoFieldDef = record<br> fldName: string[40];<br> fldType: Longint;<br> fldSize: Longint;<br> fldRequired: Boolean;<br> fldAllowZeroLength: Boolean;<br> end;<br><br> PDaoFieldDefArr = ^TDaoFieldDefArr;<br> TDaoFieldDefArr = array of PDaoFieldDef;<br><br>function daoCreateDatabase(lpszDatabaseName: LPCTSTR): Boolean; stdcall;<br>function daoCreateTable(lpszDatabaseName: LPCTSTR; lpszTableName: LPCTSTR;<br> lpFieldDefList: PDaoFieldDefArr; nCount: Longint): Boolean; stdcall;<br><br>implementation<br> <br>function daoCreateDatabase; stdcall; external 'DaoAPI.dll';<br>function daoCreateTable; stdcall; external 'DaoAPI.dll';<br><br>end.<br><br>//-----------------------------------------------------------------------------------------//<br>unit DemoFrm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TDemoForm = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> Edit2: TEdit;<br> ResultEdit: TEdit;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> DemoForm: TDemoForm;<br><br>implementation<br><br>uses DaoAPI, db;<br><br>{$R *.DFM}<br><br>procedure TDemoForm.Button1Click(Sender: TObject);<br>var<br> bSuccess: Boolean;<br>begin<br> bSuccess := daoCreateDatabase(PChar(Edit1.Text));<br> if bSuccess then<br> ResultEdit.Text := 'Success' else<br> ResultEdit.Text := 'Fail!';<br>end;<br><br>procedure TDemoForm.Button2Click(Sender: TObject);<br>var<br> bSuccess: Boolean;<br> FieldDefs: PDaoFieldDefArr;<br>begin<br> FieldDefs := New(PDaoFieldDefArr);<br> SetLength(FieldDefs^, 4);<br><br> FieldDefs^[0] := New(PDaoFieldDef);<br> FieldDefs^[0].fldName := '编号';<br> FieldDefs^[0].fldType := dbText;<br> FieldDefs^[0].fldSize := 16;<br> FieldDefs^[0].fldRequired := False;<br> FieldDefs^[0].fldAllowZeroLength := True;<br><br> FieldDefs^[1] := New(PDaoFieldDef);<br> FieldDefs^[1].fldName := '名称';<br> FieldDefs^[1].fldType := dbText;<br> FieldDefs^[1].fldSize := 80;<br> FieldDefs^[1].fldRequired := False;<br> FieldDefs^[1].fldAllowZeroLength := True;<br><br> FieldDefs^[2] := New(PDaoFieldDef);<br> FieldDefs^[2].fldName := '年龄';<br> FieldDefs^[2].fldType := dbLong;<br> FieldDefs^[2].fldSize := 0;<br> FieldDefs^[2].fldRequired := False;<br> FieldDefs^[2].fldAllowZeroLength := False;<br><br> FieldDefs^[3] := New(PDaoFieldDef);<br> FieldDefs^[3].fldName := '备注';<br> FieldDefs^[3].fldType := dbMemo;<br> FieldDefs^[3].fldSize := 10;<br> FieldDefs^[3].fldRequired := False;<br> FieldDefs^[3].fldAllowZeroLength := True;<br><br> bSuccess := daoCreateTable(PChar(Edit1.Text), PChar(Edit2.Text), FieldDefs, 4);<br> if bSuccess then<br> ResultEdit.Text := 'Success' else<br> ResultEdit.Text := 'Fail!';<br><br>end;<br><br>end.<br><br>//-----------------------------------------------------------------------------------------//<br>