ReactOS 0.4.15-dev-8235-gcd88a97
tools.c File Reference
#include "precomp.h"
Include dependency graph for tools.c:

Go to the source code of this file.

Functions

DoesPortExist

Checks all Port Monitors installed on the local system to find out if a given port already exists.

Parameters
pwszPortNameThe port name to check.
Returns
TRUE if a port with that name already exists on the local system. If the return value is FALSE, either the port doesn't exist or an error occurred. Use GetLastError in this case to check the error case.
BOOL DoesPortExist (PCWSTR pwszPortName)
 
DWORD GetLPTTransmissionRetryTimeout (VOID)
 
GetPortNameWithoutColon

Most of the time, we operate on port names with a trailing colon. But some functions require the name without the trailing colon. This function checks if the port has a trailing colon and if so, it returns the port name without the colon.

Parameters
pwszPortNameThe port name with colon
ppwszPortNameWithoutColonPointer to a PWSTR that will contain the port name without colon. You have to free this buffer using DllFreeSplMem.
Returns
ERROR_SUCCESS if the port name without colon was successfully copied into the buffer. ERROR_INVALID_PARAMETER if this port name has no trailing colon. ERROR_NOT_ENOUGH_MEMORY if memory allocation failed.
DWORD GetPortNameWithoutColon (PCWSTR pwszPortName, PWSTR *ppwszPortNameWithoutColon)
 
_IsNEPort

Checks if the given port name is a virtual Ne port. A virtual Ne port may appear in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports and can have the formats Ne00:, Ne01:, Ne-02:, Ne456: This check is extra picky to not cause false positives (like file name ports starting with "Ne").

Parameters
pwszPortNameThe port name to check.
Returns
TRUE if this is definitely a virtual Ne port, FALSE if not.
static __inline BOOL _IsNEPort (PCWSTR pwszPortName)
 
DWORD GetTypeFromName (LPCWSTR name)
 

Function Documentation

◆ _IsNEPort()

static __inline BOOL _IsNEPort ( PCWSTR  pwszPortName)
static

Definition at line 183 of file tools.c.

184{
185 PCWSTR p = pwszPortName;
186
187 // First character needs to be 'N' (uppercase or lowercase)
188 if (*p != L'N' && *p != L'n')
189 return FALSE;
190
191 // Next character needs to be 'E' (uppercase or lowercase)
192 p++;
193 if (*p != L'E' && *p != L'e')
194 return FALSE;
195
196 // An optional hyphen may follow now.
197 p++;
198 if (*p == L'-')
199 p++;
200
201 // Now an arbitrary number of digits may follow.
202 while (*p >= L'0' && *p <= L'9')
203 p++;
204
205 // Finally, the virtual Ne port must be terminated by a colon.
206 if (*p != ':')
207 return FALSE;
208
209 // If this is the end of the string, we have a virtual Ne port.
210 p++;
211 return (*p == L'\0');
212}
#define FALSE
Definition: types.h:117
GLfloat GLfloat p
Definition: glext.h:8902
#define L(x)
Definition: ntvdm.h:50
const uint16_t * PCWSTR
Definition: typedefs.h:57

Referenced by GetTypeFromName().

◆ DoesPortExist()

BOOL DoesPortExist ( PCWSTR  pwszPortName)

Definition at line 24 of file tools.c.

25{
26 BOOL bReturnValue = FALSE;
27 DWORD cbNeeded;
28 DWORD dwErrorCode;
29 DWORD dwReturned;
30 DWORD i;
32 PPORT_INFO_1W pPortInfo1 = NULL;
33
34 // Determine the required buffer size.
35 EnumPortsW(NULL, 1, NULL, 0, &cbNeeded, &dwReturned);
37 {
38 dwErrorCode = GetLastError();
39 ERR("EnumPortsW failed with error %lu!\n", dwErrorCode);
40 goto Cleanup;
41 }
42
43 // Allocate a buffer large enough.
44 pPortInfo1 = DllAllocSplMem(cbNeeded);
45 if (!pPortInfo1)
46 {
47 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
48 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
49 goto Cleanup;
50 }
51
52 // Now get the actual port information.
53 if (!EnumPortsW(NULL, 1, (PBYTE)pPortInfo1, cbNeeded, &cbNeeded, &dwReturned))
54 {
55 dwErrorCode = GetLastError();
56 ERR("EnumPortsW failed with error %lu!\n", dwErrorCode);
57 goto Cleanup;
58 }
59
60 // We were successful! Loop through all returned ports.
61 dwErrorCode = ERROR_SUCCESS;
62 p = pPortInfo1;
63
64 for (i = 0; i < dwReturned; i++)
65 {
66 // Check if this existing port matches our queried one.
67 if (wcsicmp(p->pName, pwszPortName) == 0)
68 {
69 bReturnValue = TRUE;
70 goto Cleanup;
71 }
72
73 p++;
74 }
75
77 if (pPortInfo1)
78 DllFreeSplMem(pPortInfo1);
79
80 SetLastError(dwErrorCode);
81 return bReturnValue;
82}
#define ERR(fmt,...)
Definition: debug.h:113
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define SetLastError(x)
Definition: compat.h:752
#define wcsicmp
Definition: compat.h:15
static const WCHAR Cleanup[]
Definition: register.c:80
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
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
BYTE * PBYTE
Definition: pedump.c:66
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
WINBOOL WINAPI EnumPortsW(LPWSTR pName, DWORD Level, LPBYTE pPorts, DWORD cbBuf, LPDWORD pcbNeeded, LPDWORD pcReturned)

Referenced by _HandleAddPort(), _HandlePortExists(), LocalmonAddPort(), and LocalmonAddPortEx().

◆ GetLPTTransmissionRetryTimeout()

DWORD GetLPTTransmissionRetryTimeout ( VOID  )

Definition at line 85 of file tools.c.

86{
87 DWORD cbBuffer;
88 DWORD dwReturnValue = 90; // Use 90 seconds as default if we fail to read from registry.
89 HKEY hKey;
90 LSTATUS lStatus;
91
92 // Six digits is the most you can enter in Windows' LocalUI.dll.
93 // Larger values make it crash, so introduce a limit here.
94 WCHAR wszBuffer[6 + 1];
95
96 // Open the key where our value is stored.
97 lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows", 0, KEY_READ, &hKey);
98 if (lStatus != ERROR_SUCCESS)
99 {
100 ERR("RegOpenKeyExW failed with status %ld!\n", lStatus);
101 goto Cleanup;
102 }
103
104 // Query the value.
105 cbBuffer = sizeof(wszBuffer);
106 lStatus = RegQueryValueExW(hKey, L"TransmissionRetryTimeout", NULL, NULL, (PBYTE)wszBuffer, &cbBuffer);
107 if (lStatus != ERROR_SUCCESS)
108 {
109 ERR("RegQueryValueExW failed with status %ld!\n", lStatus);
110 goto Cleanup;
111 }
112
113 // Return it converted to a DWORD.
114 dwReturnValue = wcstoul(wszBuffer, NULL, 10);
115
116Cleanup:
117 if (hKey)
119
120 return dwReturnValue;
121}
#define RegCloseKey(hKey)
Definition: registry.h:49
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
FxAutoRegKey hKey
_Check_return_ unsigned long __cdecl wcstoul(_In_z_ const wchar_t *_Str, _Out_opt_ _Deref_post_z_ wchar_t **_EndPtr, _In_ int _Radix)
#define KEY_READ
Definition: nt_native.h:1023
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by _HandleGetTransmissionRetryTimeout(), and _SetTransmissionRetryTimeout().

◆ GetPortNameWithoutColon()

DWORD GetPortNameWithoutColon ( PCWSTR  pwszPortName,
PWSTR ppwszPortNameWithoutColon 
)

Definition at line 142 of file tools.c.

143{
144 DWORD cchPortNameWithoutColon;
145
146 // Compute the string length of pwszPortNameWithoutColon.
147 cchPortNameWithoutColon = wcslen(pwszPortName) - 1;
148
149 // Check if pwszPortName really has a colon as the last character.
150 if (pwszPortName[cchPortNameWithoutColon] != L':')
152
153 // Allocate the output buffer.
154 *ppwszPortNameWithoutColon = DllAllocSplMem((cchPortNameWithoutColon + 1) * sizeof(WCHAR));
155 if (!*ppwszPortNameWithoutColon)
156 {
157 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
159 }
160
161 // Copy the port name without colon into the buffer.
162 // The buffer is already zero-initialized, so no additional null-termination is necessary.
163 CopyMemory(*ppwszPortNameWithoutColon, pwszPortName, cchPortNameWithoutColon * sizeof(WCHAR));
164
165 return ERROR_SUCCESS;
166}
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define CopyMemory
Definition: winbase.h:1710

Referenced by _ClosePortHandles(), _CreateNonspooledPort(), and _HandleSetDefaultCommConfig().

◆ GetTypeFromName()

DWORD GetTypeFromName ( LPCWSTR  name)

Definition at line 215 of file tools.c.

216{
217 HANDLE hfile;
218
219 if (!wcsncmp(name, L"LPT", ARRAYSIZE(L"LPT") - 1) )
220 return PORT_IS_LPT;
221
222 if (!wcsncmp(name, L"COM", ARRAYSIZE(L"COM") - 1) )
223 return PORT_IS_COM;
224
225 if (!lstrcmpW(name, L"FILE:") )
226 return PORT_IS_FILE;
227
228// if (name[0] == '/')
229// return PORT_IS_UNIXNAME;
230
231// if (name[0] == '|')
232// return PORT_IS_PIPE;
233
234 if ( _IsNEPort( name ) )
235 return PORT_IS_VNET;
236
237 if (!wcsncmp(name, L"XPS", ARRAYSIZE(L"XPS") - 1))
238 return PORT_IS_XPS;
239
240 /* Must be a file or a directory. Does the file exist ? */
242
243 FIXME("%p for OPEN_EXISTING on %s\n", hfile, debugstr_w(name));
244
245 if (hfile == INVALID_HANDLE_VALUE)
246 {
247 /* Can we create the file? */
249 FIXME("%p for OPEN_ALWAYS\n", hfile);
250 }
251
252 if (hfile != INVALID_HANDLE_VALUE)
253 {
254 CloseHandle(hfile); FIXME("PORT_IS_FILENAME %d\n",PORT_IS_FILENAME);
255 return PORT_IS_FILENAME;
256 }
257 FIXME("PORT_IS_UNKNOWN %d\n",PORT_IS_UNKNOWN);
258 /* We can't use the name. use GetLastError() for the reason */
259 return PORT_IS_UNKNOWN;
260}
#define FIXME(fmt,...)
Definition: debug.h:114
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4242
#define debugstr_w
Definition: kernel32.h:32
#define FILE_FLAG_DELETE_ON_CLOSE
Definition: disk.h:42
#define OPEN_ALWAYS
Definition: disk.h:70
static __inline BOOL _IsNEPort(PCWSTR pwszPortName)
Definition: tools.c:183
#define GENERIC_WRITE
Definition: nt_native.h:90
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
#define PORT_IS_XPS
Definition: spoolss.h:23
#define PORT_IS_LPT
Definition: spoolss.h:15
#define PORT_IS_COM
Definition: spoolss.h:16
#define PORT_IS_FILE
Definition: spoolss.h:17
#define PORT_IS_FILENAME
Definition: spoolss.h:18
#define PORT_IS_VNET
Definition: spoolss.h:22
#define PORT_IS_UNKNOWN
Definition: spoolss.h:14
Definition: name.c:39

Referenced by _HandlePortIsValid().