关于调试:WIndows系统编程使用标准C库

WIndows System Programming use of standard C library

本问题已经有最佳答案,请猛点这里访问。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}


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

有关可变参数通常如何实现的一些详细信息: