【问题标题】:WIndows System Programming use of standard C library [duplicate]标准C库的Windows系统编程使用[重复]
【发布时间】:2015-11-08 22:13:25
【问题描述】:

我正在阅读 Johnson M. Hart Windows 系统编程。他有一个使用 va_start 标准 c 方法的方法。在下面的示例中,有人可以解释为什么将 Handle hOut 传递给 va_start

BOOL PrintStrings (HANDLE hOut, ...)

/* Write the messages to the output handle. Frequently hOut
    will be standard out or error, but this is not required.
    Use WriteConsole (to handle Unicode) first, as the
    output will normally be the console. If that fails, use WriteFile.

    hOut:   Handle for output file. 
    ... :   Variable argument list containing TCHAR strings.
        The list must be terminated with NULL. */
{
    DWORD msgLen, count;
    LPCTSTR pMsg;
    va_list pMsgList;   /* Current message string. */
    va_start (pMsgList, hOut);  /* Start processing msgs. */
    while ((pMsg = va_arg (pMsgList, LPCTSTR)) != NULL) {
        msgLen = lstrlen (pMsg);
        if (!WriteConsole (hOut, pMsg, msgLen, &count, NULL)
                && !WriteFile (hOut, pMsg, msgLen * sizeof (TCHAR), &count, NULL)) {
            va_end (pMsgList);
            return FALSE;
        }
    }
    va_end (pMsgList);
    return TRUE;
}

【问题讨论】:

  • void va_start (va_list ap, paramN);初始化变量参数列表初始化 ap 以检索参数 paramN 之后的附加参数。
  • 因为 C 标准要求它:va_start 的第二个参数必须是函数的最后命名参数。
  • @Kninnug 可以使用其他值代替 HANDLE 吗?
  • 函数签名中的... 意味着函数可以接受hOut 之后的任意数量的参数,而va_startva_arg 是函数访问这些参数的方式。

标签: c debugging winapi


【解决方案1】:

va_start 是一个提取可变参数的宏。它的工作原理是在第一个可变参数之前立即给出参数。关键是va_start 需要知道在哪里可以找到可变参数。在最后一个命名参数之后立即找到它们。因此使用hOut

关于如何实现可变参数的一些细节:

【讨论】:

  • 能否请您稍微解释一下“va_start 是一个提取可变参数的宏”这句话?我对 C 很陌生,正在寻求一些见解。
  • @DavidHeffernan 我不确定骗子是否真的解释了可变参数是什么,这似乎是 OP 的问题。
  • @JonathanPotter 这个问题只能被解释为好像提问者已经知道什么是可变参数。
  • 我了解可变参数。只是不清楚为什么要使用 HANDLE。
  • 你读过这个欺骗问题吗?
猜你喜欢
  • 2023-03-25
  • 2011-11-16
  • 2019-09-03
  • 2018-10-31
  • 2014-01-31
  • 2023-01-28
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
相关资源
最近更新 更多