View Full Version : Code for VB needed
Telkomisaloser
02-06-2006, 06:58 PM
I need a few pieces of code for Visual Basic
1. A code that will open another program. Place the opening program first and then minimize the form you used to open it and once you close the program you opened via the form it must spring back up
2. I need a code that will open a folder also also minimize the form
I'm only going to use the code for non-profit reasons :) like storing stuff on a cd
any help will be apprieciated
freeek
02-06-2006, 07:06 PM
or running a virus
Telkomisaloser
02-06-2006, 07:08 PM
:mad: what makes you think I'm doing a virus :confused: :confused: :confused:
I kinda stay outta that side
raZer
04-06-2006, 08:09 AM
Will the program you'll be opening be the same program all the time or will it vary?
erMaC
04-06-2006, 09:56 AM
U didn't specify which version of vb u got. I've only got vb6, so i hope this helps.
Put this in a seperate module
Option Explicit
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function WaitForProcess(taskID As Long, Optional msecs As Long = -1) As Boolean
Dim procHandle As Long
'get Handle
procHandle = OpenProcess(&H100000, True, taskID)
DoEvents
WaitForProcess = WaitForSingleObject(procHandle, msecs) <> -1
'kill handle
CloseHandle procHandle
End Function
U can call the function like so
Me.Hide
WaitForProcess Shell("notepad", vbNormalFocus)
Me.Show
Me.WindowState = vbNormal
or to open ur specific folder
Me.Hide
WaitForProcess Shell("explorer c:\", vbNormalFocus)
Me.Show
Me.WindowState = vbNormal
Unfortunately because explorer runs as a thread along with the taskbar and desktop in a single process the above doesn't work correctly, u'll find the form pops back up right away.
A quick work around is to change the behaviour of Windows, by making each instance of explorer run as a seperate process.
Just go to Go to HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer
in the registry
Add a new DWORD called DesktopProcess with a value of 1
Restart your machine and presto!
Hopefully that'll help u, if ur using VB.NET then oh well....
Telkomisaloser
04-06-2006, 06:11 PM
Will the program you'll be opening be the same program all the time or will it vary?
The program I'm creating uses buttons to open a folder so therefore each button = same program assigned to each button
U didn't specify which version of vb u got. I've only got vb6, so i hope this helps.
Hopefully that'll help u, if ur using VB.NET then oh well....
Nope I'm using VB 6 and thanks for the code :) it helped me alot
Theres just one more question, I'm planning to run the program from a CD so how can I create a button that will open up the cd to me :confused:
erMaC
04-06-2006, 09:03 PM
Glad I could help, at least their is some use for Matric Computers :D
Ok, to open the CD Tray
Throw the following in another module(I love putting my code in modules :p)
Public Declare Function mciSendString Lib _
"winmm.dll" Alias "mciSendStringA" (ByVal _
lpstrCommand As String, ByVal _
lpstrReturnString As String, ByVal _
uReturnLength As Long, ByVal hWndCallback As _
Long) As Long
Public Sub openTray(drive As Integer)
mciSendString "open " & drive & "type cdaudio alias cdaudio", vbNullString, 0, 0
mciSendString "set cdaudio door open", vbNullString, 0, 0
mciSendString "close cdaudio", vbNullString, 0, 0
End Sub
Public Sub closeTray(drive As Integer)
mciSendString "open " & drive & "type cdaudio alias cdaudio", vbNullString, 0, 0
mciSendString "set cdaudio door Closed", vbNullString, 0, 0
mciSendString "close cdaudio", vbNullString, 0, 0
End Sub
That ought to do it.
You can call it as follows
To Open
openTray 0
To Close
closeTray 0
U can replace the number with n, where n represents the nth CD-Rom alphabetically on ur PC(starting at 0), with which ever drive you want.
U could get all the optical devices on ur PC with the following API Call
Private Declare Function GetDriveType Lib _
"kernel32" Alias "GetDriveTypeA" (ByVal _
nDrive As String) As Long
where optical devices are type 5, but I'll leave that to u.
Happy Coding...
Telkomisaloser
04-06-2006, 10:10 PM
woops sorry I knew that code, I meant to say open the cd folder on my computer
But thanks again for the code
erMaC
04-06-2006, 10:35 PM
Shell "Explorer " + App.Path, vbNormalFocus
Telkomisaloser
04-06-2006, 10:46 PM
thanks again :)
erMaC
04-06-2006, 10:47 PM
What exactly are you making?
Telkomisaloser
04-06-2006, 10:51 PM
I'm making a startup cd program just so then I can get quick access on a cd via buttons and forms instead of going all the way though my computer and then searching for it on the cd
Telkomisaloser
04-06-2006, 10:53 PM
I'm also doing it to gain experence
erMaC
04-06-2006, 11:01 PM
kk, well have fun