首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试系统插件返回的字符串时的意外逻辑库行为

测试系统插件返回的字符串时的意外逻辑库行为
EN

Stack Overflow用户
提问于 2012-09-13 08:49:02
回答 1查看 102关注 0票数 0

我是通过系统插件获得系统信息,使用类似于对我先前问题的回答的代码。API函数非常类似,只是返回(或不返回)一个宽字符字符串。

在作为域成员的计算机上使用该脚本时,我不明白logiclib测试为什么总是通过测试的${else}分支。下面的捕获显示$1不是空的:

在阅读更多的NetGetDCName参考时,我认为最好检查函数返回的值,而不是粗字符字符串指针。我有点困惑,为什么我的${If} 0 <> $1总是失败?在本例中,我检查了一个虚拟机,它不是$1等于0的域的成员。那么,我认为释放内存的${IfThen}测试也是错误的。

下面是示例代码:

代码语言:javascript
复制
!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

编辑:感谢安德斯,下面是修正后的函数:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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不受影响地传递给空闲函数.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12402628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档