-
I'm sorry that I don't have more time to work on this but you may be able to use a Win32 API function known as CreateProcess. Here is some code
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 NORMAL_PRIORITY_CLASS = &H20&
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
So after you add this code you should have what you need to call the CreateProcess function. You'll notice that the second parameter is called lpCommandLine. This is a pointer to null terminated String. This is probably the parameter that you are most interested in. It should work if you pass it a variable like:
Dim command as String
command = "mkdir c:\directory" & vbNullChar
but I haven't tested it. It is acceptable for a bare bones version of this function to pass NULL values for many of the parameters. A call to this function could look like this:
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim command as String
command = "mkdir c:\directory" & vbNullChar
ret& = CreateProcessA(0&, commmand, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
I hope this works although whenever I use win32 api functions it usually requires a little tinkering. For more info on the CreateProcess() function (And many more for that matter) GoTo http://www.mentalis.org/apilist/CreateProcess.shtml
-
-
What you would have to do if you want to use the VB functions (and I have already said this) is to have the strcommand formatted as such:
strcommand = "command argument1, arg2, arg3" etc.
Then split the strcommand string into an array (using the first space as the delimiter), and run the function with the same name as the first element in the array.
E,g. if strcommand = "kill C:\autoexec.bat" then you'd have an array with two elements: array(0) = "kill", array(1) = "C:\autoexec.bat".
then you'd run a function with the same name as the value of array(0) with the arguments in array(1) (which would be a space-delimited list of all the original arguments).
I'm not actually sure if it's possible to do this in VB, but you could give it a try and see if it works.
-
Pwaring,
How are you running the code from the first part of the array then ?
Your making this more complicated than it has to be. You have added an array now.
So what you going to do ? build the array back into a string to execute ?
How do you execute it ?
-
What I mean is, you'd get the first part of the strcommand (i.e. the actual command you want to run, minus any arguments) and execute a function/procedure with the same name (e.g. Kill()). In calling that function, you'd pass the arguments that are the remaining part of the strcommand string.
So if strcommand = "kill C:\autoexec.bat" you'd be calling kill("C:\autoexec.bat") (array(0)(array(1)), but I don't know whether you can interpolate a variable and use its value to call a function of the same name in VB.
Explaining what I'm getting at is difficult, it'd be something like this:
strcommand = "kill C:\autoexec.bat"
command_array = split(strcommand, " ", 1)
call command_array(0)(command_array(1))
The problem is, I don't know how/whether you can use the command_array(0) variable to act as a function (e.g. if command_array(0) was "mkdir", it would call the mkdir() function).
If it works, it's a darn sight simpler than VicC's solution (not that I'm criticising his solution, I'm just saying that it might be a little OTT for what you want to do).
-
I just noticed a typo with the constant NORMAL_PRIORITY_CLASS it should read
Public Const NORMAL_PRIORITY_CLASS = &H20
-
Hi Mark!
To point out the problem you're trying to solve: strings are not being compiled!
knowing that any internal command included in that string also won't be compiled to machine code. so that the command cannot be executed even if you would manage to split the
string-information up and pass is to arrays.
the only way i can think of to do this is by incorporating a script interface or to make your own script interpreter. i recently came across such code: you even can insert whole functions into the textbox or string array and then execute them.
i didn't take a too close look on the code, so right now i don't know wether an interface to VB scriptig was used or if it was pure VB. I'll check it and post the information.
Allright i checked the code ... vb script was incorporated and i think you don't wanted that. PSC is full of such applications. but if you manage to split up the string array you want to use in command and parameter you could use a lookup-table which would assign a mkdir-whatever function to that command and pass the parameter to it.
depends on how many commands you want to utilize and need to cover by that lookup-table/assign function to command expression thingy.
because of the fact i mentioned before i think there's no way to do it with pure in-string information - but vb script or pre-defined lookup-table.
hope it helped sort thing out.
-
Heres a quicky i whipped up.
strCommand = txtCommand.text
Select Case LCase(mid(strCommand, 1, instr(1, strCommand, " ") - 1))
Case "kill"
kill Mid(strCommand, instr(1, strCommand, " ") + 1, Len(strCommand))
Case "mkdir"... etc.
Havnt tested it, but i dont see why it wouldnt work.
-
Cheers for all the answers guys but What I wanted to do can't actually be done.
Doesn't matter. I'm sure I got you all thinking anyway.
-
I know this is old. But I would have used the WSH object.