ReactOS 0.4.15-dev-7934-g1dc8d80
utils.c File Reference

General-purpose utility functions (wrappers around or reimplementations of Win32 APIs). More...

#include <windef.h>
#include <winbase.h>
#include <winnls.h>
#include <winuser.h>
#include <wincon.h>
#include <strsafe.h>
#include <pseh/pseh2.h>
#include "utils.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Macros

#define UNICODE
 
#define _UNICODE
 

Functions

K32LoadStringExW

Loads a string resource from the executable file associated with a specified module, copies the string into a buffer, and appends a terminating null character. This is basically the LoadString() API ripped from user32.dll to remove any dependency of ConUtils from user32.dll, and to add support for loading strings from other languages than the current one.

Parameters
[in]hInstanceOptional handle to an instance of the module whose executable file contains the string resource. Can be set to NULL to get the handle to the application itself.
[in]uIDThe identifier of the string to be loaded.
[in]LanguageIdThe language identifier of the resource. If this parameter is MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), the current language associated with the calling thread is used. To specify a language other than the current language, use the MAKELANGID macro to create this parameter.
[out]lpBufferThe buffer that receives the string. Must be of sufficient length to hold a pointer (8 bytes).
[in]nBufferMaxThe size of the buffer, in characters. The string is truncated and NULL-terminated if it is longer than the number of characters specified. If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.
Returns
If the function succeeds, the return value is the number of characters copied into the buffer, not including the terminating null character, or zero if the string resource does not exist. To get extended error information, call GetLastError().
See also
LoadString(), K32LoadStringW()
INT WINAPI K32LoadStringExW (IN HINSTANCE hInstance OPTIONAL, IN UINT uID, IN LANGID LanguageId, OUT LPWSTR lpBuffer, IN INT nBufferMax)
 
K32LoadStringW

Loads a string resource from the executable file associated with a specified module, copies the string into a buffer, and appends a terminating null character. This is a restricted version of K32LoadStringExW().

See also
LoadString(), K32LoadStringExW()
INT WINAPI K32LoadStringW (IN HINSTANCE hInstance OPTIONAL, IN UINT uID, OUT LPWSTR lpBuffer, IN INT nBufferMax)
 
FormatMessageSafeW

Loads and formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed to the function. It can come from a message table resource in an already-loaded module, or the caller can ask the function to search the system's message table resource(s) for the message definition. Please refer to the Win32 FormatMessage() function for more details.

Parameters
[in]dwFlagsThe formatting options, and how to interpret the lpSource parameter. See FormatMessage() for more details.
[in]lpSourceThe location of the message definition. The type of this parameter depends upon the settings in the dwFlags parameter.
[in]dwMessageIdThe message identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.
[in]dwLanguageIdThe language identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.
[out]lpBufferA pointer to a buffer that receives the null-terminated string that specifies the formatted message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc() function, and places the pointer to the buffer at the address specified in lpBuffer. This buffer cannot be larger than 64kB.
[in]nSizeIf the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the size of the output buffer, in TCHARs. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of TCHARs to allocate for an output buffer. The output buffer cannot be larger than 64kB.
[in]ArgumentsOptional pointer to an array of values describing a variable number of arguments, depending on the message string. Each argument is used to replace an insert sequence in the message string. By default, the Arguments parameter is of type va_list*, initialized with va_start(). The state of the va_list argument is undefined upon return from the function. To use the va_list again, destroy the variable argument list pointer using va_end() and reinitialize it with va_start(). If you do not have a pointer of type va_list*, then specify the FORMAT_MESSAGE_ARGUMENT_ARRAY flag and pass a pointer to an array of DWORD_PTR values; those values are input to the message formatted as the insert values. Each insert must have a corresponding element in the array.
Returns
If the function succeeds, the return value is the number of characters copied into the buffer, not including the terminating null character, or zero if the string resource does not exist. To get extended error information, call GetLastError().
Remarks
This function is a "safe" version of FormatMessage(), that does not crash if a malformed source string is retrieved and then being used for formatting. It basically wraps calls to FormatMessage() within SEH.
See also
FormatMessage() (on MSDN)
DWORD WINAPI FormatMessageSafeW (IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId, OUT LPWSTR lpBuffer, IN DWORD nSize, IN va_list *Arguments OPTIONAL)
 
ConSetThreadUILanguage

Sets the current thread's user interface language. Mostly used by console applications for selecting a language identifier that best supports the NT Console. This function dynamically loads and calls kernel32!SetThreadUILanguage() so as to be able to work on older environments where this API is not supported. The FormatMessage() API also bases itself on the thread's current language for its default behaviour (unless an explicit language identifier has been provided).

Parameters
[in,opt]LangId (Vista+) A non-zero language identifier that specifies the current thread's user interface language to set. (XP/2003) Set the language identifier to 0 for selecting a language identifier that best supports the NT Console.
Returns
Returns LangId in case of success, or 0 in case of failure. If LangId was set to 0, the function always succeeds and returns the language identifier that best supports the NT Console.
Remarks
This function is thread-safe.
See also
SetThreadUILanguage() (on MSDN)
LANGID ConSetThreadUILanguage (IN LANGID LangId OPTIONAL)
 
IsTTYHandle

Checks whether a handle refers to a valid TTY object. A TTY object may be a console or a "communications" (e.g. serial) port.

Parameters
[in]hHandleHandle to the TTY object to check for.
Returns
TRUE when the handle refers to a valid TTY object, FALSE if it does not.
Remarks
This test is more general than IsConsoleHandle() as it is not limited to Win32 console objects only.
See also
IsConsoleHandle()
BOOL IsTTYHandle (IN HANDLE hHandle)
 
IsConsoleHandle

Checks whether a handle refers to a valid Win32 console object.

Parameters
[in]hHandleHandle to the Win32 console object to check for: console input buffer, console output buffer.
Returns
TRUE when the handle refers to a valid Win32 console object, FALSE if it does not.
See also
IsTTYHandle()
BOOL IsConsoleHandle (IN HANDLE hHandle)
 

Detailed Description

General-purpose utility functions (wrappers around or reimplementations of Win32 APIs).

Definition in file utils.c.

Macro Definition Documentation

◆ _UNICODE

#define _UNICODE

Definition at line 20 of file utils.c.

◆ UNICODE

Definition at line 19 of file utils.c.

Function Documentation

◆ ConSetThreadUILanguage()

LANGID ConSetThreadUILanguage ( IN LANGID LangId  OPTIONAL)

Definition at line 352 of file utils.c.

354{
355 /* The function pointer is shared amongst all threads */
356 static volatile LANGID (WINAPI *pfnSetThreadUILanguage)(LANGID) = NULL;
357
358 if (!pfnSetThreadUILanguage)
359 {
360 /* Load the API from kernel32 */
361 PVOID pFunc = (PVOID)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetThreadUILanguage");
362 if (!pFunc)
363 {
364 /* Fail since the API is not available */
365 return 0;
366 }
367 /* Set the function pointer in case it hasn't been already set by another thread */
368 InterlockedCompareExchangePointer((PVOID*)&pfnSetThreadUILanguage, pFunc, NULL);
369 // ASSERT(pfnSetThreadUILanguage);
370 }
371 return pfnSetThreadUILanguage(LangId);
372}
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
USHORT LANGID
Definition: mui.h:9
#define L(x)
Definition: ntvdm.h:50
void * PVOID
Definition: typedefs.h:50
#define WINAPI
Definition: msvc.h:6

Referenced by _tmain(), cmd_start(), Execute(), SetConsoleCPState(), and wmain().

◆ FormatMessageSafeW()

DWORD WINAPI FormatMessageSafeW ( IN DWORD  dwFlags,
IN LPCVOID lpSource  OPTIONAL,
IN DWORD  dwMessageId,
IN DWORD  dwLanguageId,
OUT LPWSTR  lpBuffer,
IN DWORD  nSize,
IN va_list *Arguments  OPTIONAL 
)

Definition at line 254 of file utils.c.

262{
263 DWORD dwLength = 0;
264
266 {
267 /*
268 * Retrieve the message string. Wrap in SEH
269 * to protect from invalid string parameters.
270 */
272 {
274 lpSource,
275 dwMessageId,
276 dwLanguageId,
277 lpBuffer,
278 nSize,
279 Arguments);
280 }
282 {
283 dwLength = 0;
284
285 /*
286 * An exception occurred while calling FormatMessage, this is usually
287 * the sign that a parameter was invalid, either 'lpBuffer' was NULL
288 * but we did not pass the flag FORMAT_MESSAGE_ALLOCATE_BUFFER, or the
289 * array pointer 'Arguments' was NULL or did not contain enough elements,
290 * and we did not pass the flag FORMAT_MESSAGE_IGNORE_INSERTS, and the
291 * message string expected too many inserts.
292 * In this last case only, we can call again FormatMessage but ignore
293 * explicitly the inserts. The string that we will return to the user
294 * will not be pre-formatted.
295 */
298 {
299 /* Remove any possible harmful flags and always ignore inserts */
300 dwFlags &= ~FORMAT_MESSAGE_ARGUMENT_ARRAY;
302
303 /* If this call also throws an exception, we are really dead */
305 lpSource,
306 dwMessageId,
307 dwLanguageId,
308 lpBuffer,
309 nSize,
310 NULL /* Arguments */);
311 }
312 }
313 _SEH2_END;
314 }
316 {
317 }
318 _SEH2_END;
319
320 return dwLength;
321}
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
static DWORD DWORD * dwLength
Definition: fusion.c:86
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
#define _SEH2_FINALLY
Definition: filesup.c:21
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:34
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:420
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:419
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2084
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176

Referenced by ConMsgPrintfV(), and ConResMsgPrintfExV().

◆ IsConsoleHandle()

BOOL IsConsoleHandle ( IN HANDLE  hHandle)

Definition at line 419 of file utils.c.

420{
421 DWORD dwMode;
422
423 /* Check whether the handle may be that of a console... */
424 if ((GetFileType(hHandle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR)
425 return FALSE;
426
427 /*
428 * It may be. Perform another test. The idea comes from the
429 * MSDN description of the WriteConsole API:
430 *
431 * "WriteConsole fails if it is used with a standard handle
432 * that is redirected to a file. If an application processes
433 * multilingual output that can be redirected, determine whether
434 * the output handle is a console handle (one method is to call
435 * the GetConsoleMode function and check whether it succeeds).
436 * If the handle is a console handle, call WriteConsole. If the
437 * handle is not a console handle, the output is redirected and
438 * you should call WriteFile to perform the I/O."
439 */
440 return GetConsoleMode(hHandle, &dwMode);
441}
#define FALSE
Definition: types.h:117
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
#define FILE_TYPE_CHAR
Definition: winbase.h:260
#define FILE_TYPE_REMOTE
Definition: winbase.h:262

◆ IsTTYHandle()

BOOL IsTTYHandle ( IN HANDLE  hHandle)

Definition at line 393 of file utils.c.

394{
395 /*
396 * More general test than IsConsoleHandle(). Consoles, as well as serial
397 * (communications) ports, etc... verify this test, but only consoles
398 * verify the IsConsoleHandle() test: indeed the latter checks whether
399 * the handle is really handled by the console subsystem.
400 */
401 return ((GetFileType(hHandle) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR);
402}

Referenced by ConClearLine(), ConClearScreen(), ConGetScreenInfo(), and ConRingBell().

◆ K32LoadStringExW()

INT WINAPI K32LoadStringExW ( IN HINSTANCE hInstance  OPTIONAL,
IN UINT  uID,
IN LANGID  LanguageId,
OUT LPWSTR  lpBuffer,
IN INT  nBufferMax 
)

Definition at line 99 of file utils.c.

105{
106 HRSRC hrsrc;
107 HGLOBAL hmem;
108 WCHAR *p;
109 UINT i;
110
111 if (!lpBuffer)
112 return 0;
113
114 /* Use LOWORD (incremented by 1) as ResourceID */
115 /* There are always blocks of 16 strings */
118 MAKEINTRESOURCEW((LOWORD(uID) >> 4) + 1),
119 LanguageId);
120 if (!hrsrc) return 0;
121
122 hmem = LoadResource(hInstance, hrsrc);
123 if (!hmem) return 0;
124
125 p = LockResource(hmem);
126 // FreeResource(hmem);
127
128 /* Find the string we're looking for */
129 uID &= 0x000F; /* Position in the block, same as % 16 */
130 for (i = 0; i < uID; i++)
131 p += *p + 1;
132
133 /*
134 * If nBufferMax == 0, then return a read-only pointer
135 * to the resource itself in lpBuffer it is assumed that
136 * lpBuffer is actually a (LPWSTR*).
137 */
138 if (nBufferMax == 0)
139 {
140 *((LPWSTR*)lpBuffer) = p + 1;
141 return *p;
142 }
143
144 i = min(nBufferMax - 1, *p);
145 if (i > 0)
146 {
147 memcpy(lpBuffer, p + 1, i * sizeof(WCHAR));
148 lpBuffer[i] = L'\0';
149 }
150 else
151 {
152 if (nBufferMax > 1)
153 {
154 lpBuffer[0] = L'\0';
155 return 0;
156 }
157 }
158
159 return i;
160}
HINSTANCE hInstance
Definition: charmap.c:19
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
HRSRC WINAPI FindResourceExW(HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang)
Definition: res.c:164
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define min(a, b)
Definition: monoChain.cc:55
unsigned int UINT
Definition: ndis.h:50
#define LOWORD(l)
Definition: pedump.c:82
#define RT_STRING
Definition: pedump.c:368
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185

Referenced by ConResMsgPrintfExV(), ConResPrintfExV(), ConResPutsEx(), and K32LoadStringW().

◆ K32LoadStringW()

INT WINAPI K32LoadStringW ( IN HINSTANCE hInstance  OPTIONAL,
IN UINT  uID,
OUT LPWSTR  lpBuffer,
IN INT  nBufferMax 
)

Definition at line 173 of file utils.c.

178{
179 // NOTE: Instead of using LANG_NEUTRAL, one might use LANG_USER_DEFAULT...
180 return K32LoadStringExW(hInstance, uID,
182 lpBuffer, nBufferMax);
183}
#define LANG_NEUTRAL
Definition: nls.h:22
#define MAKELANGID(p, s)
Definition: nls.h:15
#define SUBLANG_NEUTRAL
Definition: nls.h:167
INT WINAPI K32LoadStringExW(IN HINSTANCE hInstance OPTIONAL, IN UINT uID, IN LANGID LanguageId, OUT LPWSTR lpBuffer, IN INT nBufferMax)
Definition: utils.c:99

Referenced by ConResPagingEx(), HelpCommand(), HelpCommandList(), PagePrompt(), Usage(), WlanPrintCurrentStatus(), WlanScan(), and wmain().