I'm trying to run a batch file (xyz.bat) using Admin / Elevated
permissions in Vista, using the "runas" verb via ShellExecuteEx.
This code is crashing -- I'm probably making a dumb mistake. Can
anyone spot the bug?
ShellExecuteExW32 (runAs as boolean, cmd as string, directory as
string) as string
' if RunAs is true, then we use the "runas" verb under Vista
// see http://blogs.msdn.com/vistacompatteam/archive/
2006/09/25/771232.aspx
// http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/shellcc/platform/shell/reference/functions/shellexecuteex.asp
#if TargetWin32
Declare Function ShellExecuteEx Lib "shell32.dll" Alias
"ShellExecuteA" (lpExecInfo as Ptr) as integer
dim iError as integer
dim sError as string
'Private Type SHELLEXECUTEINFO
'cbSize As Long
'fMask As Long
'hWnd As Long
'lpVerb As String
'lpFile As String
'lpParameters As String
'lpDirectory As String
'nShow As Long
'hInstApp As Long
'lpIDList As Long
'lpClass As String
'hkeyClass As Long
'dwHotKey As Long
'hIcon As Long
'hProcess As Long
'End Type
const SIZEOF_SHELLEXECUTEINFO = 60
' length in bytes
const SW_MAXIMIZE = 3
const SW_SHOWNORMAL = 1
const SEE_MASK_NO_CONSOLE = 32768
dim verb as string
dim mb, mbVerb, mbFile, mbDirectory as memoryBlock
WriteLog "ShellExecuteEx runAs= " + BooleanToString(runas) + "
cmd= " + cmd + " directory=" + directory
mb = newMemoryBlock(SIZEOF_SHELLEXECUTEINFO)
if runAs then
verb = "runas"
else
verb = "open"
end if
mbVerb = newMemoryBlock(lenb(verb) +1)
mbVerb.cstring(0) = verb
mbFile = newMemoryBlock(lenb(cmd) + 1)
mbFIle.cstring(0) = cmd
mbDirectory = newMemoryblock(lenb(directory)+1)
mbDirectory.cstring(0) = directory
mb.long(0) = SIZEOF_SHELLEXECUTEINFO ' cbSize
mb.long(4) = 0 ' SEE_MASK_NO_CONSOLE ' fmask
mb.long(8) = 0 ' hwnd
mb.ptr(12) = mbVerb
mb.ptr(16) = mbFile
mb.long(20) = 0 ' parameters
mb.ptr(24) = mbDirectory
mb.long(28) = SW_MAXIMIZE ' nShow
mb.long(32) = 0 ' hInstApp
' rest of parameters are optional
iError = ShellExecuteEx(mb)
If iError <= 32 Then
Select Case iError
Case 2
sError = "The specified file was not found."
Case 31
sError = "There is no application associated with the given
file name extension."
else
sError = "Unknown error " + str(iError)
End Select
End If
WriteLog "ShellExecuteEx result = '" + sError + "'"
return sError
#endif
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>