-
Notifications
You must be signed in to change notification settings - Fork 3
/
M_omAPI32FileFunctions.def
159 lines (129 loc) · 5.17 KB
/
M_omAPI32FileFunctions.def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Option Compare Database
Option Explicit
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_READONLY = &H1
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Public Function GetShortFileName(sFileName As String) As String
On Error Resume Next
Dim lpszShortPath As String
Dim cchBuffer As Long
Dim szSize As Long
Dim iFile As Integer
Dim fDelete As Boolean
' This function will only return a value for a file that exists, so
' we must create a dummy file for it to work consistently.
If Dir(sFileName) = "" Then
iFile = FreeFile
Open sFileName For Output As iFile
Print #iFile, "bye"
Close #iFile
fDelete = True
Else
fDelete = False
End If
cchBuffer = 256
lpszShortPath = String$(cchBuffer, Chr(0))
szSize = GetShortPathName(sFileName, lpszShortPath, cchBuffer)
GetShortFileName = Left(lpszShortPath, szSize)
If fDelete = True Then Kill (sFileName)
End Function
Public Function GetShortFolderName(sFolderName As String) As String
On Error Resume Next
Dim lpszShortPath As String
Dim cchBuffer As Long
Dim szSize As Long
Dim iFile As Integer
Dim fDelete As Boolean
Const sFile As String * 8 = "temp.txt"
sFolderName = sFolderName & sFile
iFile = FreeFile
Open sFolderName For Output As iFile
Print #iFile, "bye"
Close #iFile
cchBuffer = 256
lpszShortPath = String$(cchBuffer, Chr(0))
szSize = GetShortPathName(sFolderName, lpszShortPath, cchBuffer)
sFolderName = Left(lpszShortPath, szSize)
GetShortFolderName = Left(sFolderName, Len(sFolderName) - 8)
Kill (sFolderName)
End Function
Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
On Error GoTo Err_Handler
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.description, vbExclamation, "E R R O R"
Resume Exit_Here
End Sub
Public Function GetUserName() As Variant
On Error Resume Next
Dim sUserName As String
Dim lngLength As Long
Dim lngResult As Long
sUserName = String$(255, 0)
lngLength = 255
lngResult = apiGetUserName(sUserName, lngLength)
GetUserName = Left(sUserName, InStr(1, sUserName, Chr(0)) - 1)
End Function