dll动态调用无返回值,静态调用有返回值,错在哪?(0分)

  • 主题发起人 主题发起人 jsyzzdj
  • 开始时间 开始时间
J

jsyzzdj

Unregistered / Unconfirmed
GUEST, unregistred user!
library systemdata;

uses
ShareMem,
Windows,
SysUtils,
registry,
Comobj,
Messages,
Variants,
Classes,
Unit1 in 'Unit1.pas';


{$R *.res}
Function getlinkstr():string;stdcall;
var
strFileName, strHost, strUserName, strUserPasswd, strDataBase: String;
boolSelect: Boolean;
FileHandle, i: Integer;
Information: array[0..215] of Byte;
ConnectStr: AnsiString;
ComputerName: array[0..255] of char;
nSize: Cardinal;
begin
result:='';
//进行连接前从Connect.ifo中读取连接信息,生成连接字串
strFileName := ExtractFilePath(ParamStr(0))+'Connect.ifo';
if FileExists(strFileName) then
begin
FillChar(Information, SizeOf(Information), $B5);
FileHandle := FileOpen(strFileName, fmOpenReadWrite);
if FileHandle>0 then
begin
FileSeek(FileHandle,0,0);
FileRead(FileHandle, Information, Sizeof(Information));
FileClose(FileHandle);
for i:=0 to 215 do
Information := Information xor strInfo;

strHost := ''; //主机信息
i:=0;
while (i<75) and (Information<>0) do
begin
strHost := strHost + chr(Information);
Inc(i);
end;

if Information[75]=0 then //连接方式
boolSelect := false
else
boolSelect := true;

strUserName := ''; //用户名
i:=76;
while (i<96) and (Information<>0) do
begin
strUserName := strUserName + chr(Information);
Inc(i);
end;

strUserPasswd := '';//密码
i:=96;
while (i<116) and (Information<>0) do
begin
strUserPasswd := strUserPasswd + chr(Information);
Inc(i);
end;

strDataBase := ''; //数据库名称
i:=116;
while (i<139) and (Information<>0) do
begin
strDataBase := strDataBase + chr(Information);
Inc(i);
end;

ConnectStr := 'Provider=SQLOLEDB.1;';
if boolSelect then
ConnectStr := ConnectStr+'Integrated Security=SSPI;'
else if strUserPasswd <> '' then
ConnectStr := ConnectStr+'Password='+strUserPasswd+';';
if (boolSelect = false) and (strUserPasswd <> '') then
ConnectStr := ConnectStr+'Persist Security Info=True;'
else
ConnectStr := ConnectStr+'Persist Security Info=False;';
if boolSelect then
begin
nSize := 255;
GetUserName(ComputerName, nSize);
ConnectStr := ConnectStr+'User ID='+Trim(ComputerName)+';';
end
else
ConnectStr := ConnectStr+'User ID='+strUserName+';';
ConnectStr := ConnectStr+'Initial Catalog='+strDataBase+';';
ConnectStr := ConnectStr+'Data Source='+strHost+';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;';
nSize := 255;
GetComputerName(ComputerName, nSize);
ConnectStr := ConnectStr+'Workstation ID='+Trim(ComputerName)+';Use Encryption for Data=False;Tag with column collation when possible=False';
result:= ConnectStr;
end;
end;

end;
exports
getlinkstr;
begin
end.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
/////////////////////////////////////////////////////
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TGetlinkstr=function :string;stdcall;
TForm1 = class(TForm)
ComboBox1: TComboBox;
Edit1: TEdit;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
//function getlinkstr():string ;stdcall;external 'systemdata.dll';
{$R *.dfm}


procedure TForm1.Button2Click(Sender: TObject);
var
NHandle : THandle;
Getlinkstr:TGetlinkstr;
fpointer:Tfarproc;
begin
NHandle:=LoadLibrary(pchar('systemdata.dll')); //动态载入DLL,并返回其句柄
try
if NHandle <>0 then //如果载入成功则获取函数的地址
fpointer:=GetProcAddress(NHandle,pchar('Getlinkstr'));
if assigned(fpointer) then
begin
Getlinkstr:=TGetlinkstr(fpointer);
edit1.Text:=Getlinkstr;
end;

finally
FreeLibrary(NHandle); //调用完毕收回DLL占用的资源
end;
end;



end.
 
试一下 Function getlinkstr(var p: PChar):string;stdcall;
用传值的方式:)
 
没有用,调用报错
 
调用时也需要use ShareMem
 

Similar threads

I
回复
0
查看
753
import
I
I
回复
0
查看
885
import
I
I
回复
0
查看
780
import
I
I
回复
0
查看
819
import
I
I
回复
0
查看
734
import
I
后退
顶部