void testDestEnd(){ wchar_t dest[11]; wchar_t * destEnd; diag("Test calculation of destEnd."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"Data", &destEnd, NULL, 0)), "Test calculation of destEnd " "while copying short string."); is_wstring(L"Data", dest, "Result of copying short string."); ok(destEnd == &dest[4], "Value of destEnd after copying short string."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"", &destEnd, NULL, 0)), "Test calculation of destEnd " "while copying empty string."); is_wstring(L"", dest, "Result of copying empty string."); ok(destEnd == &dest[0], "Value of destEnd after copying empty string."); ok(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"longer string", &destEnd, NULL, 0) == STRSAFE_E_INSUFFICIENT_BUFFER, "Test calculation of destEnd " "while copying a too long string."); is_wstring(L"longer str", dest, "Result of copying a too long string."); ok(destEnd == &dest[10], "Value of destEnd after copying a too long string."); }
void testRemaining(){ wchar_t dest[11]; size_t remaining; diag("Test calculation of remaining space."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"STR", NULL, &remaining, 0)), "Test calculation of remaining space " "while copying short string."); is_wstring(L"STR", dest, "Result of copying short string."); is_int(8 * sizeof(wchar_t), remaining, "Number of remaining characters after copying short string."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"", NULL, &remaining, 0)), "Test calculation of remaining space " "while copying empty string."); is_wstring(L"", dest, "Result of copying empty string."); is_int(11 * sizeof(wchar_t), remaining, "Number of remaining characters after copying empty string."); ok(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"too long string", NULL, &remaining, 0) == STRSAFE_E_INSUFFICIENT_BUFFER, "Test calculation of remaining space " "while copying a too long string."); is_wstring(L"too long s", dest, "Result of copying a too long string."); is_int(1 * sizeof(wchar_t), remaining, "Number of remaining characters after copying " "a too long string."); }
HRESULT GetNTObjectSymbolicLinkTarget(LPCWSTR path, LPCWSTR entryName, PUNICODE_STRING LinkTarget) { HANDLE handle; WCHAR buffer[MAX_PATH]; LPWSTR pend = buffer; StringCbCopyExW(buffer, sizeof(buffer), path, &pend, NULL, 0); if (pend[-1] != '\\') { *pend++ = '\\'; *pend = 0; } StringCbCatW(buffer, sizeof(buffer), entryName); DbgPrint("GetNTObjectSymbolicLinkTarget %d\n", buffer); LinkTarget->Length = 0; DWORD err = NtOpenObject(SYMBOLICLINK_OBJECT, &handle, SYMBOLIC_LINK_QUERY, buffer); if (!NT_SUCCESS(err)) return HRESULT_FROM_NT(err); err = NtQuerySymbolicLinkObject(handle, LinkTarget, NULL); if (!NT_SUCCESS(err)) return HRESULT_FROM_NT(err); NtClose(handle); return S_OK; }
int main(void){ wchar_t dest[11]; plan(32); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"test", NULL, NULL, 0)), "Copy short string without any extended functionality."); is_wstring(L"test", dest, "Result of copying short string."); testDestEnd(); testRemaining(); testFlags(); return 0; }
VOID WINAPI StreamingDeviceSetupW(IN HWND hwnd, IN HINSTANCE hinst, IN LPWSTR lpszCmdLine, IN int nCmdShow) { DWORD Length, dwResult; LPWSTR pCmdLine; LPWSTR pStr; GUID Guids[2]; WCHAR DevicePath[MAX_PATH]; HRESULT hResult; DWORD Index; Length = (wcslen(lpszCmdLine) + 1) * sizeof(WCHAR); pCmdLine = (LPWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Length); if (pCmdLine == NULL) { // no memory return; } hResult = StringCbCopyExW(pCmdLine, Length, lpszCmdLine, NULL, NULL, STRSAFE_NULL_ON_FAILURE); if (hResult != S_OK) { // failed HeapFree(GetProcessHeap(), 0, pCmdLine); return; } pStr = wcstok(pCmdLine, L",\t\""); Index = 0; do { if (pStr == NULL) { // invalid parameter HeapFree(GetProcessHeap(), 0, pCmdLine); return; } hResult = IIDFromString(pStr, &Guids[Index]); if (hResult != S_OK) { // invalid parameter HeapFree(GetProcessHeap(), 0, pCmdLine); return; } Index++; pStr = wcstok(NULL, L",\t\""); }while(Index < 2); dwResult = InstallSoftwareDeviceInterface(&Guids[0], &Guids[1], pStr); if (dwResult == ERROR_SUCCESS) { pStr = wcstok(NULL, L",\t\""); if (pStr != NULL) { wcscpy(DevicePath, pStr); pStr = wcstok(NULL, L",\t\""); if (pStr != NULL) { dwResult = InstallSoftwareDeviceInterfaceInf(DevicePath, pStr); } } } }
void testFlags(){ wchar_t dest[11]; wchar_t * wanted; diag("Test the STRSAFE_IGNORE_NULLS flag."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), NULL, NULL, NULL, STRSAFE_IGNORE_NULLS)), "Test copying a NULL string."); is_wstring(L"", dest, "Result of copying a NULL string."); diag("Test the STRSAFE_FILL_BEHIND_NULL flag."); ok(SUCCEEDED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"testing", NULL, NULL, STRSAFE_FILL_BEHIND_NULL | '@')), "Test filling with '@' behind null termination."); is_wstring(L"testing", dest, "Result of copying and filling behind null termination."); wanted = malloc(3 * sizeof(wchar_t)); if(wanted == NULL){ bail("Memory allocation failed."); } memset(wanted, '@', 3 * sizeof(wchar_t)); ok(memcmp(&dest[8], wanted, 3 * sizeof(wchar_t)) == 0, "Correct data filled after null termination."); diag("Test the STRSAFE_FILL_ON_FAILURE flag."); ok(FAILED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"too much data", NULL, NULL, STRSAFE_FILL_ON_FAILURE | '@')), "Test filling with '@' on failure."); wanted = malloc(10 * sizeof(wchar_t)); if(wanted == NULL){ bail("Memory allocation failed."); } memset(wanted, '@', 10 * sizeof(wchar_t)); ok(memcmp(dest, wanted, 10 * sizeof(wchar_t)) == 0, "Result of filling with '@' on failure."); ok(dest[10] == L'\0', "Check null termination at end of filled buffer."); free(wanted); diag("Test the STRSAFE_NULL_ON_FAILURE flag."); ok(FAILED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"Also too much", NULL, NULL, STRSAFE_NULL_ON_FAILURE)), "Test nulling string on failure."); is_wstring(L"", dest, "Result when nulling string on failure."); diag("Test the STRSAFE_NO_TRUNCATION flag."); ok(FAILED(StringCbCopyExW(dest, 11 * sizeof(wchar_t), L"Won't fit in dest", NULL, NULL, STRSAFE_NO_TRUNCATION)), "Test copying with truncating disabled."); is_wstring(L"", dest, "Result after copying with truncating disabled."); }