simple registry/shell question

vb6 Kuwait
  • 18 years ago

    I would do this:
    private sub form_load()
    dim pth as string,dt as string
      pth=getsetting    'etc.  to retrieve the path with the default set to ""
      if len(pth)<1 then  savesetting    'etc.  save path
      dt=getsetting 'etc. (date) with the default set to "1/1/1980"
      if datediff("d",dt,date)=0 then
        unload me
        exit sub
      end if
      savesetting   'etc.  date
    end sub




    Do you want to kill the program without saving it's info?
    Do you want the program to run inside your program?


    I got these from the API Guide


    Play with these 2 examples


    ' *******
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Const GW_HWNDNEXT = 2
    Dim mWnd As Long
    Function InstanceToWnd(ByVal target_pid As Long) As Long
       Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
       'Find the first window
       test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
       Do While test_hwnd <> 0
           'Check if the window isn't a child
           If GetParent(test_hwnd) = 0 Then
               'Get the window's thread
               test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
               If test_pid = target_pid Then
                   InstanceToWnd = test_hwnd
                   Exit Do
               End If
           End If
           'retrieve the next window
           test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
       Loop
    End Function
    Private Sub Form_Load()
       'KPD-Team 1999
       'URL: http://www.allapi.net/
       'E-Mail: [email protected]
       Dim Pid As Long
       'Lock the window update
       LockWindowUpdate GetDesktopWindow
       'Execute notepad.Exe
       Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
       If Pid = 0 Then MsgBox "Error starting the app"
       'retrieve the handle of the window
       mWnd = InstanceToWnd(Pid)
       'Set the notepad's parent
       SetParent mWnd, Me.hwnd
       'Put the focus on notepad
       Putfocus mWnd
       'Unlock windowupdate
       LockWindowUpdate False
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
       'Unload notepad
       DestroyWindow mWnd
       'End this program
       TerminateProcess GetCurrentProcess, 0
    End Sub
    '
    ***********



    I modified this one a little when it loads it starts notepad then you click the button(command1) and notepad closes or asks you to save before closing.
    '***********
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
       'KPD-Team 1999
       'URL: http://www.allapi.net/
       'E-Mail: [email protected]
    Dim lHandle As Long, lHnd As Long
    Const GW_HWNDNEXT = 2


    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long


    Private Sub Command1_Click()
     CloseProgram
    End Sub


       'First we're going to retrieve the handle of this window
    Private Sub Form_Load()


      lHandle = Shell("C:\windows\notepad.exe", vbNormalFocus)
     'now use the :
         lHandle = InstanceToWnd(lHandle)  'from the example above
    End Sub
    Private Sub CloseProgram()
       'Set this window to the foreground
       lHnd = SetForegroundWindow(lHandle)
       DoEvents
       SendKeys "%{F4}", True
    End Sub
    Function InstanceToWnd(ByVal targetpid As Long) As Long
       Dim test
    hwnd As Long, testpid As Long, testthreadid As Long
       'Find the first window
       test
    hwnd = FindWindow(ByVal 0&, ByVal 0&)
       Do While testhwnd <> 0
           'Check if the window isn't a child
           If GetParent(test
    hwnd) = 0 Then
               'Get the window's thread
               testthreadid = GetWindowThreadProcessId(testhwnd, testpid)
               If testpid = targetpid Then
                   InstanceToWnd = testhwnd
                   Exit Do
               End If
           End If
           'retrieve the next window
           test
    hwnd = GetWindow(testhwnd, GWHWNDNEXT)
       Loop
    End Function




    '***********



    you can find cool info at AllAPI.net  (API Guide)

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks