Results 1 to 6 of 6

Thread: [RESOLVED] How to unload WMP in custom ActiveX Control?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Resolved [RESOLVED] How to unload WMP in custom ActiveX Control?

    I'm using VB5.

    I've tried some methods I've seen on here and other places for unloading the Windows Media Player ActiveX control, (specifically the newer one, "wmp.dll"), but those methods seem to only work if I have it embedded in a Standard EXE project's form. My project is an ActiveX Control, (specifically an OCX), and unloading it from my control produces a runtime error and a crash.

    What is the proper way to remove/unload Windows Media Player, (for video playback specifically), in an ActiveX OCX Control?

    I've tried stopping the video and making the URL property reference a vbNullString, and after that I tried both:
    Code:
    Unload WindowsMediaPlayer
    Set WindowsMediaPlayer = Nothing
    And just this by itself:
    Code:
    Unload WindowsMediaPlayer
    Both produce the same runtime error and crash.
    Any help or suggestions are greatly appreciated.

    (Why has no one replied to me here on any of my topics yet?)

    Thanks!
    Samuel
    Last edited by Samuel Venable; Feb 11th, 2018 at 12:14 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: How to unload WMP in custom ActiveX Control?

    Quote Originally Posted by Samuel Venable View Post
    I'm using VB5.

    I've tried some methods I've seen on here and other places for unloading the Windows Media Player ActiveX control
    Where?

    You only can Unload a form or a control that is in a controls array.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Re: How to unload WMP in custom ActiveX Control?

    What do you mean by "a controls array"? I know generally what an array is, but not in this context.

    Here's an example:
    http://www.vbforums.com/showthread.p...w-media-player

    Code:
    Private Sub WindowsMediaPlayer1_OpenStateChange(ByVal NewState As Long)
    If NewState = 8 'its stopped
    Unload Me
    End If
    End Sub
    I've also tried "Unload Me" like suggested in that topic but it does the same thing as stated before.

    In case it helps anyone this is the error I'm getting:
    Attached Images Attached Images  
    Last edited by Samuel Venable; Feb 10th, 2018 at 09:17 PM.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,671

    Re: How to unload WMP in custom ActiveX Control?

    Info about controls arrays here.

    But in your case, it is not a control array.
    In the code you pointed, the Unload Me unloads the form.

    If you are in an UserControl, you cannot unload the UserControl.

    I think that you need to read a book or at least a tutorial about Visual Basic 6.

    Anyway, try with:

    Code:
    Unload Parent
    It will close the form.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    16

    Re: How to unload WMP in custom ActiveX Control?

    Well it's not in a VB form, and yes the WMP control is in a UserControl. The ActiveX control is made in VB5, but the actual executable I want to embed it in is not a VB5 application, (it's not VB at all for that matter).

    If no one else knows of a quick solution for me I guess I will have a look at that user guide CD that comes with VB5.

    Thank you!

    Edit: I found the solution.

    I need this API function and Windows Message Constant:
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Const WM_CLOSE = &H10
    And do this when right before the UserControl reaches its Terminate event:
    Code:
    WindowsMediaPlayer.Controls.stop
    WindowsMediaPlayer.URL = vbNullString
    WindowsMediaPlayer.Close
    Call SendMessage(UserControl.hWnd, WM_CLOSE, 0&, 0&)
    Last edited by Samuel Venable; Feb 11th, 2018 at 12:15 AM.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: [RESOLVED] How to unload WMP in custom ActiveX Control?

    Quote Originally Posted by Samuel Venable View Post
    Edit: I found the solution.
    I don't think that's a "clean" way of unloading an ActiveX control. An ActiveX control is composed of not just a child window but also COM code that exposes properties, methods and events relevant to the child window being wrapped. Simply closing the child window in that manner1 is most likely not going to destroy the COM portion of the ActiveX control as well. So, it may look like the ActiveX control is gone, but it is actually still loaded in memory.

    Properly unloading the ActiveX control depends on how it was created. If it was placed on a VB5/6 Form/UserControl at design time, you won't be able to remove it until the Form/UserControl is unloaded. If it was loaded dynamically (either via the aforementioned Control Arrays or via the Controls collection), then it can be unloaded dynamically as well. If you're using a different ActiveX host container, it will depend on whether that container exposes an option to remove ActiveX controls dynamically.

    Note that the control array approach requires that at least one element of the control array is already present on the Form/UserControl during design time. The Controls collection option2 has no such requirement, so it might be the more appropriate choice in your situation. You might want to check out these links to learn more about them:




    1 According to MSDN, the Player.close method just "closes the current digital media file, not Windows Media Player itself."
    2 I'm not sure if this is available in VB5.

Tags for this Thread

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