API 在屏幕上简单显示字符串

原文链接:https://www.cnblogs.com/jqdy/p/15078827.html

  编写服务程序时,仅仅在安装服务过程中需要在显示器上输出简单的提示信息,其他部分并不需要显示器的输出操作。如果使用printf系列函数会占用一定的空间。

  使用 API 函数做了一个简单的输出函数:

 1 // ANSI 编码
 2 VOID ApiPrintfA(INT count, ...)
 3 {
 4     static HANDLE hConsoleOutput = NULL;
 5     va_list args;
 6     PSTR arg = NULL;
 7     SIZE_T cchStr;
 8 
 9     // 获取标准输出句柄
10 
11     if (NULL == hConsoleOutput)
12     {
13         hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
14     }
15 
16     va_start(args, count);
17 
18     do{
19         arg = va_arg(args, PSTR);
20         StringCchLengthA(arg, 1024, &cchStr);
21         WriteConsoleA(
22             hConsoleOutput,
23             arg,
24             (DWORD)cchStr,
25             NULL,
26             NULL);
27     } while (--count); // count = 0 时为假,跳出循环
28 
29     va_end(args);
30 }
31 
32 // UNICODE 编码
33 VOID ApiPrintfW(INT count, ...)
34 {
35     static HANDLE hConsoleOutput = NULL;
36     va_list args;
37     PWSTR arg = NULL;
38     SIZE_T cchStr;
39 
40     // 获取标准输出句柄
41 
42     if (NULL == hConsoleOutput)
43     {
44         hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
45     }
46 
47     va_start(args, count);
48 
49     do {
50         arg = va_arg(args, PWSTR);
51         StringCchLengthW(arg, 1024, &cchStr);
52         WriteConsoleW(
53             hConsoleOutput,
54             arg,
55             (DWORD)cchStr,
56             NULL,
57             NULL);
58     } while (--count); // count = 0 时为假,跳出循环
59 
60     va_end(args);
61 }

 

posted @ 2021-07-30 11:30  jqdy  阅读(164)  评论(0编辑  收藏  举报