Results 1 to 21 of 21

Thread: OLE Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    OLE Question

    Hi all,
    I have read several tutorials about an "OLE Container," but I've looked and looked and I can find no such thing in my Visual Basic (Part of Visual Studio 2010)

    Can anyone help a noob?

    Thanks in advance.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    It does not exists in VS2010 see the following http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    :O Why?

    I need to embed a .exe file into a form.... any other way of doing this?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    :O Why?

    I need to embed a .exe file into a form.... any other way of doing this?
    What are you trying to do as an end result? Embedding something is only part of a solution.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Quote Originally Posted by kevininstructor View Post
    What are you trying to do as an end result? Embedding something is only part of a solution.
    I'm trying to "embed" an exe into my project so that by pushing a button it appears in a form... Sorry if I don't explain myself well...

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    I'm trying to "embed" an exe into my project so that by pushing a button it appears in a form... Sorry if I don't explain myself well...
    What is the purpose of this executable?
    Did you write it or someone else?

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Quote Originally Posted by kevininstructor View Post
    What is the purpose of this executable?
    Did you write it or someone else?
    Someone else wrote it... I'm trying to make like a swiss army knife sort of application...

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    Someone else wrote it... I'm trying to make like a swiss army knife sort of application...
    Your best option is to include the executable in your installation, place it in a known folder and then call it using Process.Start which does not start it in your form but by itself.

    Now lets say you wanted to embedded something that could be shown in a web browser you could easily do that but not an executable file.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Quote Originally Posted by kevininstructor View Post
    Your best option is to include the executable in your installation, place it in a known folder and then call it using Process.Start which does not start it in your form but by itself.

    Now lets say you wanted to embedded something that could be shown in a web browser you could easily do that but not an executable file.
    The thing is that the point of the app is that it is portable... How would I go about doing what you just said? Thanks for your help.

  10. #10
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: OLE Question

    Look up setparent API

    Kris

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: OLE Question

    as i00 said, the setParent API will allow you to embed an executable in a container (form, panel, or something)

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Here is a working example of embedding an external app into a form
    http://www.vbforums.com/showpost.php...24&postcount=2

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Thanks for your help guys. I will post back with results.

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Neither of those methods worked for me o_0.. I am lost :O

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    Neither of those methods worked for me o_0.. I am lost :O
    When you say neither method works nobody can help if you do not explain in detail what happened.

  16. #16
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Here is an example which requires ColorCop to be in the same folder as the code executing below.

    I have been using Color Cop for years now which is why I used it for the demo code below which works fine as with the code below.


    One form, one button
    Code:
    Public Class Form1
        Dim notepad_hWnd As IntPtr = IntPtr.Zero
        Public Const GWL_STYLE As Integer = (-16)
        Public Const WS_BORDER As Integer = &H800000
        Public Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As IntPtr) As Integer
        Public Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        Public Declare Function MoveWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
        Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
        Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    
        Private ProgramToEmbed As String = IO.Path.Combine(Application.StartupPath, "ColorCop.exe")
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim MyPSI As New ProcessStartInfo(ProgramToEmbed)
            Try
                Dim MyProcess As Process = Process.Start(MyPSI)
                MyProcess.WaitForInputIdle()
                notepad_hWnd = MyProcess.MainWindowHandle
    
                If notepad_hWnd <> IntPtr.Zero Then
                    Me.Text = MyProcess.MainWindowTitle
                    ' set parent
                    SetParent(notepad_hWnd, Me.Handle)
                    ' remove notepad border
                    SetWindowLong(notepad_hWnd, GWL_STYLE, GetWindowLong(notepad_hWnd, GWL_STYLE) + WS_BORDER)
                    ' size notepad to forms client size
                    MoveWindow(notepad_hWnd, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height, True)
                End If
            Catch ex As Exception
                MessageBox.Show("Unable to launch program", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Button1.Enabled = IO.File.Exists(ProgramToEmbed)
        End Sub
    End Class

  17. #17

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Got it. Sorry for not replying in detail. In the end I had to change the name of application.exe in the project's files, for some reason, VB was saving it as application.exe.deploy
    Your previous code worked like a charm, kevininstructor.

    Now I'm trying to figure out how to use it in a panel.

  18. #18
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    Got it. Sorry for not replying in detail. In the end I had to change the name of application.exe in the project's files, for some reason, VB was saving it as application.exe.deploy
    Your previous code worked like a charm, kevininstructor.

    Now I'm trying to figure out how to use it in a panel.
    Change
    Code:
    SetParent(notepad_hWnd, Me.Handle)
    To (change the name of the panel to the name of your panel)

    Code:
    SetParent(notepad_hWnd, Panel1.Handle)

  19. #19

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: OLE Question

    Quote Originally Posted by kevininstructor View Post
    Change
    Code:
    SetParent(notepad_hWnd, Me.Handle)
    To (change the name of the panel to the name of your panel)

    Code:
    SetParent(notepad_hWnd, Panel1.Handle)
    Man you are awesome!

    EDIT: I got it!!! Made a new form and presto... weird fix o.0

    Thank you so much kevininstructor and everyone else!!
    Last edited by SgtJulian; Oct 5th, 2011 at 12:38 PM.

  20. #20
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: OLE Question

    Quote Originally Posted by SgtJulian View Post
    Man you are awesome!

    EDIT: I got it!!! Made a new form and presto... weird fix o.0

    Thank you so much kevininstructor and everyone else!!
    The real thanks should go to EdgeMeal for his solution here http://www.vbforums.com/showpost.php...24&postcount=2

    Any ways glad you have a solution now

  21. #21
    Lively Member
    Join Date
    Oct 2010
    Posts
    83

    Re: OLE Question

    Hi, Kevin,
    I've tried your code with your exe and it works well.
    Code:
      Private ProgramToEmbed As String = IO.Path.Combine(Application.StartupPath, "MENU.EXE")
        Dim notepad_hWnd As IntPtr = IntPtr.Zero
        Private Const GWL_STYLE As Integer = (-16)
        Private Const WS_BORDER As Integer = &H800000
        Private Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As IntPtr) As Integer
        Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        Private Declare Function MoveWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
        Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
        Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim MyPSI As New ProcessStartInfo(ProgramToEmbed)
            Try
                Dim MyProcess As Process = Process.Start(MyPSI)
                MyProcess.WaitForInputIdle()
                notepad_hWnd = MyProcess.MainWindowHandle
                If notepad_hWnd <> IntPtr.Zero Then
                    Me.Text = MyProcess.MainWindowTitle
                    ' set parent
                    SetParent(notepad_hWnd, Me.Handle)
                    ' remove notepad border
                    SetWindowLong(notepad_hWnd, GWL_STYLE, GetWindowLong(notepad_hWnd, GWL_STYLE) + WS_BORDER)
                    ' size notepad to forms client size
                    MoveWindow(notepad_hWnd, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height, True)
                End If
            Catch ex As Exception
                MessageBox.Show("Unable to launch program", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Button1.Enabled = IO.File.Exists(ProgramToEmbed)
        End Sub
    I have a folder with several someone else executables for simple calculations (total size is only 1,5 MB). I am not sure but it looks like a console application.
    Please look at attached picture. I have a two possibilities to start program:
    - click on MENU.EXE , then select one from the list of 19 subprograms or
    - click on one of other 19 executables (C2 - C38).
    I don't know what is BASRUN.EXE and other two files in picture.
    All these files I have copied to bin - debug folder of my project and I try your code with name of exe: MENU.EXE but I received an message: Unable to launch program.
    I do not know if there is a chance to get this resolved. It appears to me that these executables somehow interconnected with each other.
    Does anyone know the solution?
    Onenew

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