확장자와 연결된 프로그램의 경로를 얻는다.

● 선언
Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long


● 인수
lpFile ━ 관련 파일명
lpDirectory ━ 기본 폴더 또는 Null
lpResult ━ 결과를 저장할 버퍼


● 반환
성공 ━ 32보다 큰값
실패 ━ 32보다 작은값


소스의 초기 이미지


소스의 결과 이미지


' 확장자에 대한 실행파일 찾기
Option Explicit

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Sub Form_Load() ' 메모리에 로드될때 발생
    txtExtension.Text = "txt"
End Sub

Private Sub txtExtension_KeyDown(KeyCode As Integer, Shift As Integer) ' 키가 눌린 경우 발생
    If Not KeyCode = vbKeyReturn Then Exit Sub
    labReult.Caption = GetConnectPath(txtExtension.Text) ' 확자자와 연결된 프로그램 경로를 얻는다.
End Sub

Public Function GetConnectPath(ByVal Extension As String) As String ' 확장자에 연결된 프로그램 절대 경로를 얻는다
    Const MAX_PATH As Long = 260 ' 버퍼 길이
   
    Dim TempPath As String ' 임시 파일 경로
    Dim TempFileName As String ' 임시 파일명
    Dim ChangeFileName As String ' 변경한 파일명
    Dim ConnectProgramPath As String ' 연결된 프로그램 파일명
    Dim nRet As Long

    If InStr(1, Extension, ".") > 0 Then Extension = Mid$(Extension, InStr(1, Extension, ".") + 1)
    TempPath = Space$(MAX_PATH) ' 임시 파일 경로
    If GetTempPath(MAX_PATH, TempPath) Then ' 임시 파일 경로를 얻는다
        TempPath = GetString(TempPath)
        TempFileName = String$(MAX_PATH, 0) ' 임시 파일명
        If GetTempFileName(TempPath, "~", 0, TempFileName) Then
            TempFileName = GetString(TempFileName)
            ChangeFileName = Left$(TempFileName, InStrRev(TempFileName, ".")) & Extension
            Name TempFileName As ChangeFileName ' 임시 파일명을 변경한다
            ConnectProgramPath = Space$(MAX_PATH) ' 확장자와 연결된 프로그램 경로
            Call FindExecutable(ChangeFileName, vbNullString, ConnectProgramPath)
            GetConnectPath = GetString(ConnectProgramPath)
            Kill ChangeFileName
        End If
    End If
End Function

Private Function GetString(ByVal APIStr As String) As String ' 버퍼로 사용된 문자열에서 유효한 문자열만 반환
    GetString = Left$(APIStr, InStr(APIStr, vbNullChar) - 1)
End Function

+ Recent posts