Abrir solo dos instancias de mi programa

Iniciado por Gorky, 26 Marzo 2006, 15:02 PM

0 Miembros y 1 Visitante están viendo este tema.

LeandroA

te paso el code, fijate que para que funcione primero tenes que generar el exe


Option Explicit
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Private Sub Form_Load()
Comprovar
End Sub

Sub Comprovar()
Dim hSnapShot As Long, uProcess As PROCESSENTRY32, r As Long, Contador As Integer
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess)
Dim Proceso As String
Do While r
Proceso = Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
If UCase(App.EXEName & ".exe") = UCase(Proceso) Then
Contador = Contador + 1
If Contador > 2 Then End
End If
r = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
End Sub

Ironhorse

Otra opcion es pasarle como parametro al ejecutable la cantidad de instancias que se van llamando:


Private Instancias As Long

Private Sub Form_Load()
  Instancias = Val(Command)
  Instancias = Instancias + 1
  If Instancias < 2 Then
   Shell (App.Path & "\" & App.EXEName & " " & Instancias)
  End If
End Sub



Saludos
Pégense una vuelta por Cracton :)