Code for VB needed

Joined
Apr 8, 2005
Messages
11,424
Reaction score
155
Location
٩(̾●̮̮̃̾•̃̾)۶ __̴ı̴̴̡&
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
 
Will the program you'll be opening be the same program all the time or will it vary?
 
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

Code:
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
Code:
Me.Hide
WaitForProcess Shell("notepad", vbNormalFocus)
Me.Show
Me.WindowState = vbNormal

or to open ur specific folder

Code:
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\CurrentVersion\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....
 
raZer said:
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

erMaC said:
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:
 
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)

Code:
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

Code:
openTray 0

To Close

Code:
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
Code:
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...
 
Top
Sign up to the MyBroadband newsletter
X