Dll得问题?(50分)

  • 主题发起人 zwjChina
  • 开始时间
Z

zwjChina

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我在Dll中定义得一个函数,
直接引用该pas调用没问题!
放在Dll调用却出了问题!不知问题在哪里!

function ASTAddNewUser(UserName: Pchar): Boolean; stdcall;
var
AName: Pchar;
UserCount, i: Integer;
APathFile : string;
F: File;
NameBuffer : array[0..20] of char;
begin
UserCount := ASTUserCount;//不用担心此函数
APathFile := ExtractFilePath(ASTModulePath);//不用担心此函数
if (APathFile <> '') then
begin
APathFile := APathFile + 'UserList.Dat';
AssignFile(F, APathFile);
if FileExists(APathFile) then
begin
Reset(F, 1);
for i := 1 to UserCount do
begin
AName := ASTGetUser(i);
if StrComp(AName, UserName) = 0 then
begin
Result := True;
CloseFile(F);
Exit;
end;
end;
end
else
Rewrite(F, 1);

StrCopy(Pchar(@NameBuffer[1]), UserName);
NameBuffer[0] := Char(StrLen(UserName));
BlockWrite(F, NameBuffer, 21);
Result := True;
CloseFile(F);
end
else
Result := False;
end;
 
具体一些,是在调用时出问题呢还是在退出时出问题
 
调用得时候!
 
type
TDllName = function(UserName: Pchar): Boolean;
var
UserList: TStrings;
DllName: TDllName;
AHandle: THandle;
UN: Pchar;
begin
AHandle := LoadLibrary('Assistant.dll');
if AHandle <> 0 then
begin
@DllName := GetProcAddress(AHandle, 'ASTAddNewUser');
if @DllName <> nil then
begin
UN := 'showit';
DllName(UN); ///此句出错!
end;
FreeLibrary(AHandle);
end
else
begin
MessageDlg('系统无法加载Assistant.dll!', mtError, [mbOK], 0);
Application.Terminate;
end;
inherited;
end;
 
你采用UN:string
DllName(pchar(UN))
试一试??
 
没用,还是错!
 
等一会,我调试一下,我的QQ号码是13490914
 
在生成文件的时候主要是这几行代码导致出错!
我估计主要是第一句!但是一般直接在项目中使用第1句是没错的,问什么呢!
StrCopy(Pchar(@NameBuffer[1]), UserName);
NameBuffer[0] := Char(StrLen(UserName));
BlockWrite(F, NameBuffer, 21);
 
接受答案了.
 
顶部