@@ -392,41 +392,41 @@ internal final class WindowsRemoteProcess: RemoteProcess {
392
392
}
393
393
394
394
private func allocateDllPathRemote( ) -> UnsafeMutableRawPointer ? {
395
- // The path to the dll assuming it's in the same directory as swift-inspect.
396
- let swiftInspectPath = ProcessInfo . processInfo. arguments [ 0 ]
397
- return URL ( fileURLWithPath: swiftInspectPath)
395
+ URL ( fileURLWithPath: ProcessInfo . processInfo. arguments [ 0 ] )
398
396
. deletingLastPathComponent ( )
399
397
. appendingPathComponent ( " SwiftInspectClient.dll " )
400
- . withUnsafeFileSystemRepresentation {
401
- #"\\?\ \#( String ( decodingCString: unsafeBitCast ( $0!, to: UnsafePointer< UInt8> . self ) , as: UTF8 . self) ) "#
402
- . withCString ( encodedAs: UTF16 . self) {
403
- // Check that the dll file exists
404
- var faAttributes : WIN32_FILE_ATTRIBUTE_DATA = . init( )
405
- guard GetFileAttributesExW ( $0, GetFileExInfoStandard, & faAttributes) ,
406
- faAttributes. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_REPARSE_POINT) == 0
407
- else {
408
- print ( " \( $0) doesn't exist " )
409
- return nil
410
- }
411
- // Allocate memory in the remote process
412
- let szLength = SIZE_T ( wcslen ( $0) * MemoryLayout < WCHAR > . size + 1 )
413
- guard
414
- let allocation = VirtualAllocEx (
415
- self . process, nil , szLength,
416
- DWORD ( MEM_COMMIT) , DWORD ( PAGE_READWRITE) )
417
- else {
418
- print ( " VirtualAllocEx failed \( GetLastError ( ) ) " )
419
- return nil
420
- }
421
- // Write the path in the allocated memory
422
- if !WriteProcessMemory( self . process, allocation, $0, szLength, nil ) {
423
- print ( " WriteProcessMemory failed \( GetLastError ( ) ) " )
424
- VirtualFreeEx ( self . process, allocation, 0 , DWORD ( MEM_RELEASE) )
425
- return nil
426
- }
427
-
428
- return allocation
398
+ . path
399
+ . withCString ( encodedAs: UTF16 . self) { pwszPath in
400
+ let dwLength = GetFullPathNameW ( pwszPath, 0 , nil , nil )
401
+ return withUnsafeTemporaryAllocation ( of: WCHAR . self, capacity: Int ( dwLength) ) {
402
+ guard GetFullPathNameW ( pwszPath, dwLength, $0. baseAddress, nil ) == dwLength - 1 else { return nil }
403
+
404
+ var faAttributes : WIN32_FILE_ATTRIBUTE_DATA = . init( )
405
+ guard GetFileAttributesExW ( $0. baseAddress, GetFileExInfoStandard, & faAttributes) else {
406
+ print ( " \( String ( decodingCString: $0. baseAddress!, as: UTF16 . self) ) doesn't exist " )
407
+ return nil
429
408
}
409
+ guard faAttributes. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_REPARSE_POINT) == 0 else {
410
+ print ( " \( String ( decodingCString: $0. baseAddress!, as: UTF16 . self) ) doesn't exist " )
411
+ return nil
412
+ }
413
+
414
+ let szLength = SIZE_T ( Int ( dwLength) * MemoryLayout< WCHAR> . size)
415
+ guard let pAllocation =
416
+ VirtualAllocEx ( self . process, nil , szLength,
417
+ DWORD ( MEM_COMMIT) , DWORD ( PAGE_READWRITE) ) else {
418
+ print ( " VirtualAllocEx failed \( GetLastError ( ) ) " )
419
+ return nil
420
+ }
421
+
422
+ if !WriteProcessMemory( self . process, pAllocation, $0. baseAddress, szLength, nil ) {
423
+ print ( " WriteProcessMemory failed \( GetLastError ( ) ) " )
424
+ _ = VirtualFreeEx ( self . process, pAllocation, 0 , DWORD ( MEM_RELEASE) )
425
+ return nil
426
+ }
427
+
428
+ return pAllocation
429
+ }
430
430
}
431
431
}
432
432
@@ -449,7 +449,7 @@ internal final class WindowsRemoteProcess: RemoteProcess {
449
449
// Unload the dll from the remote process
450
450
let hUnloadThread = CreateRemoteThread (
451
451
self . process, nil , 0 , freeLibraryAddr,
452
- unsafeBitCast ( hDllModule, to : LPVOID . self ) , 0 , nil )
452
+ UnsafeMutableRawPointer ( hDllModule) , 0 , nil )
453
453
if hUnloadThread == HANDLE ( bitPattern: 0 ) {
454
454
print ( " CreateRemoteThread for unload failed \( GetLastError ( ) ) " )
455
455
return false
0 commit comments