-
Jun 22nd, 2021, 01:49 PM
#1
[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
-
Jun 22nd, 2021, 02:10 PM
#2
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.
-
Jun 22nd, 2021, 03:07 PM
#3
Re: File Association Problem
Originally Posted by jdc2000
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
-
Jun 22nd, 2021, 03:12 PM
#4
Re: File Association Problem
-
Jun 22nd, 2021, 03:15 PM
#5
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.
-
Jun 22nd, 2021, 03:18 PM
#6
Re: File Association Problem
Originally Posted by dz32
quotes?
Good point! That is possible for filenames that include spaces, but this problem occurs for all intended files.
J.A. Coutts
-
Jun 22nd, 2021, 03:48 PM
#7
Re: File Association Problem
Originally Posted by jdc2000
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
-
Jun 22nd, 2021, 03:54 PM
#8
Re: File Association Problem
Originally Posted by couttsj
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)?
-
Jun 22nd, 2021, 04:30 PM
#9
Re: File Association Problem
Originally Posted by OptionBase1
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
-
Jun 22nd, 2021, 05:55 PM
#10
Hyperactive Member
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
-
Jun 22nd, 2021, 06:59 PM
#11
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.
-
Jun 22nd, 2021, 07:03 PM
#12
Re: File Association Problem
Unicode characters in filename or path?
-
Jun 22nd, 2021, 08:41 PM
#13
Re: File Association Problem
Originally Posted by couttsj
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
-
Jun 22nd, 2021, 10:16 PM
#14
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
-
Jun 23rd, 2021, 01:06 AM
#15
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.
-
Jun 23rd, 2021, 01:12 AM
#16
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.
-
Jun 23rd, 2021, 08:38 AM
#17
Re: [RESOLVED] File Association Problem
Originally Posted by dilettante
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
-
Jun 23rd, 2021, 11:56 AM
#18
Re: [RESOLVED] File Association Problem
Originally Posted by couttsj
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.
-
Jun 23rd, 2021, 12:41 PM
#19
Re: [RESOLVED] File Association Problem
Originally Posted by DataMiser
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
-
Jun 23rd, 2021, 06:06 PM
#20
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
-
Jun 23rd, 2021, 07:53 PM
#21
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
-
Jun 24th, 2021, 09:52 AM
#22
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|