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