Results 1 to 15 of 15

Thread: Getting the name of a file

  1. #1

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Question Getting the name of a file

    I am using VB6 on WIn 10. I have my desktop background set to change once per day. I want to be able to get the name of the photo and display is on the screen. Displaying it is simple. But how do I get the name in code of the current desktop picture?

    Thanks

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

    Re: Getting the name of a file

    Try calling this function:

    Code:
    Private Declare Function lstrlenW Lib "kernel32.dll" (ByVal lpString As Long) As Long
    Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
    Private Declare Function SystemParametersInfoW Lib "user32.dll" (ByVal uiAction As Long, ByVal uiParam As Long, ByRef pvParam As Any, ByVal fWinIni As Long) As Long
    
    Public Function GetDesktopWallpaperPath() As String
        Const MAX_PATH = 260&, SPI_GETDESKWALLPAPER = &H73&
        Dim sBuffer As String
    
        If SysReAllocStringLen(VarPtr(sBuffer), , MAX_PATH - 1&) Then
            If SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, ByVal StrPtr(sBuffer), 0&) Then
                GetDesktopWallpaperPath = Left$(sBuffer, lstrlenW(StrPtr(sBuffer)))
            End If
        End If
    End Function

  3. #3

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    That code gives me a directory I cannot reach thru the file explorer. It addition it only gives a dir, not the file name.

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

    Re: Getting the name of a file

    Quote Originally Posted by AccessShell View Post
    That code gives me a directory I cannot reach thru the file explorer.
    What is the path that you're getting from that function? Please post it here.

    Quote Originally Posted by AccessShell View Post
    It addition it only gives a dir, not the file name.
    According to the documentation of SPI_GETDESKWALLPAPER, it "retrieves the full path of the bitmap file for the desktop wallpaper." The full path should contain the filename of the wallpaper. It should be easy enough to extract the filename using a combination of VB6's String functions. Do you want an example?

  5. #5

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    "C:\Users\Me.DESKTOP-BL62LB2\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper

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

    Re: Getting the name of a file

    Hmm, that's weird. Is "Me.DESKTOP-BL62LB2" really your profile name? Also, did you forget to add the end quotation mark or is the string really truncated?

  7. #7

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    Yes, I forgot the ". And no it is not Me, it is Shell

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

    Re: Getting the name of a file

    Quote Originally Posted by AccessShell View Post
    And no it is not Me, it is Shell
    What exactly do you mean by "it is Shell"?

  9. #9

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    C:\Users\Shell.DESKTOP-BL62LB2\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper

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

    Re: Getting the name of a file

    I asked you above what path you were getting and you wrote:

    Quote Originally Posted by AccessShell View Post
    "C:\Users\Me.DESKTOP-BL62LB2\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper
    But now you're telling me it's actually:

    Quote Originally Posted by AccessShell View Post
    C:\Users\Shell.DESKTOP-BL62LB2\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper
    I really don't have time to play any games with you. I'll try helping you one last time, so please cooperate!

    Start a new Standard EXE project and paste my code above. Then add this code as well:

    Code:
    Private Sub Form_Load()
        MsgBox """" & GetDesktopWallpaperPath & """", vbInformation
        Unload Me
    End Sub
    Now, compile that project and run the resulting EXE. When the message box comes up, press CTRL+C to copy the message and paste it here.

    If you still get the same result, then I'm out of ideas.

  11. #11

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    Same result
    C:\Users\Shell.DESKTOP-BL62LB2\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper

  12. #12
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Getting the name of a file

    Code from post 2 works for me with exception. I'm on XP. If the wallpaper bitmap fills the entire screen then I get the correct path and file name but if the image only shows in the center of the screen then the path is always the same no matter which picture I select.

    Examples:

    I select from Display Properties--->Desktop--->Santa Fe Stucco (which covers the entire screen) then I get

    C: \Windows\Santa Fe Stucco.bmp

    but if I select from Display Properties--->Desktop--->Stonehenge(which only show a square picture centered in the screen) then I get

    C: \Documents and Settings\Ordinary Guy\Local Settings\Application Data\Microsoft\Wallpaper1.bmp

    I get the above path\filename no matter what the name of the image is that I select
    Last edited by Ordinary Guy; Dec 28th, 2019 at 03:33 PM.

  13. #13

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Getting the name of a file

    I have discovered that "TranscodedWallpaper" is supposed to the file that represents the current background. By copying the file to another place and adding the suffix .jpg you can open with with any photo app. However the photo in "TranscodedWallpaper" is the blue default background, not the photo I have. I checked the CachedFiles subdir and found a file called "CachedImage_1920_1080_POS4.jpg. This is the default blue background.

  14. #14
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    706

    Re: Getting the name of a file

    This is what I get on Windows 7:

    C:\Users\<MyUserIDHere>\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg

  15. #15
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: Getting the name of a file

    Remembered this thread when I came across something today...

    In a project with oleexp.tlb, this method gives you the actual image file:
    Code:
    Dim pAD As ActiveDesktop
    Set pAD = New ActiveDesktop
    Dim szwp As String
    Dim hr As Long
    szwp = String$(MAX_PATH, 0)
    Const AD_GETWP_LAST_APPLIED = &H2
    hr = pAD.GetWallpaper(szwp, MAX_PATH, AD_GETWP_LAST_APPLIED)
    If hr = S_OK Then
        Debug.Print Left$(szwp, InStr(szwp, vbNullChar) - 1)
    Else
        Debug.Print "Error 0x" & Hex$(hr)
    End If
    Worked on both Win7 and Win10.
    Last edited by fafalone; Jan 24th, 2020 at 11:24 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