Не правильно воспринимается путь Comdlg32.dll - VB
Формулировка задачи:
На самом деле вопроса 2.
Форма:
Некоторые имена файлов, папок воспринимаются неверно. Но когда я сам вручную пишу то же самое название, воспринимается верно.Пример №1:Best Music Mix 2016 - ♫ 1H Gaming Music ♫ - Dubstep, Electro House, EDM, Trap.mp3. Вместо ♫ будет "d".Пример №2: дейтрейдер.pdf. Воспринимается как "дэи?треи?дер.pdf"
Когда встречается неверное название файла, то программа, которая обрабатывает их, не может открыть для чтения и вылетает с ошибкой. Может что в коде неверно? Файлы открываются API CreateFile в режиме "для чтения".
Предположим, на форме есть кнопка паузы.
Вопрос 1
: Модуль:
Листинг программы
- Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
- Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
- Public Type OPENFILENAME
- lStructSize As Long
- hwndOwner As Long
- hInstance As Long
- lpstrFilter As String
- lpstrCustomFilter As String
- nMaxCustFilter As Long
- nFilterIndex As Long
- lpstrFile As String
- nMaxFile As Long
- lpstrFileTitle As String
- nMaxFileTitle As Long
- lpstrInitialDir As String
- lpstrTitle As String
- flags As Long
- nFileOffset As Integer
- nFileExtension As Integer
- lpstrDefExt As String
- lCustData As Long
- lpfnHook As Long
- lpTemplateName As String
- End Type
- Public Const OFN_ALLOWMULTISELECT = &H200
- Public Const OFN_CREATEPROMPT = &H2000
- Public Const OFN_DONTADDTORECENT = &H2000000
- Public Const OFN_ENABLEHOOK = &H20
- Public Const OFN_ENABLEINCLUDENOTIFY = &H400000
- Public Const OFN_ENABLESIZING = &H800000
- Public Const OFN_ENABLETEMPLATE = &H40
- Public Const OFN_ENABLETEMPLATEHANDLE = &H80
- Public Const OFN_EXPLORER = &H80000
- Public Const OFN_EXTENSIONDIFFERENT = &H400
- Public Const OFN_FILEMUSTEXIST = &H1000
- Public Const OFN_FORCESHOWHIDDEN = &H10000000
- Public Const OFN_HIDEREADONLY = &H4
- Public Const OFN_LONGNAMES = &H200000
- Public Const OFN_NOCHANGEDIR = &H8
- Public Const OFN_NODEREFERENCELINKS = &H100000
- Public Const OFN_NOLONGNAMES = &H40000
- Public Const OFN_NONETWORKBUTTON = &H20000
- Public Const OFN_NOREADONLYRETURN = &H8000
- Public Const OFN_NOTESTFILECREATE = &H10000
- Public Const OFN_NOVALIDATE = &H1000
- Public Const OFN_OVERWRITEPROMPT = &H2
- Public Const OFN_PATHMUSTEXIST = &H800
- Public Const OFN_READONLY = &H1
- Public Const OFN_SHAREAWARE = &H4000
- Public Const OFN_SHOWHELP = &H10
Листинг программы
- Prvate Sub Form_Load()
- Dim OpenFile As OPENFILENAME
- Dim lReturn As Long
- Dim sFilter As String
- OpenFile.lStructSize = Len(OpenFile)
- OpenFile.hwndOwner = Me.HWND
- OpenFile.hInstance = App.hInstance
- sFilter = "ALL Files (*.*)" & Chr(0) & "*.*" & Chr(0)
- OpenFile.lpstrFilter = sFilter
- OpenFile.nFilterIndex = 1
- OpenFile.lpstrFile = String(65536, 0)
- OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
- OpenFile.lpstrFileTitle = OpenFile.lpstrFile
- OpenFile.nMaxFileTitle = OpenFile.nMaxFile
- OpenFile.lpstrInitialDir = App.Path
- OpenFile.lpstrTitle = "Выберите путь..."
- OpenFile.flags = &H80000 + &H4
- lReturn = GetOpenFileName(OpenFile)
- MsgBox OpenFile.lpstrFileName
- End Sub
Каким образом конвертировать в корректное имя?
Вопрос 2
: Допустим, имеется простенькая процедура работы с файлом:
Листинг программы
- Private Sub FileWorking()
- Open File$ For Append As #1
- Do
- Print #1, "rrr"
- If anything Then Exit Do
- Loop
- Close #1
- End Sub
Каким образом приостановить работу с файлом и продолжить после отжатия паузы?
Естественно, чтобы форма не "замерзала", т.е. Sleep не подойдёт.Решение задачи: «Не правильно воспринимается путь Comdlg32.dll»
textual
Листинг программы
- '
- ' modFile module by Alex Dragokas
- '
- Option Explicit
- Const MAX_PATH As Long = 260&
- Const MAX_FILE_SIZE As Currency = 104857600@
- Enum VB_FILE_ACCESS_MODE
- FOR_READ = 1
- FOR_READ_WRITE = 2
- FOR_OVERWRITE_CREATE = 4
- End Enum
- Enum CACHE_TYPE
- USE_CACHE
- NO_CACHE
- End Enum
- Private Type LARGE_INTEGER
- LowPart As Long
- HighPart As Long
- End Type
- Private Type FILETIME
- dwLowDateTime As Long
- dwHighDateTime As Long
- End Type
- Private Type WIN32_FIND_DATA
- dwFileAttributes As Long
- ftCreationTime As FILETIME
- ftLastAccessTime As FILETIME
- ftLastWriteTime As FILETIME
- nFileSizeHigh As Long
- nFileSizeLow As Long
- dwReserved0 As Long
- dwReserved1 As Long
- lpszFileName(MAX_PATH) As Integer
- lpszAlternate(14) As Integer
- End Type
- Private Type SECURITY_ATTRIBUTES
- nLength As Long
- lpSecurityDescriptor As Long
- bInheritHandle As Long
- End Type
- Private Type VS_FIXEDFILEINFO
- dwSignature As Long
- dwStrucVersionl As Integer
- dwStrucVersionh As Integer
- dwFileVersionMSl As Integer
- dwFileVersionMSh As Integer
- dwFileVersionLSl As Integer
- dwFileVersionLSh As Integer
- dwProductVersionMSl As Integer
- dwProductVersionMSh As Integer
- dwProductVersionLSl As Integer
- dwProductVersionLSh As Integer
- dwFileFlagsMask As Long
- dwFileFlags As Long
- dwFileOS As Long
- dwFileType As Long
- dwFileSubtype As Long
- dwFileDateMS As Long
- dwFileDateLS As Long
- End Type
- Private Declare Function PathFileExists Lib "Shlwapi.dll" Alias "PathFileExistsW" (ByVal pszPath As Long) As Long
- Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesW" (ByVal lpFileName As Long) As Long
- Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileW" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long
- Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileW" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
- Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
- 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
- Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
- 'Private Declare Function SHFileExists Lib "shell32.dll" Alias "#45" (ByVal szPath As String) As Long
- Private Declare Function Wow64DisableWow64FsRedirection Lib "kernel32.dll" (OldValue As Long) As Long
- Private Declare Function Wow64RevertWow64FsRedirection Lib "kernel32.dll" (ByVal OldValue As Long) As Long
- Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeW" (ByVal nDrive As Long) As Long
- Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
- Private Declare Function GetFileSizeEx Lib "kernel32.dll" (ByVal hFile As Long, lpFileSize As Any) As Long
- Private Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
- 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
- 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
- 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
- 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
- Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
- Private Declare Function memcpy Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) As Long
- Private Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryW" (ByVal lpBuffer As Long, ByVal uSize As Long) As Long
- Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
- Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpStringDest As Long, ByVal lpStringSrc As Long) As Long
- Private Declare Function GetLongPathNameW Lib "kernel32" (ByVal lpszShortPath As Long, ByVal lpszLongPath As Long, ByVal cchBuffer As Long) As Long
- 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
- Private Declare Function GetFileVersionInfoSize Lib "version.dll" Alias "GetFileVersionInfoSizeW" (ByVal lptstrFilename As Long, lpdwHandle As Long) As Long
- Private Declare Function VerQueryValue Lib "version.dll" Alias "VerQueryValueW" (pBlock As Any, ByVal lpSubBlock As Long, lplpBuffer As Long, puLen As Long) As Long
- Const FILE_SHARE_READ As Long = &H1&
- Const FILE_SHARE_WRITE As Long = &H2&
- Const FILE_SHARE_DELETE As Long = 4&
- Const FILE_READ_ATTRIBUTES As Long = &H80&
- Const OPEN_EXISTING As Long = 3&
- Const CREATE_ALWAYS As Long = 2&
- Const GENERIC_READ As Long = &H80000000
- Const GENERIC_WRITE As Long = &H40000000
- Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10&
- Const INVALID_HANDLE_VALUE As Long = &HFFFFFFFF
- Const ERROR_SUCCESS As Long = 0&
- Const INVALID_FILE_ATTRIBUTES As Long = -1&
- Const NO_ERROR As Long = 0&
- Const FILE_BEGIN As Long = 0&
- Const FILE_CURRENT As Long = 1&
- Const FILE_END As Long = 2&
- Const INVALID_SET_FILE_POINTER As Long = &HFFFFFFFF
- Const DRIVE_FIXED As Long = 3&
- Const DRIVE_RAMDISK As Long = 6&
- Const HKEY_LOCAL_MACHINE As Long = &H80000002
- Const KEY_QUERY_VALUE As Long = &H1&
- Const RegType_DWord As Long = 4&
- Const ch_Dot As String = "."
- Const ch_DotDot As String = ".."
- Const ch_Slash As String = ""
- Const ch_SlashAsterisk As String = "\*"
- Private lWow64Old As Long
- Private DriveTypeName As New Collection
- Private arrPathFolders() As String
- Private arrPathFiles() As String
- Private Total_Folders As Long
- Private Total_Files As Long
- Public Function FileExists(ByVal sFile$, Optional bUseWow64 As Boolean) As Boolean
- On Error GoTo ErrorHandler:
- Dim Redirect As Boolean
- sFile = Trim$(sFile)
- If Len(sFile) = 0 Then Exit Function
- If Left$(sFile, 2) = "" Then Exit Function 'DriveType = "REMOTE"
- ' use 2 methods for reliability reason (both supported unicode pathes)
- Dim Ex(1) As Boolean
- Dim ret As Long
- Dim WFD As WIN32_FIND_DATA
- Dim hFile As Long
- If Not bUseWow64 Then Redirect = ToggleWow64FSRedirection(False, sFile)
- ret = GetFileAttributes(StrPtr(sFile))
- If ret <> INVALID_HANDLE_VALUE And (0 = (ret And FILE_ATTRIBUTE_DIRECTORY)) Then Ex(0) = True
- hFile = FindFirstFile(StrPtr(sFile), WFD)
- Ex(1) = (hFile <> INVALID_HANDLE_VALUE) And Not CBool(WFD.dwFileAttributes And vbDirectory)
- FindClose hFile
- ' // here must be enabling of FS redirector
- If Redirect Then Call ToggleWow64FSRedirection(True)
- FileExists = Ex(0) Or Ex(1)
- Exit Function
- ErrorHandler:
- ErrorMsg Err, "modFile.FileExists", "File:", sFile$
- If inIDE Then Stop: Resume Next
- End Function
- Public Function FolderExists(ByVal sFolder$, Optional ForceUnderRedirection As Boolean) As Boolean
- On Error GoTo ErrorHandler:
- Dim ret As Long
- sFolder = Trim$(sFolder)
- If Len(sFolder) = 0 Then Exit Function
- If Left$(sFolder, 2) = "" Then Exit Function 'network path
- '// FS redirection checking
- ret = GetFileAttributes(StrPtr(sFolder))
- FolderExists = CBool(ret And vbDirectory) And (ret <> INVALID_FILE_ATTRIBUTES)
- '// FS redirection enambling
- Exit Function
- ErrorHandler:
- ErrorMsg Err, "modFile.FolderExists", "Folder:", sFolder$, "Redirection: ", ForceUnderRedirection
- If inIDE Then Stop: Resume Next
- End Function
- Public Sub GetDriveTypeNames()
- On Error GoTo ErrorHandler
- Dim lr As Long
- Dim i As Long
- Dim DT As String
- For i = 65& To 90&
- lr = GetDriveType(StrPtr(Chr$(i) & ":"))
- Select Case lr
- Case 3&
- DT = "FIXED"
- Case 2&
- DT = "REMOVABLE"
- Case 5&
- DT = "CDROM"
- Case 4&
- DT = "REMOTE"
- Case 0&
- DT = "UNKNOWN"
- Case 1&
- DT = "DISCONNECTED" '"NO_ROOT_DIR"
- Case 6&
- DT = "RAMDISK"
- Case Else
- DT = "UNKNOWN"
- End Select
- DriveTypeName.Add DT, Chr$(i)
- Next
- Exit Sub
- ErrorHandler:
- ErrorMsg Err, "modFile.GetDriveTypeNames", "Drive:", Chr$(i)
- End Sub
- Function FileLenW(Path As String) As Currency ', Optional DoNotUseCache As Boolean
- On Error GoTo ErrorHandler
- Dim lr As Long
- Dim hFile As Long
- Dim FileSize As Currency
- hFile = CreateFile(StrPtr(Path), FILE_READ_ATTRIBUTES, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
- If hFile Then
- lr = GetFileSizeEx(hFile, FileSize)
- If lr Then
- If FileSize < 10000000000@ Then FileLenW = FileSize * 10000&
- End If
- CloseHandle hFile: hFile = 0&
- End If
- Exit Function
- ErrorHandler:
- ErrorMsg Err, "modFile.FileLenW", "File:", Path, "hFile:", hFile, "FileSize:", FileSize, "Return:", lr
- End Function
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д