您的位置:首页 > 编程语言 > Delphi

DELPHI中动态调用DLL的载入片断代码

2009-03-17 09:58 375 查看
function LoadLibs: Boolean;
var prefix : AnsiString;
function connectProc(var ProcAdr:pointer;ProcName:AnsiString):boolean;
begin
ProcAdr := GetProcAddress(DLLHandle, PChar(prefix+ProcName));
if not Assigned(ProcAdr) then
begin
ShowMessage('Could not find method: '+prefix+ProcName);
connectProc := False;
end
else connectProc := True;
end;
begin
Result := False;
DLLHandle := LoadLibrary(SQLITEDLL);
if DLLHandle <> 0 then
begin
Result := true;
//check if sqlite.dll was compiled with borland bcc (recomended!!!)
if GetProcAddress(DLLHandle, PChar('_sqlite_open')) <> nil then
prefix:='_' else prefix:='';

Result := Result and connectProc(@SQLite_Open,   'sqlite_open');
Result := Result and connectProc(@SQLite_Exec,   'sqlite_exec');
Result := Result and connectProc(@SQLite_Close,   'sqlite_close');
Result := Result and connectProc(@SQLite_Version,'sqlite_libversion');
Result := Result and connectProc(@SQLite_Encoding,    'sqlite_libencoding');
Result := Result and connectProc(@SQLite_ErrorString ,'sqlite_error_string');

Result := Result and connectProc(@SQLite_GetTable , 'sqlite_get_table');
Result := Result and connectProc(@SQLite_FreeTable ,'sqlite_free_table');
Result := Result and connectProc(@SQLite_FreeMem ,  'sqlite_freemem');
Result := Result and connectProc(@SQLite_Complete , 'sqlite_complete');

Result := Result and connectProc(@SQLite_LastInsertRow ,'sqlite_last_insert_rowid');
Result := Result and connectProc(@SQLite_Cancel ,'sqlite_interrupt');
Result := Result and connectProc(@SQLite_BusyTimeout ,'sqlite_busy_timeout');
Result := Result and connectProc(@SQLite_BusyHandler ,'sqlite_busy_handler');
Result := Result and connectProc(@SQLite_Changes ,'sqlite_changes');

Result := Result and connectProc(@SQLite_Create_Function ,  'sqlite_create_function');
Result := Result and connectProc(@SQLite_Set_Result_Double ,'sqlite_set_result_double');
Result := Result and connectProc(@SQLite_Set_Result_Int ,   'sqlite_set_result_int');
Result := Result and connectProc(@SQLite_Set_Result_String ,'sqlite_set_result_string');
Result := Result and connectProc(@SQLite_Set_Result_Error , 'sqlite_set_result_error');
Result := Result and connectProc(@SQLite_Function_Type ,    'sqlite_function_type');
end
else ShowMessage('sqlite library not loaded!');
end;


自认为这段代码还是写得挺有意思的。局部函数的引用,减少了很多冗余代码的敲入。result结果的赋值,也看起来比较爽。阅读性还是不错的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: