如何执行“网络使用"? Windows 7上的Inno Setup安装程序执行命令? [英] How to execute "net use" command from Inno Setup installer on Windows 7?

查看:116
本文介绍了如何执行“网络使用"? Windows 7上的Inno Setup安装程序执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on an Inno Setup installer, which calls net use to connect to a shared server. The installer can connect to the server, if it's running on Windows XP, but not on Windows 7. I think it's related to UAC as I type the same command, the server is connected on Windows 7, but the setup is running with admin privileges.

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

I'm using the following net use command through Exec or ShellExec script functions:

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

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

Actually, here is a part of the script showing the net use command call:

[Code] 
var 
  ErrorCode: Integer; 
  cmdString: String; 
  intvalue: Integer; 
  str: String; 

function InitializeSetup(): Boolean; 
begin 
  cmdString := '/c net use \\servername password /USER:username'; 
  ShellExec('', ExpandConstant('{cmd}'), cmdString , '', SW_SHOWNORMAL, 
    ewWaitUntilTerminated, ErrorCode) 
  if (ErrorCode = 0) then 
  begin 
    MsgBox(ExpandConstant('{cmd}'), mbInformation, MB_OK); 
  end; 
end;

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

Can anybody suggest how to use net use from Inno Setup on Windows 7? We just want to connect to a server and let user input name and password.

谢谢!

推荐答案

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

How to connect to a remote resource invoking the credentials dialog?

对您的问题使用不同的观点,实际上就是该答案的标题,建议您使用

Using a different view on your question, which is actually as the title of this answer says, I'd suggest you to use the WNetUseConnection function call with CONNECT_INTERACTIVE and CONNECT_PROMPT flags. That will in combination with empty user ID and password parameters invoke the credentials dialog (and that's what you wanted). In Inno Setup script it may look like this:

[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;

这篇关于如何执行“网络使用"? Windows 7上的Inno Setup安装程序执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆