关于DLL的问题迫切请教高手解决!!!!!!(0分)

Q

ql

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥晚上好,小弟我现在正在学习DLL文件的编写和调用,主要是想以后的代码重用。<br>但现在碰到好多问题急待解决,望热心的朋友给以帮助,万分感谢!!!<br>我写了段代码如下请帮忙修改:<br>1、DLL部分源码:<br>library My;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Unit1 in 'Unit1.pas';<br>exports<br>&nbsp; MyM;<br>{$R *.res}<br>begin<br>end.<br><br>unit1部分源码:<br>unit Unit1;<br>interface<br>uses<br>&nbsp; SysUtils, Classes, Dialogs;<br>&nbsp; procedure MyM(var a,b : string);export;<br>implementation<br>{$R *.dfm}<br>procedure MyM(var a,b : string);<br>begin<br>&nbsp; //此处我想把程序中的字符串变量传入到此处!<br>&nbsp; if (a &lt;&gt; '') and (b &lt;&gt; '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MessageDlg(a,mtConfirmation, [mbYes], 0);<br>&nbsp; &nbsp; &nbsp; MessageDlg(b,mtConfirmation, [mbYes], 0);<br>&nbsp; &nbsp; &nbsp; a := 'a传递成功';<br>&nbsp; &nbsp; &nbsp; b := 'b传递成功';<br>&nbsp; &nbsp; &nbsp; //此处想将a,b变量的新值传递出来!<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; MessageDlg('DLL参数动态传递失败!',mtConfirmation, [mbYes], 0);<br>end;<br>end.<br><br>2、动态调用DLL程序部分,请帮忙修改<br>program Project1;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Forms,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br>{$R *.res}<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.<br><br>unit部分源码:<br>unit Unit1;<br>interface<br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; TMultiplyNum = procedure;<br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; MyMultiplyNum : TMultiplyNum;<br>&nbsp; MyHandle : THandle;<br>&nbsp; str1,str2 : string;<br>implementation<br>{$R *.dfm}<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if (edit1.Text = '') or (edit2.Text = '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('请输入信息!');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; str1 := edit1.Text;<br>&nbsp; &nbsp; &nbsp; str2 := edit2.Text;<br>&nbsp; &nbsp; &nbsp; //想将str1,str2变量传入My.dll中不知道该怎么做?<br>&nbsp; &nbsp; &nbsp; //请高手指点迷津<br>&nbsp; &nbsp; &nbsp; MyHandle := LoadLibrary('My.dll');<br>&nbsp; &nbsp; &nbsp; if MyHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @MyMultiplyNum := GetProcAddress(MyHandle,'MyM');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (@MyMultiplyNum) &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TMultiplyNum(MyMultiplyNum);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeLibrary(MyHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //此处想将My.dll中的变量传递出来再附给str1和str2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //不知道该如何做,请高手指点迷津<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; application.MessageBox('DLL文件部存在!','系统信息',16);<br>&nbsp; &nbsp; end;<br>end;<br>end.<br>但以上程序运行时总报错,我想是参数传递出的问题<br>若不传递变量则没问题,请教高手帮忙修改<br>特别焦急,万分感谢!!!!!!<br>
 
{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }
 
我已经将delphimm.dll拷贝到该目录下了<br>上面的这段我将它给删除了
 
uses<br>&nbsp; ShareMem; //attention<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Unit1 in 'Unit1.pas'
 
用pChar型作参数
 
还是不行啊,ShareMem使用静态调用时没问题的
 
我想的程序是在参数传递方面出的问题,<br>请问动态调用DLL文件时如何将变量传入和传出???
 
type<br>&nbsp; TMultiplyNum = procedure (var a,b : string);
 
zw84611你好,我长见识了,非常感谢<br>请帮忙改一下,万分感谢!!!<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if (edit1.Text = '') or (edit2.Text = '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('请输入信息!');exit;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; str1 := edit1.Text; str2 := edit2.Text;<br>&nbsp; &nbsp; &nbsp; MyHandle := LoadLibrary('My.dll');<br>&nbsp; &nbsp; &nbsp; if MyHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @MyMultiplyNum := GetProcAddress(MyHandle,'MyM');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (@MyMultiplyNum) &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TMultiplyNum(MyMultiplyNum);//为什么这句报错,无法通过???<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //我想再得到经过DLL处理的a,b变量!<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //请问错在那里?如何修改?请指教!!!!!!<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeLibrary(MyHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; application.MessageBox('DLL文件部存在!','系统信息',16);<br>&nbsp; &nbsp; end;<br>end;<br>
 
fait...<br><br>a,b: string;<br>if (@MyMultiplyNum) &lt;&gt; nil then MyMultiplyNum(a,b);<br><br>
 
zw84611成功了,万分感谢,你帮我解决了个大难题,太谢谢了,我爱你。。。。。。。。
 
顶部