📄 如何在c++ builder的dll中使用数据库控件.htm
字号:
<TR>
<TD bgColor=#ffffff height=2></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=titleback1>相关文章</TD></TR>
<TR>
<TD vAlign=top height=100>· <A
title="How To Get Application Path,And How To Get The Paths Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径"
href="http://www.malegebide.com/article/1/14/2006/2006092518487.html">How
To Get Applica..</A><BR>· <FONT color=#0066ff>[图文]</FONT> <A
title="Rational Robot如何测试帮助c++程序员识别自定义或第三方控件"
href="http://www.malegebide.com/article/1/14/2006/2006092518453.html">Rational
Rob..</A><BR>· <FONT color=#0066ff>[图文]</FONT> <A
title="Delphi 7 中如何使用 Crystal Report 9.2(三、利用 Crystal Report 9 VCL 编程)"
href="http://www.malegebide.com/article/1/14/2006/2006092518419.html">Delphi
7 中如..</A><BR>· <A
title="如何根据驱动程序及其安装文件来访问该设备呢(Visual C++ or C++ Builder均可以)"
href="http://www.malegebide.com/article/1/14/2006/2006092518299.html">如何根据驱动程序及..</A><BR>·
<A title="在C++ Builder中如何通过对象名称来访问到该对象呢?答案就是使用MAP容器。"
href="http://www.malegebide.com/article/1/14/2006/2006092518231.html">在C++
Builder中如何..</A><BR>· <A
title="Windows C++ 程序员如何过度到Symbian OS C++ 程序员?"
href="http://www.malegebide.com/article/1/14/2006/2006092518219.html">Windows
C++ 程序员..</A><BR>· <A title="如何运行C++ STL程序——morning小品文"
href="http://www.malegebide.com/article/1/14/2006/2006092518214.html">如何运行C++
STL程序..</A><BR>· <A title="C++ Builder 高手进阶 (七)如何设计系统环境监控程序"
href="http://www.malegebide.com/article/1/14/2006/2006092518212.html">C++
Builder 高手进..</A><BR>· <A
title=在C++Builder中如何用“TCppWebBrowser”POST数据
href="http://www.malegebide.com/article/1/14/2006/2006092518199.html">在C++Builder中如何..</A><BR>·
<A title=C++中如何使类不能继承
href="http://www.malegebide.com/article/1/14/2006/2006092518148.html">C++中如何使类不能继..</A><BR></TD></TR></TBODY></TABLE></TD>
<TD class=tableright align=right width=588>
<TABLE style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" cellSpacing=0
cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD style="FONT-SIZE: 12pt; COLOR: #ff6600" align=middle
height=32>如何在C++ Builder的DLL中使用数据库控件</TD></TR>
<TR>
<TD align=middle bgColor=#f7f7f7
height=25>作者:佚名 来源:不详 发布时间:25/09/2006
20:01:33 发布人:admin</TD></TR>
<SCRIPT language=javascript>
var newasp_fontsize=9;
var newasp_lineheight=12;
</SCRIPT>
<TR>
<TD>
<P align=right><A title=减小字体
style="CURSOR: hand; POSITION: relative"
onclick='if(newasp_fontsize>8){NewaspContentLabel.style.fontSize=(--newasp_fontsize)+"pt";NewaspContentLabel.style.lineHeight=(--newasp_lineheight)+"pt";}'><IMG
height=15 src="如何在C++ Builder的DLL中使用数据库控件.files/1.gif" width=15
border=0><FONT color=#ff6600>减小字体</FONT></A> <A title=增大字体
style="CURSOR: hand; POSITION: relative"
onclick='if(newasp_fontsize<64){NewaspContentLabel.style.fontSize=(++newasp_fontsize)+"pt";NewaspContentLabel.style.lineHeight=(++newasp_lineheight)+"pt";}'><IMG
height=15 src="如何在C++ Builder的DLL中使用数据库控件.files/2.gif" width=15
border=0><FONT color=#ff6600>增大字体</FONT></A> </P>
<DIV class=ContentFont id=NewaspContentLabel
style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"><FONT
id=font_word
style="FONT-SIZE: 14px; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif">
<P><FONT size=4>使用方法与平常的使用办法一样,不过要注意几个地方:</FONT></P>
<P><FONT
size=4>1、TSession及TDatabase要加上或者是用new生成实例,平时可能不会加TSession,但在DLL中一定要这样做,不然在启动或者退出时会出错。</FONT></P>
<P><FONT
size=4>2、在导出DLL的函数中,一定生成DataModule的实例,不然连接数据库时会有问题的。</FONT></P>
<P><FONT
size=4>//---------------------------------------------------------------------------</FONT></P>
<P><FONT size=4>#include <VCL.H><BR>#include <<A class=wordstyle
href="http://www.malegebide.com/"
target=_blank>WINDOWS</A>.H><BR>#include "Unit2.h"<BR>#include
"Unit4.h"</FONT></P>
<P><FONT size=4>#pragma hdrstop</FONT></P>
<P><FONT size=4>#pragma argsused<BR>BOOL WINAPI DllMain(HINSTANCE
hinstDLL, DWORD fwdreason, LPVOID lpvReserved)<BR>{<BR>? return
1;<BR>}<BR>//---------------------------------------------------------------------------</FONT></P>
<P><FONT size=4>extern "C"? __declspec(dllexport) void
ShowForm();</FONT></P>
<P><FONT size=4>void ShowForm()<BR>{<BR>???? TForm2 *aTForm2 = new
TForm2(NULL);<BR>???? TDataModule4 *aTDataModule4 = new
TDataModule4(aTForm2);<BR>???? aTForm2->ShowModal();<BR>????
delete aTDataModule4;if(aTDataModule4) aTDataModule4 = NULL;<BR>????
delete aTForm2; if (aTForm2) aTForm2 = NULL;<BR>}</FONT></P>
<P><FONT size=4>调用</FONT></P>
<P><BR>void __fastcall TForm3::Button1Click(TObject
*Sender)<BR>{<BR>? HINSTANCE Dll =
LoadLibrary(".\\Project1.dll");</P>
<P>???? if (Dll)<BR>???? {<BR>???????? LoadFunction = (ShowType
*)GetProcAddress(Dll, "_ShowForm");<BR>???????? if
(LoadFunction)<BR>???????????? LoadFunction();<BR>????????
else<BR>????????????
ShowMessage(SysErrorMessage(GetLastError()));<BR>????????
FreeLibrary(Dll);<BR>???? }</P>
<P>???? else<BR>???? {<BR>????????
ShowMessage(SysErrorMessage(GetLastError()));<BR>????????
ShowMessage("Unable to load the Dll");<BR>???? }</P>
<P>}<BR>//---------------------------------------------------------------------------<BR></P>
<P>不能上传zip文件,<FONT
id=ojabqdmiibso>yhAP,=}iva3-</FONT>没有办法将整个工程传上。</P>
<P>USEFORM("Unit2.cpp", Form2);<BR>USEFORM("Unit4.cpp",
DataModule4); /* TDataModule: File Type
*/<BR>//---------------------------------------------------------------------------</P>
<P>#include <VCL.H><BR>#pragma
hdrstop<BR>//---------------------------------------------------------------------------<BR>USEFORM("Unit3.cpp",
Form3);<BR>//---------------------------------------------------------------------------<BR>WINAPI
WinMain(HINSTANCE, HINSTANCE, LPSTR, int)<BR>{<BR>? try<BR>?
{<BR>???? Application->Initialize();<BR>????
Application->CreateForm(__classid(TForm3), &Form3);<BR>????
Application->Run();<BR>? }<BR>? catch (Exception
&exception)<BR>? {<BR>????
Application->ShowException(&exception);<BR>? }<BR>? catch
(...)<BR>? {<BR>???? try<BR>???? {<BR>?????? throw
Exception("");<BR>???? }<BR>???? catch (Exception
&exception)<BR>???? {<BR>??????
Application->ShowException(&exception);<BR>???? }<BR>? }<BR>?
return
0;<BR>}<BR>//---------------------------------------------------------------------------<BR>#------------------------------------------------------------------------------<BR>VERSION
=
BWS.01<BR>#------------------------------------------------------------------------------<BR>!ifndef
ROOT<BR>ROOT =
$(MAKEDIR)\..<BR>!endif<BR>#------------------------------------------------------------------------------<BR>MAKE
= $(ROOT)\bin\make.exe -$(MAKEFLAGS) -f$**<BR>DCC =
$(ROOT)\bin\dcc32.exe $**<BR>BRCC = $(ROOT)\bin\brcc32.exe
$**<BR>#------------------------------------------------------------------------------<BR>PROJECTS
= Project1.dll
Project2.exe<BR>#------------------------------------------------------------------------------<BR>default:
$(PROJECTS)<BR>#------------------------------------------------------------------------------</P>
<P>Project1.dll: Project1.bpr<BR>? $(ROOT)\bin\bpr2mak $**<BR>?
$(ROOT)\bin\make -$(MAKEFLAGS) -f$*.mak</P>
<P>Project2.exe: Project2.bpr<BR>? $(ROOT)\bin\bpr2mak $**<BR>?
$(ROOT)\bin\make -$(MAKEFLAGS) -f$*.mak</P>
<P><BR>//---------------------------------------------------------------------------</P>
<P>#include <VCL.H><BR>#include <<A class=wordstyle
href="http://www.malegebide.com/"
target=_blank>WINDOWS</A>.H><BR>#include "Unit2.h"<BR>#include
"Unit4.h"</P>
<P>#pragma
hdrstop<BR>//---------------------------------------------------------------------------<BR>//??
Important note about DLL memory management when your DLL uses
the<BR>//?? static version of the RunTime Library:<BR>//<BR>//?? If
your DLL exports any functions that pass String objects (or
structs/<BR>//?? classes containing nested Strings) as parameter or
function results,<BR>//?? you will need to add the library
MEMMGR.LIB to both the DLL project and<BR>//?? any other projects
that use the DLL.? You will also need to use MEMMGR.LIB<BR>//?? if
any other projects which use the DLL will be performing new or
delete<BR>//?? operations on any non-TObject-derived classes which
are exported from the<BR>//?? DLL. Adding MEMMGR.LIB to your project
will change the DLL and its calling<BR>//?? EXE''''s to use the
BORLNDMM.DLL as their memory manager.? In these cases,<BR>//?? the
file BORLNDMM.DLL should be deployed along with your
DLL.<BR>//<BR>//?? To avoid using BORLNDMM.DLL, pass string
information using "char *" or<BR>//?? ShortString
parameters.<BR>//<BR>//?? If your DLL uses the dynamic version of
the RTL, you do not need to<BR>//?? explicitly add MEMMGR.LIB as
this will be done implicitly for
you<BR>//---------------------------------------------------------------------------</P>
<P>#pragma argsused<BR>BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD
fwdreason, LPVOID lpvReserved)<BR>{<BR>? return
1;<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>extern "C"? __declspec(dllexport) void ShowForm();</P>
<P>void ShowForm()<BR>{<BR>???? TForm2 *aTForm2 = new
TForm2(NULL);<BR>???? TDataModule4 *aTDataModule4 = new
TDataModule4(aTForm2);<BR>???? aTForm2->ShowModal();<BR>????
delete aTDataModule4;if(aTDataModule4) aTDataModule4 = NULL;<BR>????
delete aTForm2; if (aTForm2) aTForm2 = NULL;<BR>}</P>
<P>//---------------------------------------------------------------------------</P>
<P>#include <VCL.H><BR>#pragma hdrstop</P>
<P>#include "Unit2.h"<BR>#include
"Unit4.h"<BR>//---------------------------------------------------------------------------<BR>#pragma
package(smart_init)<BR>#pragma resource "*.dfm"<BR>TForm2
*Form2;<BR>//---------------------------------------------------------------------------<BR>__fastcall
TForm2::TForm2(TComponent* Owner)<BR>? :
TForm(Owner)<BR>{<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>void __fastcall TForm2::Button1Click(TObject *Sender)<BR>{<BR>?
//DataModule4->dllTestDB->Connected = true;//不能这样用</P>
<P>?MessageDlg("DLL窗口测试程序中的按钮!!",mtWarning,TMsgDlgButtons() <<
mbYes <<
mbNo,0);<BR>}<BR>//---------------------------------------------------------------------------<BR>object
Form2: TForm2<BR>? Left = 209<BR>? Top = 243<BR>? Width = 759<BR>?
Height = 426<BR>? Caption = ''''Form2''''<BR>? Color =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -