Results 1 to 8 of 8

Thread: Project App.path from inside UC in OCX? [Solved]

  1. #1

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    198

    Question Project App.path from inside UC in OCX? [Solved]

    Hi guys! Im trying to find the real app.path where a project is run from inside the code of a usercontrol compiled as OCX.
    Instead of what I expect I get:
    IN IDE: The App.Path is OCX project folder.
    COMPILED OCX: Where the OCX reside. (wich if its installed on the project using it its fine, but it can be installed on system32 or other folder)

    In other words, I got a project in folder A and the ocx in folder B .... In project A I put the usercontrol and try to load and image using the name of the image concatenated with the app.path (filename = app.path & "\IMG\xxx.png") and whats happening is instead of A\IMG\xxx.png it try to load B\IMG\xxx.png.

    So the question is: How to get the current executable/project path where the usercontrol inside the OCX is being used ?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Project App.path from inside UC in OCX?

    I'm not sure controls should be breaking encapsulation that way.

    This sounds like a serious design problem, but there is a cheap fix: Rewrite the UserControl with a writeable property like ClientAppPath and assign a value to it at runtime within the client code.

    If you must break encapsulation you will probably need to make API calls to go module spelunking. If this was a normal thing to do there would already be easy to use support, at least a single API call.

  3. #3

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    198

    Re: Project App.path from inside UC in OCX?

    Quote Originally Posted by dilettante View Post
    I'm not sure controls should be breaking encapsulation that way.

    This sounds like a serious design problem, but there is a cheap fix: Rewrite the UserControl with a writeable property like ClientAppPath and assign a value to it at runtime within the client code.

    If you must break encapsulation you will probably need to make API calls to go module spelunking. If this was a normal thing to do there would already be easy to use support, at least a single API call.
    The problem is in a class I use to load images wich is part of the ocx (with the usercontrols that use it) . I already fixed it setting at project load a global BasePath for all operations wich (if set) supercede the app.path .... this fixes the EXE but not inside the IDE.
    You're right, following encapsulation rules the ocx should not be aware where it is used but must be some way to get the path (seems like very basic info)

  4. #4
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Project App.path from inside UC in OCX?

    You can get the path of the process, not the path of the control or COM object.

    you can put code in ocx project:
    Code:
    Dim AppPath2 As String
    Sub SetAppPathFromExe(AppPath As String)
        AppPath2 = AppPath
    End Sub
    
    'use AppPath2  replace app.path
    run from exe:
    SetAppPathFromExe app.path
    Last edited by xiaoyao; Jul 30th, 2021 at 08:38 PM.

  5. #5

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    198

    Re: Project App.path from inside UC in OCX?

    Quote Originally Posted by xiaoyao View Post
    You can get the path of the process, not the path of the control or COM object.

    you can put code in ocx project:
    Code:
    Dim AppPath2 As String
    Sub SetAppPathFromExe(AppPath As String)
        AppPath2 = AppPath
    End Sub
    
    'use AppPath2  replace app.path
    run from exe:
    SetAppPathFromExe app.path
    Hi xiaoyao! My problem is to fix it from the IDE in Design mode. I found a workarround when running (exe or ide)

  6. #6
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: Project App.path from inside UC in OCX?

    ?/??/

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Project App.path from inside UC in OCX?

    This is some approach using a hack.
    The caveat is that when in IDE you need to open the form containing the UserControl at least once to save the client app path (and every time the app path changes), because it only can get it at design time and not at run time when in IDE.
    Attached Files Attached Files

  8. #8

    Thread Starter
    Addicted Member shagratt's Avatar
    Join Date
    Jul 2019
    Location
    Argentina
    Posts
    198

    Re: Project App.path from inside UC in OCX?

    Quote Originally Posted by Eduardo- View Post
    This is some approach using a hack.
    The caveat is that when in IDE you need to open the form containing the UserControl at least once to save the client app path (and every time the app path changes), because it only can get it at design time and not at run time when in IDE.
    Just Perfect!!!! I mixed it to my other solution and now I can get the desired path always from the dll.
    Big thanks Javi!

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