Results 1 to 22 of 22

Thread: [RESOLVED] File Association Problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Resolved [RESOLVED] File Association Problem

    I am trying to run a program using file type association. It runs from the command line without a problem, but when activated via File Explorer, I get an error 52: Bad file name or number. I have verified the File Name and Path is correct.

    Is this a permission problem, and if so, how do I get around it?

    J.A. Coutts

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,907

    Re: File Association Problem

    I assume that you are retrieving the file name and path from the command line. Add some code in your program to display what is retrieved when you get an error and you will likely locate the problem.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: File Association Problem

    Quote Originally Posted by jdc2000 View Post
    I assume that you are retrieving the file name and path from the command line. Add some code in your program to display what is retrieved when you get an error and you will likely locate the problem.
    Information retrieved direct from routine causing the error.
    Code:
        On Error GoTo FileError
    FileError:
        MsgBox FileName & " Error: " & Str$(Err) & Error$(Err)
    J.A. Coutts

  4. #4
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    508

    Re: File Association Problem

    quotes?

  5. #5
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,907

    Re: File Association Problem

    So what was the file name that caused the error? Also, show the code where you retrieve the file name, and the code with the line that is triggering the error message.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: File Association Problem

    Quote Originally Posted by dz32 View Post
    quotes?
    Good point! That is possible for filenames that include spaces, but this problem occurs for all intended files.

    J.A. Coutts

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: File Association Problem

    Quote Originally Posted by jdc2000 View Post
    So what was the file name that caused the error? Also, show the code where you retrieve the file name, and the code with the line that is triggering the error message.
    Code:
    Public Sub GetFile(FileName As String)
        Dim iFile As Integer
        frmview.Caption = FileName
        On Error GoTo FileError
        If Len(FileName) = 0 Then
            MsgBox "Nothing Selected!", vbExclamation
            Exit Sub
        End If
        iFile = FreeFile()
        Open FileName For Binary Shared As iFile
        ReDim ByteBuffer(LOF(iFile) - 1)
        Get #iFile, , ByteBuffer
        Close #iFile
        Exit Sub
    FileError:
        MsgBox CStr(iFile) & FileName & " Error: " & Str$(Err) & Error$(Err)
    End Sub
    Any file name (eg. mushroom.jpg) used by file association routine causes the problem. Exact same routine is used by CommonDialog routine without a problem.

    J.A. Coutts

  8. #8
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,657

    Re: File Association Problem

    Quote Originally Posted by couttsj View Post
    Code:
    Public Sub GetFile(FileName As String)
        Dim iFile As Integer
        frmview.Caption = FileName
        On Error GoTo FileError
        If Len(FileName) = 0 Then
            MsgBox "Nothing Selected!", vbExclamation
            Exit Sub
        End If
        iFile = FreeFile()
        Open FileName For Binary Shared As iFile
        ReDim ByteBuffer(LOF(iFile) - 1)
        Get #iFile, , ByteBuffer
        Close #iFile
        Exit Sub
    FileError:
        MsgBox CStr(iFile) & FileName & " Error: " & Str$(Err) & Error$(Err)
    End Sub
    Any file name (eg. mushroom.jpg) used by file association routine causes the problem. Exact same routine is used by CommonDialog routine without a problem.

    J.A. Coutts
    Just to clarify - is the FileName value only the filename (eg. mushroom.jpg)? Or is it the full path to the filename (eg. C:\Images\Nature\mushroom.jpg)?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: File Association Problem

    Quote Originally Posted by OptionBase1 View Post
    Just to clarify - is the FileName value only the filename (eg. mushroom.jpg)? Or is it the full path to the filename (eg. C:\Images\Nature\mushroom.jpg)?
    Full path & file name. FTI, the program is used as the default program when picture files are selected in Windows Explorer or other utility such as email attachments. There is not a problem when the file is selected from a CommonDialog routine, or when the program and file are entered at the command prompt.

    J.A. Coutts

  10. #10
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    Espaņa
    Posts
    290

    Re: File Association Problem

    when I use Command $, sometime I get invalid characters
    Code:
    Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
    Private Declare Function CommandLineToArgvW Lib "Shell32" (ByVal lpCmdLine As Long, pNumArgs As Long) As Long
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long
    Private Declare Function lstrcpyn Lib "kernel32" Alias "lstrcpynW" (ByVal lpString1 As Long, ByVal lpString2 As Long, ByVal iMaxLength As Long) As Long
    Private Declare Function GetMem4 Lib "msvbvm60" (Src As Any, dst As Any) As Long
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    
    Public Function ParsedCommandLine(Out() As String) As Boolean
        Dim ptr     As Long
        Dim Count   As Long
        Dim Index   As Long
        Dim strLen  As Long
        Dim strAdr  As Long
        ptr = CommandLineToArgvW(GetCommandLine(), Count)
        If Count < 1 Then Exit Function
        ReDim Out(Count - 1)
        For Index = 0 To Count - 1
            GetMem4 ByVal ptr + Index * 4, strAdr
            strLen = lstrlen(strAdr)
            Out(Index) = Space(strLen)
            lstrcpyn StrPtr(Out(Index)), strAdr, strLen + 1
        Next
        GlobalFree ptr
        ParsedCommandLine = True
    End Function
    
    Sub Main()
        Dim arg() As String
        If ParsedCommandLine(arg) Then
            Dim idx   As Long
            For idx = 1 To UBound(arg)
                MsgBox arg(idx)
            Next
        End If
    End Sub

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: File Association Problem

    It sounds like there is a problem with the filename. You should show what the filename variable contains at such time the error occurs.
    Bad filename or number almost always a problem with the path or filename

    You should show us exactly what the path & filename is when the error occurs.

  12. #12
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,907

    Re: File Association Problem

    Unicode characters in filename or path?

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,489

    Re: File Association Problem

    Quote Originally Posted by couttsj View Post
    Full path & file name. FTI, the program is used as the default program when picture files are selected in Windows Explorer or other utility such as email attachments. There is not a problem when the file is selected from a CommonDialog routine, or when the program and file are entered at the command prompt.

    J.A. Coutts
    sounds like the problem is in the configuratiom of setting the association. How was it done? How are you invoking it? My guess is that the Open command doesn't have the parameters set on it properly and so it isn't really passing in the selected file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: File Association Problem

    dz32 had the right idea. I was so used to seeing quotes around string info that I did not recognize that the operating system had added quotes to the full path & filename. I had to remove the quotes in order for it to work.
    Code:
    Private Sub Form_Activate()
        If Len(Command) Then
            Dim sCmd As String
            sCmd = Replace(Command, """", "")
            Call GetFile(sCmd)
        End If
    End Sub
    I had to transfer Command to another variable to get it to work. Using the same variable caused an "object required" error.

    J.A. Coutts

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,839

    Re: File Association Problem

    If you didn't want quotes then don't add them in the command line template that you cram into the registry. If you leave them in then just:

    Code:
    Private Sub GetFile(ByRef FileName As String)
    :
    End Sub
    
    Private Sub Form_Load()
        Dim C As String
    
        C = Command$()
        If LenB(C) Then GetFile Mid$(C, 2, Len(C) - 2)
    End Sub
    That obsolete Call keyword left over from early DOS Basic is a deathtrap. Code analyzers for VB6 flag it for correction for good reasons.

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,839

    Re: [RESOLVED] File Association Problem

    BTW:

    The error you ran into was probably from trying to assign a value to a degenerate call to the Variant function Command(). There is no reason not to call Command$() instead, and don't try to assign to it.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: [RESOLVED] File Association Problem

    Quote Originally Posted by dilettante View Post
    BTW:

    The error you ran into was probably from trying to assign a value to a degenerate call to the Variant function Command(). There is no reason not to call Command$() instead, and don't try to assign to it.
    Command or Command$ produce the same result. Adding a new default program in Win 8.1 was dead easy. The operating system handled it all. I had nothing to do with creating a registry entry.

    I have not tested this on Win 10, because Win 10 does not provide the tools to add default programs. The graphical handling of assigning extensions in Win 10 is an abomination in my humble opinion.

    J.A. Coutts

  18. #18
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,704

    Re: [RESOLVED] File Association Problem

    Quote Originally Posted by couttsj View Post
    Command or Command$ produce the same result.
    J.A. Coutts
    No they do not, they just seem to.
    Command$ produces a string
    Command produces a variant.

    The same is true with other VB statements and functions that have both a $ version and a non $ version such as Mid$(), Right$(), Left$() and so on.
    You should always use the string versions in VB.

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: [RESOLVED] File Association Problem

    Quote Originally Posted by DataMiser View Post
    No they do not, they just seem to.
    Command$ produces a string
    Command produces a variant.

    The same is true with other VB statements and functions that have both a $ version and a non $ version such as Mid$(), Right$(), Left$() and so on.
    You should always use the string versions in VB.
    Point taken. I didn't know Command$ existed until today.

    J.A. Coutts

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,126

    Re: [RESOLVED] File Association Problem

    Although Win 10 has removed the ability to add a default program via the "Settings" route, I did find a way around the problem.

    Using the Windows Explorer, navigate to a file that you wish to activate using a different program that is not included in Win 10 default programs or MS store (eg. MyPicture.jpg). Right button click on it, and left button click on "Open with". Then click on "Choose another app". Now scroll down to the bottom and choose "More apps". Then scroll down to the bottom and choose "Look for another app on this PC". Navigate to the program you wish to use, and open it. The program will be added to your default programs. You can choose to use it for all the file types you have chosen now or later.

    MS wants to encourage using apps that they promote, but obviously they have not removed the ability to add default programs.

    J.A. Coutts

  21. #21
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    508

    Re: [RESOLVED] File Association Problem

    you can do it programatically. probably have to run as admin though, this sample registers the .idajs extension

    Code:
    Function register_idajsFileExt() As Boolean
        
        Dim homedir As String
        Dim tmp As String
           
        homedir = App.path & "\IDA_JScript.exe"
        If Not fso.FileExists(homedir) Then Exit Function
        cmd = "cmd /c ftype IDAJS.Document=""" & homedir & """ %1 && assoc .idajs=IDAJS.Document"
        
        On Error Resume Next
        Shell cmd, vbHide
        
        Dim wsh As Object 'WshShell
        Set wsh = CreateObject("WScript.Shell")
        If Not wsh Is Nothing Then
            wsh.RegWrite "HKCR\IDAJS.Document\DefaultIcon\", homedir & ",0"
        End If
        
        tmp = ReadRegValue("HKLM\SOFTWARE\Classes\IDAJS.Document\shell\open\command")
        register_idajsFileExt = (Len(tmp) > 0)
        
    End Function

  22. #22
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,907

    Re: [RESOLVED] File Association Problem


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