Example #1
0
/********************************************************************
StrAllocString - allocates or reuses dynamic string memory and copies in an existing string

NOTE: caller is responsible for freeing ppwz even if function fails
NOTE: cchSource does not have to equal the length of wzSource
NOTE: if cchSource == 0, length of wzSource is used instead
********************************************************************/
extern "C" HRESULT DAPI StrAllocString(
	__inout LPWSTR* ppwz,
	__in LPCWSTR wzSource,
	__in DWORD_PTR cchSource
	)
{
	Assert(ppwz && wzSource); // && *wzSource);

	HRESULT hr = S_OK;
	DWORD_PTR cch = 0;

	if (*ppwz)
	{
		cch = MemSize(*ppwz);  // get the count in bytes so we can check if it failed (returns -1)
		if (-1 == cch)
			ExitOnFailure(hr = E_INVALIDARG, "failed to get size of destination string");
		cch /= sizeof(WCHAR);  //convert the count in bytes to count in characters
	}

	if (0 == cchSource)
		cchSource = lstrlenW(wzSource);

	if (cch < cchSource + 1)
	{
		cch = cchSource + 1;   // add one for the null terminator
		hr = StrAlloc(ppwz, cch);
		ExitOnFailure1(hr, "failed to allocate string from string: %S", wzSource);
	}

	// copy everything (the NULL terminator will be included)
	hr = StringCchCopyNExW(*ppwz, cch, wzSource, cchSource, NULL, NULL, STRSAFE_FILL_BEHIND_NULL);

LExit:
	return hr;
}
Example #2
0
HRESULT StringCchCopyExW(
        LPWSTR pszDest,
        size_t cchDest,
        LPCWSTR pszSrc,
        LPWSTR *ppszDestEnd,
        size_t * pcchRemaining,
        DWORD dwFlags){
    return StringCchCopyNExW(pszDest, cchDest, pszSrc, cchDest,
            ppszDestEnd, pcchRemaining, dwFlags);
}