Exemplo n.º 1
0
/*******************************************************************
 DirEnsureExists

*******************************************************************/
extern "C" HRESULT DAPI DirEnsureExists(
	__in LPCWSTR wzPath,
	__in_opt LPSECURITY_ATTRIBUTES psa
	)
{
	HRESULT hr = S_OK;
	UINT er;

	// try to create this directory
	if (!::CreateDirectoryW(wzPath, psa))
	{
		// if the directory already exists, bail
		er = ::GetLastError();
		if (ERROR_ALREADY_EXISTS == er)
			ExitFunction1(hr = S_OK);

		// get the parent path and try to create it
		LPWSTR pwzLastSlash = NULL;
		for (LPWSTR pwz = const_cast<LPWSTR>(wzPath); *pwz; pwz++)
			if (*pwz == L'\\')
				pwzLastSlash = pwz;

		// if there is no parent directory fail
		ExitOnNullDebugTrace(pwzLastSlash, hr, HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "cannot find parent path");

		*pwzLastSlash = L'\0';	// null terminate the parent path
		hr = DirEnsureExists(wzPath, psa);   // recurse!
		*pwzLastSlash = L'\\';  // put the slash back
		ExitOnFailureDebugTrace1(hr, "failed to create path: %S", wzPath);

		// try to create the directory now that all parents are created
		if (!::CreateDirectoryW(wzPath, psa))
		{
			// if the directory already exists for some reason no error
			er = ::GetLastError();
			if (ERROR_ALREADY_EXISTS == er)
				hr = S_FALSE;
			else
				hr = HRESULT_FROM_WIN32(er);
		}
		else
			hr = S_OK;
	}

LExit:
	return hr;
}
Exemplo n.º 2
0
/********************************************************************
 ResReadStringAnsi

 NOTE: ppszString should be freed with StrFree()
********************************************************************/
extern "C" HRESULT DAPI ResReadStringAnsi(
    __in HINSTANCE hinst,
    __in UINT uID,
    __deref_out_z LPSTR* ppszString
    )
{
    Assert(hinst && ppszString);

    HRESULT hr = S_OK;
    DWORD cch = 64;  // first guess
    DWORD cchReturned = 0;

    do
    {
        hr = StrAnsiAlloc(ppszString, cch);
        ExitOnFailureDebugTrace1(hr, "Failed to allocate string for resource id: %d", uID);

#pragma prefast(push)
#pragma prefast(disable:25068)
        cchReturned = ::LoadStringA(hinst, uID, *ppszString, cch);
#pragma prefast(pop)
        if (0 == cchReturned)
        {
            ExitWithLastError1(hr, "Failed to load string resource id: %d", uID);
        }

        // if the returned string count is one character too small, it's likely we have
        // more data to read
        if (cchReturned + 1 == cch)
        {
            cch *= 2;
            hr = S_FALSE;
        }
    } while (S_FALSE == hr);
    ExitOnFailure1(hr, "failed to load string resource id: %d", uID);

LExit:
    return hr;
}