C++ 命名管道 IPC (六)

2014-11-24 02:37:40 · 作者: · 浏览: 15
en;
cbResponse = sizeof(chResponse);

if (!WriteFile(
hNamedPipe, // Handle of the pipe
chResponse, // Buffer to write
cbResponse, // Number of bytes to write
&cbWritten, // Number of bytes written
NULL // Not overlapped I/O
))
{
dwError = GetLastError();
wprintf(L"WriteFile to pipe failed w/err 0x%08lx\n", dwError);
goto Cleanup;
}

wprintf(L"Send %ld bytes to client: \"%s\"\n", cbWritten, chResponse);

// Flush the pipe to allow the client to read the pipe's contents
// before disconnecting. Then disconnect the client's connection.
FlushFileBuffers(hNamedPipe);
DisconnectNamedPipe(hNamedPipe);

Cleanup:

// Centralized cleanup for all allocated resources.
if (pSa != NULL)
{
FreePipeSecurity(pSa);
pSa = NULL;
}
if (hNamedPipe != INVALID_HANDLE_VALUE)
{
CloseHandle(hNamedPipe);
hNamedPipe = INVALID_HANDLE_VALUE;
}

return dwError;
}


//
// FUNCTION: CreatePipeSecurity(PSECURITY_ATTRIBUTES *)
//
// PURPOSE: The CreatePipeSecurity function creates and initializes a new
// SECURITY_ATTRIBUTES structure to allow Authenticated Users read and
// write access to a pipe, and to allow the Administrators group full
// access to the pipe.
//
// PARAMETERS:
// * ppSa - output a pointer to a SECURITY_ATTRIBUTES structure that allows
// Authenticated Users read and write access to a pipe, and allows the
// Administrators group full access to the pipe. The structure must be
// freed by calling FreePipeSecurity.
//
// RETURN VALUE: Returns TRUE if the function succeeds.
//
// EXAMPLE CALL:
//
// PSECURITY_ATTRIBUTES pSa = NULL;
// if (CreatePipeSecurity(&pSa))
// {
// // Use the security attributes
// // ...
//
// FreePipeSecurity(pSa);
// }
//
BOOL CreatePipeSecurity(PSECURITY_ATTRIBUTES *ppSa)
{
BOOL fSucceeded = TRUE;
DWORD dwError = ERROR_SUCCESS;

PSECURITY_DESCRIPTOR pSd = NULL;
PSECURITY_ATTRIBUTES pSa = NULL;

// Define the SDDL for the security descriptor.
PCWSTR szSDDL = L"D:" // Discretionary ACL
L"(A;OICI;GRGW;;;AU)" // Allow read/write to authenticated users
L"(A;OICI;GA;;;BA)"; // Allow full control to administrators

if (!ConvertStringSecurityDescriptorToSecurityDescriptor(szSDDL,
SDDL_REVISION_1, &pSd, NULL))
{
fSucceeded = FALSE;
dwError = GetLastError();
goto Cleanup;
}

// Allocate the memory of SECURITY_ATTRIBUTES.
pSa = (PSECURITY_ATTRIBUTES)LocalAlloc(LPTR, sizeof(*pSa));
if (pSa == NULL)
{
fSucceeded = FALSE;
dwError = GetLastError();
goto Cleanup;
}

pSa->nLength = sizeof(*pSa);
pSa->lpSecurityDescriptor = pSd;
pSa->bInheritHandle = FALSE;

*ppSa = pSa;

Cleanup:
// Clean up the allocated resources if something is wrong.
if (!fSucceeded)
{
if (pSd)
{
LocalFree(pSd);
pSd = NULL;
}
if (pSa)
{
LocalFree(pSa);
pSa = NULL;
}

SetLastError(dwError);
}

return fSucceeded;
}


//
//