包含数据库访问的DLL(200分)

  • 主题发起人 主题发起人 狄克
  • 开始时间 开始时间

狄克

Unregistered / Unconfirmed
GUEST, unregistred user!
如何创建带有数据库访问的,我创建的DLL在调用时总说没有进行Coinitialize,如何......<br>请高手指教!!<br>200大分!
 
以下是我做madis的一个dll工程文件<br>library server;<br><br>uses<br>&nbsp; ComServ,<br>&nbsp; server_TLB in 'server_TLB.pas',<br>&nbsp; u_mnrs in 'u_mnrs.pas' {mnrs: TRemoteDataModule} {mnrs: CoClass};<br><br>exports<br>&nbsp; DllGetClassObject,<br>&nbsp; DllCanUnloadNow,<br>&nbsp; DllRegisterServer,<br>&nbsp; DllUnregisterServer;<br><br>{$R *.TLB}<br><br>{$R *.RES}<br><br>begin<br><br>end.
 
你在调用该dll时,是不是要初始化一下?
 
保证管用的一着!<br>ADO在DLL中使用时会出现上述问题,D5有,D6仍然有,不知Borland怎么搞的,但有一个办法可以解决,代码如下:<br>在数据模块中建立一个过程,如下:<br>procedure createDM(AHandle: THandle); stdcall;<br>begin<br>&nbsp; Application.Handle := AHandle;<br>&nbsp; try<br>&nbsp; &nbsp; DM := TDM.Create(Application);<br>&nbsp; except<br>&nbsp; &nbsp; on E: Exception do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if E.InheritsFrom(EOleSysError) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Coinitialize(nil); <br>&nbsp; &nbsp; &nbsp; &nbsp; DM := TDM.Create(Application);<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; raise;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>然后在你需要创建数据模块的时候,调用这个过程来创建,如下:<br>&nbsp;createDM(AHandle);<br>顺便说一下,上面的这个AHandle参数是从调用DLL的.EXE中传进来的.EXE的应用程序句柄,至于<br>为什么要这么做,我想大家都知道吧:)<br><br>
 
使用Coinitialize需要引用哪一个单元,我查帮助没有查到,请告诉我<br>多谢了!
 
use ole2;<br><br>
 
我要求DLL内使用包括DATA MODULE及一个显示数据库内容的FORM,<br>在FORM内或在DLL项目中声明函数取得数据库中的内容,不知gxx或其他高手<br>是否有很好的解决办法,如能发出例子本人将非常感激!
 
在主程序中使用CoInitalize(nil)和CoUnInitialize。<br>因为Ado是Com,调用的时候就需要这样做。
 
我做的一个project的例子:<br>library Hawb_Status;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; SysUtils,<br>&nbsp; Ole2,<br>&nbsp; ADODB,<br>&nbsp; U_Input_HStatus in 'U_Input_HStatus.pas' {F_Input_HStatus},<br>&nbsp; U_DM_HStatus in 'U_DM_HStatus.pas' {F_DM_HStatus: TDataModule},<br>&nbsp; U_List_HStatus in 'U_List_HStatus.pas' {F_List_HStatus},<br>&nbsp; U_SaveLoadF in 'U_SaveLoadF.pas' {F_SaveLoadF},<br>&nbsp; U_SaveLoadIni in 'U_SaveLoadIni.pas' {F_SaveLoadIni},<br>&nbsp; U_Search_HStatus in 'U_Search_HStatus.pas' {F_Search_HStatus},<br>&nbsp; U_ListDef_HStatus in 'U_ListDef_HStatus.pas' {F_ListDef_HStatus},<br>&nbsp; U_SHawb_Hint in 'U_SHawb_Hint.pas' {F_SHawb_Hint},<br>&nbsp; hawblistf in 'hawblistf.pas' {ListPreview},<br>&nbsp; u_hstatus_dest in 'u_hstatus_dest.pas' {F_HStatus_dest};<br><br>{$R *.RES}<br><br>function MainRunning(AUser: PChar; Privilege: Integer; WorkPath: PChar;<br>&nbsp; &nbsp; Conn: TADOConnection; TableName, KeyFld: PChar): Boolean; stdcall;<br><br>begin<br>&nbsp; ShortDayNames[1]:='Sun';<br>&nbsp; ShortDayNames[2]:='Mon';<br>&nbsp; ShortDayNames[3]:='Tue';<br>&nbsp; ShortDayNames[4]:='Wed';<br>&nbsp; ShortDayNames[5]:='Thu';<br>&nbsp; ShortDayNames[6]:='Fri';<br>&nbsp; ShortDayNames[7]:='Sat';<br>&nbsp; LongDayNames[1]:='Sun';<br>&nbsp; LongDayNames[2]:='Mon';<br>&nbsp; LongDayNames[3]:='Tue';<br>&nbsp; LongDayNames[4]:='Wed';<br>&nbsp; LongDayNames[5]:='Thu';<br>&nbsp; LongDayNames[6]:='Fri';<br>&nbsp; LongDayNames[7]:='Sat';<br>&nbsp; _LoginUsr := StrPas(AUser);<br>&nbsp; _TableName := TableName; &nbsp;<br>&nbsp; _UserPrivilege := Privilege;<br>&nbsp; if WorkPath = nil then<br>&nbsp; &nbsp; _WorkPath := ''<br>&nbsp; else<br>&nbsp; &nbsp; _WorkPath := StrPas(WorkPath);<br>&nbsp; CoInitialize(nil);<br>&nbsp; F_DM_HStatus := TF_DM_HStatus.Create(Application);<br>&nbsp; F_DM_HStatus.MainTable.KeyFldName := StrPas(KeyFld);<br>&nbsp; F_DM_HStatus.MainTable.Connection := Conn;<br>&nbsp; F_DM_HStatus.LookupQry.Connection := Conn;<br>&nbsp; F_DM_HStatus.ExecCommand.Connection := Conn;<br>&nbsp; F_DM_HStatus.adodataset1.connection := Conn;<br>&nbsp; F_DM_HStatus.adostoredproc1.connection := conn;<br>&nbsp; F_Input_HStatus := TF_Input_HStatus.Create(Application);<br>&nbsp; F_Search_HStatus := TF_Search_HStatus.Create(Application);<br>&nbsp; F_List_HStatus := TF_List_HStatus.Create(Application);<br>&nbsp; F_ListDef_HStatus := TF_ListDef_HStatus.Create(Application);<br>&nbsp; F_SaveLoadF := TF_SaveLoadF.Create(Application);<br>&nbsp; F_SaveLoadIni := TF_SaveLoadIni.Create(Application);<br>&nbsp; F_SHawb_Hint :=TF_SHawb_Hint.Create(Application);<br>&nbsp; lstpreview := tlstpreview.create(application);<br>&nbsp; f_hstatus_dest := tf_hstatus_dest.Create(application);<br>&nbsp; f_input_hstatus.showmodal;<br>&nbsp; FreeAndNil(F_Input_HStatus);<br><br>&nbsp; FreeAndNil(F_Search_HStatus);<br>&nbsp; FreeAndNil(F_List_HStatus);<br>&nbsp; FreeAndNil(F_ListDef_HStatus);<br>&nbsp; FreeAndNil(F_SaveLoadF);<br>&nbsp; FreeAndNil(F_SaveLoadIni);<br>&nbsp; FreeAndNil(F_SHawb_Hint);<br>&nbsp; FreeAndNil(F_DM_HStatus);<br>&nbsp; coUnInitialize;<br>&nbsp; Result := True;<br>end;<br><br>exports<br>&nbsp; MainRunning name 'MainRunning';<br><br>end.
 
还没时间测试,测试后尽快发分
 
关注这个问题
 
多人接受答案了。
 
后退
顶部