Results 1 to 10 of 10

Thread: embed and run EXE in VB6 project Picturebox

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    7

    embed and run EXE in VB6 project Picturebox

    Hello,
    I need to embed and run an EXE compiled from a VB6 project into a Picturebox of another VB6 project.

    I try to use the code which works perfectly with Notepad.exe, but with the "VB6" EXE the program is launched NOT in the Picturebox.

    This is the code:
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Private Sub Command1_Click()
    Dim lHwnd As Long
    Call Shell("notepad.exe", vbHide)
    lHwnd = FindWindow("notepad", vbNullString)
    Call SetParent(lHwnd, Picture1.hwnd)
    Call ShowWindow(lHwnd, 3)
    End Sub
    
    Private Sub Command2_Click()
    Dim lHwnd As Long
    Dim sTitle$
    Call Shell("notepad.exe", vbHide)
     
        sTitle = "Senza nome - Blocco note"
        lHwnd = FindWindow(vbNullString, sTitle)
        Call SetParent(lHwnd, Picture1.hwnd)
        Call ShowWindow(lHwnd, 3)
    End Sub
    
    Private Sub Command3_Click()
    Dim lHwnd As Long
    Call Shell("provexe.exe", vbHide)
    lHwnd = FindWindow("provexe", vbNullString)
    Call SetParent(lHwnd, Picture1.hwnd)
    Call ShowWindow(lHwnd, 3)
    
    End Sub
    
    Private Sub Command4_Click()
    Dim lHwnd As Long
    Dim sTitle$
    Call Shell("provexe.exe", vbHide)
     
        sTitle = "Form1"
        lHwnd = FindWindow(vbNullString, sTitle)
        Call SetParent(lHwnd, Picture1.hwnd)
        Call ShowWindow(lHwnd, 3)
    End Sub
    Clicking Command 1 and 2 everything is OK.
    Clicking Command 3 and 4 the EXE is launched outside the Form.

    The attached files show everything.

    How can I solve this problem?

    Many thanks in advance for your help

    Dario
    Attached Files Attached Files
    Last edited by Shaggy Hiker; Oct 5th, 2024 at 02:44 PM.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,336

    Re: embed and run EXE in VB6 project Picturebox

    What possible purpose besides hiding malware does this have?

    My shell browser control uses a technique to store arbitrary binary data in a PictureBox but that's image files, so there's button and menu icons without having to keep a bunch of .ico/.png.

    Using it for an exe sounds sketchy as hell.

  3. #3
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,098

    Re: embed and run EXE in VB6 project Picturebox

    You've misread it, the guy wants to put the Notepad window inside a PictureBox as its parent. This isn't about storing binary data in a bitmap.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: embed and run EXE in VB6 project Picturebox

    Try this:

    Code:
    Option Explicit
    
    Private Const NO_HANDLE As Long = -1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    
    Private Declare Function EnumWindows Lib "User32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "User32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function SetParent Lib "User32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function ShowWindow Lib "User32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Private FoundWindowH As Long
    
    Private Function ScanMainWindows(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim ProcessID As Long
    
       If Not GetWindowThreadProcessId(hwnd, ProcessID) = 0 Then
          If ProcessID = lParam Then
             FoundWindowH = hwnd
             ScanMainWindows = CLng(False)
             Exit Function
          End If
       End If
          
       ScanMainWindows = CLng(True)
    End Function
    
    Public Sub StartProgramInsideWindow(PathV As String, ContainerWindowH As Long)
    Dim ProcessID As Long
    
       ProcessID = Shell(PathV, vbHide)
    
       FoundWindowH = NO_HANDLE
       EnumWindows AddressOf ScanMainWindows, ProcessID
       If Not FoundWindowH = NO_HANDLE Then
          SetParent FoundWindowH, ContainerWindowH
          ShowWindow FoundWindowH, SW_SHOWMAXIMIZED
       End If
    End Sub
    (If it it doesn't work from inside vb6, try compiling it and running the exe.)

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    7

    Re: embed and run EXE in VB6 project Picturebox

    Quote Originally Posted by fafalone View Post
    What possible purpose besides hiding malware does this have?

    My shell browser control uses a technique to store arbitrary binary data in a PictureBox but that's image files, so there's button and menu icons without having to keep a bunch of .ico/.png.

    Using it for an exe sounds sketchy as hell.

    I must explain.
    The examples I attached are very simple, just to show the problem.
    Indeed the project which should contain the EXE is very complex (FFT of incoming audio signals) and the embedded EXE is quite complex itself (sound generation). Merging the codes would be very, very hard. That's why I prefer to embed one into the other.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: embed and run EXE in VB6 project Picturebox

    Could you show us these projects? Someone here might be able to help.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,595

    Re: embed and run EXE in VB6 project Picturebox

    Quote Originally Posted by bardar View Post
    I must explain.
    The examples I attached are very simple, just to show the problem.
    Indeed the project which should contain the EXE is very complex (FFT of incoming audio signals) and the embedded EXE is quite complex itself (sound generation). Merging the codes would be very, very hard. That's why I prefer to embed one into the other.
    To me, that sounds like a perfect reason to learn how to compile VB6 ActiveX DLLs. When I have near-independent programs that need to work together, these DLLs are certainly my "go to".

    And done that way, it'll be MUCH easier to pass data between the two components (i.e., the main EXE and the DLL).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    7

    Re: embed and run EXE in VB6 project Picturebox

    Hello,
    many thanks for your reply. I guess I must call the Sub StartProgramInsideWindow. Then:
    - PathV is the path of the EXE to be embedded? (i.e. for example "e:\program.exe") and and
    - ContainerWindowH is the hwnd of the target PictureBox ?

    I tried this way your routines and I receive an error "Enum windows Invalid use of Address of operator"
    If I try to compile the Exe I get the same error.

    Any suggestion?

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,892

    Re: embed and run EXE in VB6 project Picturebox

    Yes, you are correct. What are you doing when you get the invalid use of AddressOf error message? There's no reason to modify the code I provided. Please post the code causing the error. And remember to use code tags (the "#" button in the toolbar above the message you're about to post).

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2016
    Posts
    7

    Re: embed and run EXE in VB6 project Picturebox

    Hello,
    this is all the code:

    Code:
    Option Explicit
    
    Private Declare Function EnumWindows Lib "User32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "User32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function SetParent Lib "User32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function ShowWindow Lib "User32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Private Const NO_HANDLE As Long = -1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    
    Private FoundWindowH As Long
    Dim pathv As String
    
    Private Function ScanMainWindows(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim ProcessID As Long
    
       If Not GetWindowThreadProcessId(hwnd, ProcessID) = 0 Then
          If ProcessID = lParam Then
             FoundWindowH = hwnd
             ScanMainWindows = CLng(False)
             Exit Function
          End If
       End If
          
       ScanMainWindows = CLng(True)
    End Function
    
    Private Sub StartProgramInsideWindow(pathv As String, ContainerWindowH As Long)
    Dim ProcessID As Long
    
       ProcessID = Shell(pathv, vbHide)
    
       FoundWindowH = NO_HANDLE
       EnumWindows AddressOf ScanMainWindows, ProcessID
       If Not FoundWindowH = NO_HANDLE Then
          SetParent FoundWindowH, ContainerWindowH
          ShowWindow FoundWindowH, SW_SHOWMAXIMIZED
       End If
    End Sub
    
    Private Sub Command1_Click()
    StartProgramInsideWindow pathv, Picture1.hwnd
    Stop
    End Sub
    
    Private Sub Form_Load()
    pathv = "e:\onda.exe"
    
    End Sub
    the code causing the error is:

    Code:
       EnumWindows AddressOf ScanMainWindows, ProcessID
    Last edited by Shaggy Hiker; Oct 5th, 2024 at 02:43 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width