Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodWinAPI.bas
More file actions
376 lines (316 loc) · 12.9 KB
/
modWinAPI.bas
File metadata and controls
376 lines (316 loc) · 12.9 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
Attribute VB_Name = "modWinAPI"
Attribute VB_Description = "Gebräuchliche WinAPI-Funktionen"
'---------------------------------------------------------------------------------------
' Module: modWinAPI
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Gebräuchliche WinAPI-Funktionen
' </summary>
' <remarks>
' Sammlung von API-Deklarationen, die oft benötigt werden
' </remarks>
'\ingroup WinAPI
'**/
'---------------------------------------------------------------------------------------
'<codelib>
' <file>api/winapi/modWinAPI.bas</file>
' <license>_codelib/license.bas</license>
'</codelib>
'---------------------------------------------------------------------------------------
'
Option Compare Text
Option Explicit
Private Const WM_MSG_SETICON As Long = &H80
Private Const WM_PARAM_ICON_SMALL As Long = 0
Private Const IMAGE_ICON As Long = 1
Private Const LR_LOADFROMFILE As Long = &H10
Private Const SE_ERR_NOTFOUND As Long = 2
Private Const SE_ERR_NOASSOC As Long = 31
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const NORMAL_PRIORITY_CLASS As Long = &H20
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
Private Const INFINITE As Long = &HFFFFFFFF ' = -1&
Private Const WAIT_TIMEOUT As Long = &H102&
#If VBA7 Then
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare PtrSafe Function LoadImage Lib "user32" Alias "LoadImageA" ( _
ByVal hInst As Long, _
ByVal lpszName As String, _
ByVal uType As Long, _
ByVal cxDesired As Long, _
ByVal cyDesired As Long, _
ByVal fuLoad As Long) As Long
Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
ByVal Hwnd As Long, _
ByVal lOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
Private Declare PtrSafe Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" ( _
ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Declare PtrSafe Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ( _
ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
lpEnvironment As Any, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare PtrSafe Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" ( _
ByVal hInst As Long, _
ByVal lpszName As String, _
ByVal uType As Long, _
ByVal cxDesired As Long, _
ByVal cyDesired As Long, _
ByVal fuLoad As Long) As Long
Private Declare Function ShellExecuteA Lib "shell32.dll" ( _
ByVal Hwnd As Long, _
ByVal lOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" ( _
ByVal lpBuffer As String, _
ByVal nSize As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ( _
ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
lpEnvironment As Any, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
'---------------------------------------------------------------------------------------
' Kapselungen
'---------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------
' Function: ShellExecuteOpenFile (Josef Pötzl, 2010-04-19)
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Datei mit ShellExecute öffnen
' </summary>
' <param name="sFile">vollständiger Dateiname inkl. Verzeichnis</param>
' <param name="sAPIOperation">"open", "print", ...</param>
' <returns>Boolean</returns>
' <remarks>
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Function ShellExecuteOpenFile(ByVal sFile As String, _
Optional ByVal sAPIOperation As String = vbNullString) As Boolean
Dim lRet As Long
Dim sDirectory As String
Dim lngDeskWin As Long
If sFile = vbNullString Then
ShellExecuteOpenFile = False
Exit Function
Else
lngDeskWin = GetDesktopWindow()
lRet = ShellExecuteA(lngDeskWin, sAPIOperation, sFile, vbNullString, vbNullString, vbNormalFocus)
End If
If lRet = SE_ERR_NOTFOUND Then
'Datei nicht gefunden
MsgBox "Datei nicht gefunden" & vbNewLine & vbNewLine & _
sFile
ShellExecuteOpenFile = False
Exit Function
ElseIf lRet = SE_ERR_NOASSOC Then
'Wenn die Dateierweiterung noch nicht bekannt ist...
'wird der "Öffnen mit..."-Dialog angezeigt.
sDirectory = Space$(260)
lRet = GetSystemDirectory(sDirectory, Len(sDirectory))
sDirectory = Left$(sDirectory, lRet)
Call ShellExecuteA(lngDeskWin, vbNullString, "RUNDLL32.EXE", "shell32.dll, OpenAs_RunDLL " & _
sFile, sDirectory, vbNormalFocus)
End If
ShellExecuteOpenFile = True
End Function
'---------------------------------------------------------------------------------------
' Function: ShellExecuteSendMail
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Email mit Standard-Programm versenden
' </summary>
' <param name="sTo">Empfänger-Adresse</param>
' <param name="sSubject">Betreff-Zeile</param>
' <param name="sBody">Email-Text</param>
' <returns>Boolean</returns>
' <remarks>
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Function ShellExecuteSendMail(ByVal sTo As String, _
ByVal sSubject As String, _
ByVal sBody As String) As Boolean
Dim lRet As Long
Dim strLpFile As String
If Len(sTo) = 0 Then
ShellExecuteSendMail = False
Exit Function
End If
If sSubject > vbNullString Then
strLpFile = "subject=" & sSubject
End If
If sBody > vbNullString Then
If strLpFile > vbNullString Then
strLpFile = strLpFile & "&body=" & sBody
Else
strLpFile = "body=" & sBody
End If
End If
If strLpFile > vbNullString Then
strLpFile = "mailto:" & sTo & "?" & strLpFile
Else
strLpFile = "mailto:" & sTo
End If
lRet = ShellExecuteA(GetDesktopWindow(), "open", strLpFile, vbNullString, vbNullString, vbNormalFocus)
ShellExecuteSendMail = (lRet <> 0)
End Function
'---------------------------------------------------------------------------------------
' Function: LaunchAppSynchronous (Josef Pötzl, 2010-04-19)
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Anwnedung Synchron ausführen
' </summary>
' <param name="strExecutablePathAndName">Ausführbare Datei</param>
' <param name="sParam">Startparameter der Anwendung</param>
' <param name="lShowCommand">Fenstermodus</param>
' <returns>Boolean</returns>
' <remarks>
' Code hält so lange an, bis die gestartete Anwendung beendet wurde.
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Function LaunchAppSynchronous(ByVal strExecutablePathAndName As String, _
Optional ByVal sParam As String = vbNullString, _
Optional ByVal lShowCommand As Long = vbNormalFocus) As Boolean
'http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=3716&lngWId=1
Dim lngResponse As Long
Dim typStartUpInfo As STARTUPINFO
Dim typProcessInfo As PROCESS_INFORMATION
LaunchAppSynchronous = False
With typStartUpInfo
.cb = Len(typStartUpInfo)
.lpReserved = vbNullString
.lpDesktop = vbNullString
.lpTitle = vbNullString
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = lShowCommand
End With
'Launch the application by creating a ne
' w process
lngResponse = CreateProcess(vbNullString, strExecutablePathAndName & " " & sParam, 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, typStartUpInfo, typProcessInfo)
If lngResponse Then
'Wait for the application to terminate b
' efore moving on
Call WaitForTermination(typProcessInfo)
LaunchAppSynchronous = True
Else
LaunchAppSynchronous = False
End If
End Function
Private Sub WaitForTermination(ByRef typProcessInfo As PROCESS_INFORMATION)
'This wait routine allows other applicat
' ion events
'to be processed while waiting for the p
' rocess to
'complete.
Dim lngResponse As Long
'Let the process initialize
Call WaitForInputIdle(typProcessInfo.hProcess, INFINITE)
'We don't need the thread handle so get
' rid of it
Call CloseHandle(typProcessInfo.hThread)
'Wait for the application to end
Do
lngResponse = WaitForSingleObject(typProcessInfo.hProcess, 0)
If lngResponse <> WAIT_TIMEOUT Then
'No timeout, app is terminated
Exit Do
End If
DoEvents
Loop While True
'Kill the last handle of the process
Call CloseHandle(typProcessInfo.hProcess)
End Sub
'---------------------------------------------------------------------------------------
' Sub: SetFormIconFromFile (Josef Pötzl, 2009-12-19)
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Spezielles Icon für ein Formular einstellen
' </summary>
' <param name="FormRef">Referenz zum Access.Form</param>
' <param name="IconFilePath">Icon-Datei (vollständige Pfadangabe)</param>
' <remarks>
'
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Sub SetFormIconFromFile(ByRef FormRef As Access.Form, ByVal IconFilePath As String)
On Error Resume Next ' ... Fehlermeldung würde bei dieser "unwichtigen" Funktion nur stören
Const ICONPIXELSIZE As Long = 16
Dim imageHandle As Long
imageHandle = LoadImage(0, IconFilePath, IMAGE_ICON, _
ICONPIXELSIZE, ICONPIXELSIZE, LR_LOADFROMFILE)
If imageHandle <> 0 Then
SendMessage FormRef.Hwnd, WM_MSG_SETICON, WM_PARAM_ICON_SMALL, ByVal imageHandle
End If
End Sub
You can’t perform that action at this time.
