Не правильно воспринимается путь Comdlg32.dll - VB

Узнай цену своей работы

Формулировка задачи:

На самом деле вопроса 2.

Вопрос 1

: Модуль:
Листинг программы
  1. Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  2. Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  3. Public Type OPENFILENAME
  4. lStructSize As Long
  5. hwndOwner As Long
  6. hInstance As Long
  7. lpstrFilter As String
  8. lpstrCustomFilter As String
  9. nMaxCustFilter As Long
  10. nFilterIndex As Long
  11. lpstrFile As String
  12. nMaxFile As Long
  13. lpstrFileTitle As String
  14. nMaxFileTitle As Long
  15. lpstrInitialDir As String
  16. lpstrTitle As String
  17. flags As Long
  18. nFileOffset As Integer
  19. nFileExtension As Integer
  20. lpstrDefExt As String
  21. lCustData As Long
  22. lpfnHook As Long
  23. lpTemplateName As String
  24. End Type
  25. Public Const OFN_ALLOWMULTISELECT = &H200
  26. Public Const OFN_CREATEPROMPT = &H2000
  27. Public Const OFN_DONTADDTORECENT = &H2000000
  28. Public Const OFN_ENABLEHOOK = &H20
  29. Public Const OFN_ENABLEINCLUDENOTIFY = &H400000
  30. Public Const OFN_ENABLESIZING = &H800000
  31. Public Const OFN_ENABLETEMPLATE = &H40
  32. Public Const OFN_ENABLETEMPLATEHANDLE = &H80
  33. Public Const OFN_EXPLORER = &H80000
  34. Public Const OFN_EXTENSIONDIFFERENT = &H400
  35. Public Const OFN_FILEMUSTEXIST = &H1000
  36. Public Const OFN_FORCESHOWHIDDEN = &H10000000
  37. Public Const OFN_HIDEREADONLY = &H4
  38. Public Const OFN_LONGNAMES = &H200000
  39. Public Const OFN_NOCHANGEDIR = &H8
  40. Public Const OFN_NODEREFERENCELINKS = &H100000
  41. Public Const OFN_NOLONGNAMES = &H40000
  42. Public Const OFN_NONETWORKBUTTON = &H20000
  43. Public Const OFN_NOREADONLYRETURN = &H8000
  44. Public Const OFN_NOTESTFILECREATE = &H10000
  45. Public Const OFN_NOVALIDATE = &H1000
  46. Public Const OFN_OVERWRITEPROMPT = &H2
  47. Public Const OFN_PATHMUSTEXIST = &H800
  48. Public Const OFN_READONLY = &H1
  49. Public Const OFN_SHAREAWARE = &H4000
  50. Public Const OFN_SHOWHELP = &H10
Форма:
Листинг программы
  1. Prvate Sub Form_Load()
  2. Dim OpenFile As OPENFILENAME
  3. Dim lReturn As Long
  4. Dim sFilter As String
  5. OpenFile.lStructSize = Len(OpenFile)
  6. OpenFile.hwndOwner = Me.HWND
  7. OpenFile.hInstance = App.hInstance
  8. sFilter = "ALL Files (*.*)" & Chr(0) & "*.*" & Chr(0)
  9. OpenFile.lpstrFilter = sFilter
  10. OpenFile.nFilterIndex = 1
  11. OpenFile.lpstrFile = String(65536, 0)
  12. OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
  13. OpenFile.lpstrFileTitle = OpenFile.lpstrFile
  14. OpenFile.nMaxFileTitle = OpenFile.nMaxFile
  15. OpenFile.lpstrInitialDir = App.Path
  16. OpenFile.lpstrTitle = "Выберите путь..."
  17. OpenFile.flags = &H80000 + &H4
  18. lReturn = GetOpenFileName(OpenFile)
  19. MsgBox OpenFile.lpstrFileName
  20. End Sub
Некоторые имена файлов, папок воспринимаются неверно. Но когда я сам вручную пишу то же самое название, воспринимается верно.Пример №1:Best Music Mix 2016 - ♫ 1H Gaming Music ♫ - Dubstep, Electro House, EDM, Trap.mp3. Вместо ♫ будет "d".Пример №2: дейтрейдер.pdf. Воспринимается как "дэи?треи?дер.pdf" Когда встречается неверное название файла, то программа, которая обрабатывает их, не может открыть для чтения и вылетает с ошибкой. Может что в коде неверно? Файлы открываются API CreateFile в режиме "для чтения".

Каким образом конвертировать в корректное имя?

Вопрос 2

: Допустим, имеется простенькая процедура работы с файлом:
Листинг программы
  1. Private Sub FileWorking()
  2. Open File$ For Append As #1
  3. Do
  4. Print #1, "rrr"
  5. If anything Then Exit Do
  6. Loop
  7. Close #1
  8. End Sub
Предположим, на форме есть кнопка паузы.

Каким образом приостановить работу с файлом и продолжить после отжатия паузы?

Естественно, чтобы форма не "замерзала", т.е. Sleep не подойдёт.

Решение задачи: «Не правильно воспринимается путь Comdlg32.dll»

textual
Листинг программы
  1. '
  2. ' modFile module by Alex Dragokas
  3. '
  4.  
  5. Option Explicit
  6.  
  7. Const MAX_PATH As Long = 260&
  8. Const MAX_FILE_SIZE As Currency = 104857600@
  9.  
  10. Enum VB_FILE_ACCESS_MODE
  11.     FOR_READ = 1
  12.     FOR_READ_WRITE = 2
  13.     FOR_OVERWRITE_CREATE = 4
  14. End Enum
  15.  
  16. Enum CACHE_TYPE
  17.     USE_CACHE
  18.     NO_CACHE
  19. End Enum
  20.  
  21. Private Type LARGE_INTEGER
  22.     LowPart As Long
  23.     HighPart As Long
  24. End Type
  25.  
  26. Private Type FILETIME
  27.    dwLowDateTime As Long
  28.    dwHighDateTime As Long
  29. End Type
  30.  
  31. Private Type WIN32_FIND_DATA
  32.     dwFileAttributes As Long
  33.     ftCreationTime As FILETIME
  34.     ftLastAccessTime As FILETIME
  35.     ftLastWriteTime As FILETIME
  36.     nFileSizeHigh As Long
  37.     nFileSizeLow As Long
  38.     dwReserved0 As Long
  39.     dwReserved1 As Long
  40.     lpszFileName(MAX_PATH) As Integer
  41.     lpszAlternate(14) As Integer
  42. End Type
  43.  
  44. Private Type SECURITY_ATTRIBUTES
  45.     nLength As Long
  46.     lpSecurityDescriptor As Long
  47.     bInheritHandle As Long
  48. End Type
  49.  
  50. Private Type VS_FIXEDFILEINFO
  51.     dwSignature As Long
  52.     dwStrucVersionl As Integer
  53.     dwStrucVersionh As Integer
  54.     dwFileVersionMSl As Integer
  55.     dwFileVersionMSh As Integer
  56.     dwFileVersionLSl As Integer
  57.     dwFileVersionLSh As Integer
  58.     dwProductVersionMSl As Integer
  59.     dwProductVersionMSh As Integer
  60.     dwProductVersionLSl As Integer
  61.     dwProductVersionLSh As Integer
  62.     dwFileFlagsMask As Long
  63.     dwFileFlags As Long
  64.     dwFileOS As Long
  65.     dwFileType As Long
  66.     dwFileSubtype As Long
  67.     dwFileDateMS As Long
  68.     dwFileDateLS As Long
  69. End Type
  70.  
  71. Private Declare Function PathFileExists Lib "Shlwapi.dll" Alias "PathFileExistsW" (ByVal pszPath As Long) As Long
  72. Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesW" (ByVal lpFileName As Long) As Long
  73. Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long
  74. Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
  75. Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
  76. Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  77. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
  78. 'Private Declare Function SHFileExists Lib "shell32.dll" Alias "#45" (ByVal szPath As String) As Long
  79. Private Declare Function Wow64DisableWow64FsRedirection Lib "kernel32.dll" (OldValue As Long) As Long
  80. Private Declare Function Wow64RevertWow64FsRedirection Lib "kernel32.dll" (ByVal OldValue As Long) As Long
  81. Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeW" (ByVal nDrive As Long) As Long
  82. Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
  83. Private Declare Function GetFileSizeEx Lib "kernel32.dll" (ByVal hFile As Long, lpFileSize As Any) As Long
  84. Private Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
  85. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToRead As Long, lpNumberOfByConstesRead As Long, ByVal lpOverlapped As Long) As Long
  86. Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
  87. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExW" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  88. Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExW" (ByVal hKey As Long, ByVal lpValueName As Long, ByVal lpReserved As Long, ByRef lpType As Long, szData As Long, ByRef lpcbData As Long) As Long
  89. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  90. Private Declare Function memcpy Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) As Long
  91. Private Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryW" (ByVal lpBuffer As Long, ByVal uSize As Long) As Long
  92. Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
  93. Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpStringDest As Long, ByVal lpStringSrc As Long) As Long
  94. Private Declare Function GetLongPathNameW Lib "kernel32" (ByVal lpszShortPath As Long, ByVal lpszLongPath As Long, ByVal cchBuffer As Long) As Long
  95. Private Declare Function GetFileVersionInfo Lib "version.dll" Alias "GetFileVersionInfoW" (ByVal lptstrFilename As Long, ByVal dwHandle As Long, ByVal dwLen As Long, lpData As Any) As Long
  96. Private Declare Function GetFileVersionInfoSize Lib "version.dll" Alias "GetFileVersionInfoSizeW" (ByVal lptstrFilename As Long, lpdwHandle As Long) As Long
  97. Private Declare Function VerQueryValue Lib "version.dll" Alias "VerQueryValueW" (pBlock As Any, ByVal lpSubBlock As Long, lplpBuffer As Long, puLen As Long) As Long
  98.  
  99.  
  100. Const FILE_SHARE_READ           As Long = &H1&
  101. Const FILE_SHARE_WRITE          As Long = &H2&
  102. Const FILE_SHARE_DELETE         As Long = 4&
  103. Const FILE_READ_ATTRIBUTES      As Long = &H80&
  104. Const OPEN_EXISTING             As Long = 3&
  105. Const CREATE_ALWAYS             As Long = 2&
  106. Const GENERIC_READ              As Long = &H80000000
  107. Const GENERIC_WRITE             As Long = &H40000000
  108. Const FILE_ATTRIBUTE_DIRECTORY  As Long = &H10&
  109. Const INVALID_HANDLE_VALUE      As Long = &HFFFFFFFF
  110. Const ERROR_SUCCESS             As Long = 0&
  111. Const INVALID_FILE_ATTRIBUTES   As Long = -1&
  112. Const NO_ERROR                  As Long = 0&
  113. Const FILE_BEGIN                As Long = 0&
  114. Const FILE_CURRENT              As Long = 1&
  115. Const FILE_END                  As Long = 2&
  116. Const INVALID_SET_FILE_POINTER  As Long = &HFFFFFFFF
  117.  
  118. Const DRIVE_FIXED               As Long = 3&
  119. Const DRIVE_RAMDISK             As Long = 6&
  120.  
  121. Const HKEY_LOCAL_MACHINE        As Long = &H80000002
  122. Const KEY_QUERY_VALUE           As Long = &H1&
  123. Const RegType_DWord             As Long = 4&
  124.  
  125. Const ch_Dot                    As String = "."
  126. Const ch_DotDot                 As String = ".."
  127. Const ch_Slash                  As String = ""
  128. Const ch_SlashAsterisk          As String = "\*"
  129.  
  130. Private lWow64Old               As Long
  131. Private DriveTypeName           As New Collection
  132. Private arrPathFolders()        As String
  133. Private arrPathFiles()          As String
  134. Private Total_Folders           As Long
  135. Private Total_Files             As Long
  136.  
  137.  
  138.  
  139. Public Function FileExists(ByVal sFile$, Optional bUseWow64 As Boolean) As Boolean
  140.     On Error GoTo ErrorHandler:
  141.     Dim Redirect As Boolean
  142.    
  143.     sFile = Trim$(sFile)
  144.     If Len(sFile) = 0 Then Exit Function
  145.     If Left$(sFile, 2) = "" Then Exit Function 'DriveType = "REMOTE"
  146.    
  147.     ' use 2 methods for reliability reason (both supported unicode pathes)
  148.    Dim Ex(1) As Boolean
  149.     Dim ret As Long
  150.    
  151.     Dim WFD     As WIN32_FIND_DATA
  152.     Dim hFile   As Long
  153.    
  154.     If Not bUseWow64 Then Redirect = ToggleWow64FSRedirection(False, sFile)
  155.    
  156.     ret = GetFileAttributes(StrPtr(sFile))
  157.     If ret <> INVALID_HANDLE_VALUE And (0 = (ret And FILE_ATTRIBUTE_DIRECTORY)) Then Ex(0) = True
  158.  
  159.     hFile = FindFirstFile(StrPtr(sFile), WFD)
  160.     Ex(1) = (hFile <> INVALID_HANDLE_VALUE) And Not CBool(WFD.dwFileAttributes And vbDirectory)
  161.     FindClose hFile
  162.  
  163.     ' // here must be enabling of FS redirector
  164.    If Redirect Then Call ToggleWow64FSRedirection(True)
  165.  
  166.     FileExists = Ex(0) Or Ex(1)
  167.     Exit Function
  168. ErrorHandler:
  169.     ErrorMsg Err, "modFile.FileExists", "File:", sFile$
  170.     If inIDE Then Stop: Resume Next
  171. End Function
  172.  
  173. Public Function FolderExists(ByVal sFolder$, Optional ForceUnderRedirection As Boolean) As Boolean
  174.     On Error GoTo ErrorHandler:
  175.    
  176.     Dim ret As Long
  177.     sFolder = Trim$(sFolder)
  178.     If Len(sFolder) = 0 Then Exit Function
  179.     If Left$(sFolder, 2) = "" Then Exit Function 'network path
  180.    
  181.     '// FS redirection checking
  182.    
  183.     ret = GetFileAttributes(StrPtr(sFolder))
  184.     FolderExists = CBool(ret And vbDirectory) And (ret <> INVALID_FILE_ATTRIBUTES)
  185.    
  186.     '// FS redirection enambling
  187.    
  188.     Exit Function
  189. ErrorHandler:
  190.     ErrorMsg Err, "modFile.FolderExists", "Folder:", sFolder$, "Redirection: ", ForceUnderRedirection
  191.     If inIDE Then Stop: Resume Next
  192. End Function
  193.  
  194.  
  195. Public Sub GetDriveTypeNames()
  196.     On Error GoTo ErrorHandler
  197.     Dim lr As Long
  198.     Dim i  As Long
  199.     Dim DT As String
  200.  
  201.     For i = 65& To 90&
  202.  
  203.       lr = GetDriveType(StrPtr(Chr$(i) & ":"))
  204.  
  205.       Select Case lr
  206.         Case 3&
  207.             DT = "FIXED"
  208.         Case 2&
  209.             DT = "REMOVABLE"
  210.         Case 5&
  211.             DT = "CDROM"
  212.         Case 4&
  213.             DT = "REMOTE"
  214.         Case 0&
  215.             DT = "UNKNOWN"
  216.         Case 1&
  217.             DT = "DISCONNECTED" '"NO_ROOT_DIR"
  218.        Case 6&
  219.             DT = "RAMDISK"
  220.         Case Else
  221.             DT = "UNKNOWN"
  222.       End Select
  223.  
  224.       DriveTypeName.Add DT, Chr$(i)
  225.  
  226.     Next
  227.  
  228.     Exit Sub
  229. ErrorHandler:
  230.     ErrorMsg Err, "modFile.GetDriveTypeNames", "Drive:", Chr$(i)
  231. End Sub
  232.  
  233.  
  234. Function FileLenW(Path As String) As Currency ', Optional DoNotUseCache As Boolean
  235.    On Error GoTo ErrorHandler
  236.    
  237.     Dim lr          As Long
  238.     Dim hFile       As Long
  239.     Dim FileSize    As Currency
  240.  
  241.     hFile = CreateFile(StrPtr(Path), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
  242.    
  243.     If hFile Then
  244.         lr = GetFileSizeEx(hFile, FileSize)
  245.         If lr Then
  246.             If FileSize < 10000000000@ Then FileLenW = FileSize * 10000&
  247.         End If
  248.         CloseHandle hFile: hFile = 0&
  249.     End If
  250.     Exit Function
  251. ErrorHandler:
  252.     ErrorMsg Err, "modFile.FileLenW", "File:", Path, "hFile:", hFile, "FileSize:", FileSize, "Return:", lr
  253. End Function

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4.4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут