【发布时间】: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_start和va_arg是函数访问这些参数的方式。