以下代码应该可以(注:Edit1为IP或计算机名,Edit2为用户名,Edit3为密码);
procedure CallNetExtError;
var
ErrorCode: Cardinal;
ErrBuf,NameBuf: string;
ShowMsg: string;
begin
SetLength(ErrBuf,MAX_PATH);
SetLength(NameBuf,MAX_PATH);
if WNetGetLastError(ErrorCode,PChar(ErrBuf),MAX_PATH+1,PChar(NameBuf),MAX_PATH+1) = NO_ERROR then
begin
ShowMsg := 'Error Code:' + IntToStr(ErrorCode) + #13#10;
ShowMsg := ShowMsg + 'Error String:' + ErrBuf + #13#10;
ShowMsg := ShowMsg + 'Error Provider:' + NameBuf;
ShowMessage(ShowMsg);
end
else ShowMessage('Invalid Buffer Size! :-(');
end;
procedure TfrmMain.Button4Click(Sender: TObject);
var
NR: NETRESOURCE;
Ret: DWORD;
S: string;
begin
S:=pchar('//'+Edit1.Text);
NR.dwType:=RESOURCETYPE_ANY;
NR.lpLocalName:=NIL;
NR.lpRemoteName:=pchar(S);
NR.lpProvider:=NIL;
RET:=WNetAddConnection2(NR,PChar(edit3.Text),PChar(edit2.Text),CONNECT_UPDATE_PROFILE);
if Ret <> NO_ERROR then
begin
if Ret <> ERROR_EXTENDED_ERROR then RaiseLastWin32Error
else CallNetExtError;
end
else begin
Application.MessageBox('LOGIN SUCCESSFULLY','INFORMATION',MB_OK or MB_ICONINFORMATION);
end;
end;