Ruby-DOS(Win32)路径名到NT(本机)路径名

Ruby-DOS(Win32)路径名到NT(本机)路径名,ruby,pathname,Ruby,Pathname,我需要从Ruby(1.9.3)中的DOS(Win32)路径获取文件的NT(本机)路径 意思是,我有字符串: dos_path = "C:\Windows\regedit.exe" 但我需要: nt_path = "\Device\HarddiskVolume1\Windows\regedit.exe" 有什么办法吗? 谢谢 可以使用该函数将MS-DOS设备名转换为NT路径。从Ruby调用这样一个函数可以使用。从1.9.3开始,它就是Ruby标准库的一部分。但是,以下示例仅适用于2.0.0或更

我需要从Ruby(1.9.3)中的DOS(Win32)路径获取文件的NT(本机)路径

意思是,我有字符串:

dos_path = "C:\Windows\regedit.exe"
但我需要:

nt_path = "\Device\HarddiskVolume1\Windows\regedit.exe"
有什么办法吗?
谢谢

可以使用该函数将MS-DOS设备名转换为NT路径。从Ruby调用这样一个函数可以使用。从1.9.3开始,它就是Ruby标准库的一部分。但是,以下示例仅适用于2.0.0或更新版本

require 'fiddle/import'
require 'fiddle/types'

module DOS2NT
  extend Fiddle::Importer # makes function importing easier
  dlload 'kernel32.dll'
  include Fiddle::Win32Types # so DWORD can be used instead of unsigned long
  extern 'DWORD QueryDosDeviceW(void*, void*, DWORD)', :stdcall
  extern 'DWORD GetLastError()', :stdcall
  ERROR_INSUFFICIENT_BUFFER = 122

  SIZEOF_WCHAR = 2 # sizeof(WCHAR) on Windows

  # a generic error class
  class Error < StandardError
  end

  def self.dos_device_name_to_path(devicename)
    initial_len = 256
    grow_factor = 2
    # we care about Unicode (Windows uses UTF-16LE)
    devicename.encode!(Encoding::UTF_16LE)
    # create buffer
    buf = "\0\0".force_encoding(Encoding::UTF_16LE) * initial_len
    # call QueryDosDeviceW until the call was successful
    while (written_chars = QueryDosDeviceW(devicename, buf, buf.length)) == 0
      # it wasn't
      case (error = GetLastError())
      # resize buffer as it was too short
      when ERROR_INSUFFICIENT_BUFFER
        buf *= grow_factor
      # other errors like ERROR_FILE_NOT_FOUND (2)
      else
        raise Error, "QueryDosDeviceW failed (GetLastError returned #{error})"
      end
    end
    # truncate buffer (including the null character)
    path = buf[0, written_chars - SIZEOF_WCHAR]
    # transcode the path to UTF-8 as that's usually more useful
    path.encode!(Encoding::UTF_8)
  end
end
# example strings from the question
dos_path = 'C:\Windows\regedit.exe'
nt_path = '\Device\HarddiskVolume1\Windows\regedit.exe'
# convert and print inspected result
p dos_path.sub(/\A[A-Z]:/i) { |m| DOS2NT.dos_device_name_to_path(m) } # => "\\Device\\HarddiskVolume1\\Windows\\regedit.exe"
require'fiddle/import'
需要“提琴/打字”
模块DOS2NT
extend Fiddle::Importer#使函数导入更容易
dlload'kernel32.dll'
include Fiddle::Win32Types#因此可以使用DWORD而不是unsigned long
外部'DWORD QueryDosDeviceW(void*,void*,DWORD)',:stdcall
外部“DWORD GetLastError()”,:stdcall
错误\u缓冲区不足\u=122
SIZEOF_WCHAR=2#Windows上的SIZEOF(WCHAR)
#泛型错误类
类错误<标准错误
结束
def self.dos\u设备名称\u到路径(devicename)
初始长度=256
增长系数=2
#我们关心Unicode(Windows使用UTF-16LE)
devicename.encode!(编码::UTF_16LE)
#创建缓冲区
buf=“\0\0”。强制编码(编码::UTF\U 16LE)*初始长度
#调用QueryDosDeviceW,直到调用成功
而(writed_chars=QueryDosDeviceW(devicename,buf,buf.length))==0
#不是
案例(错误=GetLastError())
#调整缓冲区大小,因为它太短
当错误\u缓冲区不足\u时
buf*=增长系数
#其他错误,如找不到错误文件(2)
其他的
引发错误,“QueryDosDeviceW失败(GetLastError返回#{Error})”
结束
结束
#截断缓冲区(包括空字符)
path=buf[0,写入字符-大小为
#将路径转换为UTF-8,因为这通常更有用
路径编码!(编码::UTF_8)
结束
结束
#问题中的字符串示例
dos_路径='C:\Windows\regedit.exe'
nt\U路径='\Device\HarddiskVolume1\Windows\regedit.exe'
#转换并打印检查结果
p dos_path.sub(/\A[A-Z]:/i){m|DOS2NT.dos_device_name_to_path(m)}#=>“\\device\\HarddiskVolume1\\Windows\\regedit.exe”

您需要获取路径或转换路径的方法吗?你能用“\Device\HarddiskVolume1”替换“C:\”吗?当然,我需要一种转换路径的方法。。我有完整的DOS路径,我需要在不事先知道本机路径调用是什么(使用FIDLE或Win32API)的情况下转换它,然后再转换为
String\sub
。你需要一个例子吗?@cremno会很感激的:)