-
- if(NT_SUCCESS(Status))
- {
- /* now, let's allocate some anonymous memory to copy the string to.
- we can't just copy it to the buffer the caller pointed as it might
- be user memory in another context */
- PWSTR PathCopy = ExAllocatePool(PagedPool, LocalDest.Length + sizeof(WCHAR));
- if(PathCopy != NULL)
- {
- /* make a copy of the buffer to the temporary buffer */
- _SEH_TRY
- {
- RtlCopyMemory(PathCopy, StrSource, LocalDest.Length);
- PathCopy[LocalDest.Length / sizeof(WCHAR)] = L'\0';
- }
- _SEH_HANDLE
- {
- Status = _SEH_GetExceptionCode();
- }
- _SEH_END;
-
- /* detach from the process */
- if(Attached)
- KeDetachProcess();
-
- /* only copy the string back to the caller if we were able to
- copy it into the temporary buffer! */
- if(NT_SUCCESS(Status))
- {
- /* now let's copy the buffer back to the caller */
- _SEH_TRY
- {
- *DstPath = LocalDest;
- RtlCopyMemory(LocalDest.Buffer, PathCopy, LocalDest.Length + sizeof(WCHAR));
- if (ReturnLength)
- {
- *ReturnLength = sizeof(UNICODE_STRING) + LocalDest.Length + sizeof(WCHAR);
- }
- }
- _SEH_HANDLE
- {
- Status = _SEH_GetExceptionCode();
- }
- _SEH_END;
- }
-
- /* we're done with the copy operation, free the temporary kernel buffer */
- ExFreePool(PathCopy);
-
- /* we need to bail because we're already detached from the process */
- break;
- }
- else
- {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- }
- }
- }