简体   繁体   English

在 InnoSetup 安装程序中获取用户主体名称 (UPN)?

[英]Get User Principal Name (UPN) In InnoSetup Installer?

Within the InitializeSetup() function among other actions, when the installer is ran, I would like the installer to retrieve the current UPN.在 InitializeSetup() function 等操作中,当安装程序运行时,我希望安装程序检索当前的 UPN。 The UserName variable is not sufficient enough. UserName 变量还不够。 I have also tried methods discussed here utilizing the WTSQuerySessionInformation() function but they don't seem to return what I am looking for.我还尝试过使用 WTSQuerySessionInformation() function 在此处讨论的方法,但它们似乎没有返回我正在寻找的内容。 Depending on the organization and setting the UPN should often return some sort of an email address which I am looking for.根据组织和设置的不同,UPN 应该经常返回我正在寻找的某种 email 地址。 Can someone shed some light on how to return the full UPN value as a string?有人可以阐明如何将完整的 UPN 值作为字符串返回吗? Thank you.谢谢你。

EDIT:编辑:

I have also tried the GetUserNameExW() function passing in value 8 as an input which refers to UserNamePrincipal, however I am returning an empty value it seems.我还尝试了 GetUserNameExW() function 将值 8 作为输入传递给 UserNamePrincipal,但我似乎返回了一个空值。

function GetUserNameExW(NameFormat: Integer; lpNameBuffer: string; var nSize: DWORD): Boolean;
  external 'GetUserNameExW@secur32.dll stdcall';
var
  NumChars: DWORD;
  OutStr: string;
  name: string;

begin
  SetLength(OutStr, NumChars);
  GetUserNameExW(8, OutStr, NumChars);
  name := Copy(OutStr,1,NumChars);

The correct code to call GetUserNameExW to get the current user's userPrincipalName (UPN) attribute would look like this:调用GetUserNameExW以获取当前用户的userPrincipalName (UPN) 属性的正确代码如下所示:

function GetUserNameExW(NameFormat: Integer; lpNameBuffer: string; var nSize: DWORD): Boolean;
  external 'GetUserNameExW@secur32.dll stdcall';

function GetUserPrincipalName(): string;
var
  NumChars: DWORD;
  OutStr: string;
begin
  result := '';
  NumChars := 0;
  if (not GetUserNameExW(8, '', NumChars)) and (DLLGetLastError() = 234) then
  begin
    SetLength(OutStr, NumChars);
    if GetUserNameExW(8, OutStr, NumChars) then
      result := Copy(OutStr, 1, NumChars);
  end;
end;

The value 8 for the NameFormat parameter corresponds to NameUserPrincipal in the EXTENDED_NAME_FORMAT enumeration, and the value 234 is API value ERROR_MORE_DATA . NameFormat参数的值8对应于EXTENDED_NAME_FORMAT枚举中的NameUserPrincipal ,值234是 API 值ERROR_MORE_DATA

However--as I pointed out in a comment--if you are looking for an email address, this would not be the code to do that because userPrincipalName (UPN) is a separate user attribute (in fact, in many, if not most organizations, the UPN is different from the user's email address).然而——正如我在评论中指出的——如果你正在寻找一个 email 地址,这将不是执行此操作的代码,因为userPrincipalName (UPN) 是一个单独的用户属性(事实上,即使不是大多数,也有很多)组织,UPN 不同于用户的 email 地址)。 Also, if you are assuming the UPN to be the same as one of the user's email email addresses, this would also be an incorrect assumption, as the UPN's value is very often not in the list of valid email addresses for a user.此外,如果您假设 UPN 与用户的 email email 地址之一相同,这也是一个错误的假设,因为 UPN 的值通常不在用户的有效 email 地址列表中。

The point is that if you are looking for a reliable way to get a valid email address for a user, the UPN is not going give you one.关键是,如果您正在寻找一种可靠的方法来为用户获取有效的 email 地址,UPN 不会给您一个。

I have managed to the solve this issue myself but still not 100% sure on why my previous iteration resulted in odd behavior.我自己设法解决了这个问题,但仍然不能 100% 确定为什么我之前的迭代会导致奇怪的行为。

Essentially, I had to add an if check before:本质上,我必须在之前添加一个 if 检查:

if GetUserNameExW(8, OutStr, NumChars) then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM