我是通过系统插件获得系统信息,使用类似于对我先前问题的回答的代码。API函数非常类似,只是返回(或不返回)一个宽字符字符串。
在作为域成员的计算机上使用该脚本时,我不明白logiclib测试为什么总是通过测试的${else}分支。下面的捕获显示$1不是空的:

在阅读更多的NetGetDCName参考时,我认为最好检查函数返回的值,而不是粗字符字符串指针。我有点困惑,为什么我的${If} 0 <> $1总是失败?在本例中,我检查了一个虚拟机,它不是$1等于0的域的成员。那么,我认为释放内存的${IfThen}测试也是错误的。
下面是示例代码:
!include "logiclib.nsh"
outfile "hello2.exe"
!define DEBUG `System::Call kernel32::OutputDebugString(ts)`
section
Call GetDCName
pop $0
Messagebox MB_OK "DCName=$0"
sectionEnd
Function GetDCName
;Push the domain controler name to the stack (or an empty string if no dc)
push $0
push $1
push $2
System::Call "netapi32::NetGetDCName(i0, i0, *w 0 r1) i.r2"
Dumpstate::debug
${If} 0 <> $1
${debug} "copy $1"
StrCpy $0 $1
${else}
${debug} "copy empty string"
StrCpy $0 ""
${endif}
${IfThen} $1 <> 0 ${|} System::Call "netapi32::NetApiBufferFree(ir1)" ${|}
Pop $2
Pop $1
Exch $0
FunctionEnd编辑:感谢安德斯,下面是修正后的函数:
Function GetDCName
;Push the domain controler name to the stack (or an empty string if no dc)
push $0
push $1
push $2
System::Call "netapi32::NetGetDCName(i0, i0, *i 0 r1) i.r2" ;do not convert r1 to wchar, keep the pointer to free it
${If} $2 = 0
System::Call "*$1(&w${NSIS_MAX_STRLEN} .r0)" ;get the wchar from the pointer
${IfThen} $1 <> 0 ${|} System::Call "netapi32::NetApiBufferFree(ir1)" ${|} ;free the pointer
${else}
StrCpy $0 ""
${endif}
Pop $2
Pop $1
Exch $0
FunctionEnd发布于 2012-09-13 09:17:49
<>运算符用于数字,而不是字符串。这并不是您唯一的问题;* (通常)只能与i一起使用,而不是与i一起使用。t/m/w类型转换为C样式字符串,但在您的情况下,您需要实际的内存地址,因为您必须将它传递给空闲函数。
您需要将*i0r1传递给NetGetDCName,如果$2为0,则可以使用struct语法(*$1(&w${NSIS_MAX_STRLEN}.r5)或*$1(w.r5))提取字符串,最后将$1不受影响地传递给空闲函数.
https://stackoverflow.com/questions/12402628
复制相似问题