如何在Windows 7上从Inno Setup安装程序执行“net use”命令?

时间:2012-10-23 07:12:05

标签: windows-7 inno-setup uac net-use

我正在使用Inno Setup安装程序,该安装程序调用{​​{1}}以连接到共享服务器。安装程序可以连接到服务器,如果它在Windows XP上运行,但不在Windows 7上运行。我认为它与UAC有关,因为我键入相同的命令,服务器在Windows 7上连接,但安装程序正在运行{{ 1}}特权。

我通过net useadmin脚本函数使用以下net use命令:

Exec

实际上,这是显示ShellExec命令调用的脚本的一部分:

/c net use \\servername password /user:username

有人可以建议如何在Windows 7上使用Inno Setup中的net use吗?我们只想连接到服务器并让用户输入名称和密码。

谢谢!

1 个答案:

答案 0 :(得分:5)

如何连接到调用凭据对话框的远程资源?

在您的问题上使用不同的视图,这实际上就是此答案的标题所示,我建议您使用带有CONNECT_INTERACTIVECONNECT_PROMPT标记的WNetUseConnection函数调用。这将结合空的用户ID和密码参数调用凭证对话框(这就是你想要的)。在Inno Setup脚本中,它可能如下所示:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif
const
  NO_ERROR = 0;
  ERROR_ACCESS_DENIED = 5;
  ERROR_BAD_NET_NAME = 67;
  ERROR_ALREADY_ASSIGNED = 85;
  ERROR_INVALID_PASSWORD = 86;
  ERROR_INVALID_PARAMETER = 87;
  ERROR_MORE_DATA = 234;
  ERROR_NO_MORE_ITEMS = 259;
  ERROR_INVALID_ADDRESS = 487;
  ERROR_BAD_DEVICE = 1200;
  ERROR_NO_NET_OR_BAD_PATH = 1203;
  ERROR_BAD_PROVIDER = 1204;
  ERROR_EXTENDED_ERROR = 1208;
  ERROR_NO_NETWORK = 1222;
  ERROR_CANCELLED = 1223;
  RESOURCETYPE_ANY = $00000000;
  RESOURCETYPE_DISK = $00000001;
  RESOURCETYPE_PRINT = $00000002;
  CONNECT_UPDATE_PROFILE = $00000001;
  CONNECT_INTERACTIVE = $00000008;
  CONNECT_PROMPT = $00000010;
  CONNECT_REDIRECT = $00000080;
  CONNECT_COMMANDLINE = $00000800;
  CONNECT_CMD_SAVECRED = $00001000;
type
  TNetResource = record
    dwScope: DWORD;
    dwType: DWORD;
    dwDisplayType: DWORD;
    dwUsage: DWORD;
    lpLocalName: string;
    lpRemoteName: string;
    lpComment: string;
    lpProvider: string;
  end;
  TResourceType = (
    rtAny,
    rtDisk,
    rtPrinter
  );

function WNetUseConnection(hwndOwner: HWND; const lpNetResource: TNetResource;
  lpPassword, lpUserID: string; dwFlags: DWORD; lpAccessName: PAnsiChar;
  var lpBufferSize, lpResult: DWORD): DWORD;
  external 'WNetUseConnection{#AW}@mpr.dll stdcall';

function UseConnection(const ARemoteName: string; 
  AResourceType: TResourceType): DWORD;
var
  BufferSize: DWORD;
  ResultFlag: DWORD;
  NetResource: TNetResource;
begin
  case AResourceType of
    rtAny: NetResource.dwType := RESOURCETYPE_ANY;
    rtDisk: NetResource.dwType := RESOURCETYPE_DISK;
    rtPrinter: NetResource.dwType := RESOURCETYPE_PRINT;
  end;
  NetResource.lpLocalName := '';
  NetResource.lpRemoteName := ARemoteName;
  NetResource.lpProvider := '';  
  BufferSize := 0;
  Result := WNetUseConnection(WizardForm.Handle, NetResource,
    '', '', CONNECT_INTERACTIVE or CONNECT_PROMPT, '',
    BufferSize, ResultFlag); 
end;

procedure UseConnectionButtonClick(Sender: TObject);
var
  S: string;
  ResultCode: DWORD;
begin
  ResultCode := UseConnection('\\MySuperSecret\Place', rtDisk);
  case ResultCode of
    NO_ERROR: S := 'NO_ERROR';
    ERROR_ACCESS_DENIED: S := 'ERROR_ACCESS_DENIED';
    ERROR_ALREADY_ASSIGNED: S := 'ERROR_ALREADY_ASSIGNED';
    ERROR_BAD_DEVICE: S := 'ERROR_BAD_DEVICE';
    ERROR_BAD_NET_NAME: S := 'ERROR_BAD_NET_NAME';
    ERROR_BAD_PROVIDER: S := 'ERROR_BAD_PROVIDER';
    ERROR_CANCELLED: S := 'ERROR_CANCELLED';
    ERROR_EXTENDED_ERROR: S := 'ERROR_EXTENDED_ERROR';
    ERROR_INVALID_ADDRESS: S := 'ERROR_INVALID_ADDRESS';
    ERROR_INVALID_PARAMETER: S := 'ERROR_INVALID_PARAMETER';
    ERROR_MORE_DATA: S := 'ERROR_MORE_DATA';
    ERROR_INVALID_PASSWORD: S := 'ERROR_INVALID_PASSWORD';
    ERROR_NO_MORE_ITEMS: S := 'ERROR_NO_MORE_ITEMS';
    ERROR_NO_NET_OR_BAD_PATH: S := 'ERROR_NO_NET_OR_BAD_PATH';
    ERROR_NO_NETWORK: S := 'ERROR_NO_NETWORK';
  end;
  MsgBox(S, mbInformation, MB_OK);
end;

procedure InitializeWizard;
var
  UseConnectionButton: TNewButton;
begin
  UseConnectionButton := TNewButton.Create(WizardForm);
  UseConnectionButton.Parent := WizardForm;
  UseConnectionButton.Left := 8;
  UseConnectionButton.Top := WizardForm.ClientHeight - UseConnectionButton.Height - 8;
  UseConnectionButton.Width := 155;
  UseConnectionButton.Caption := 'Use connection...';
  UseConnectionButton.OnClick := @UseConnectionButtonClick;
end;