File Association Icon

Started by Zlatko Vid, June 01, 2023, 02:27:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid

I found something on codeGuru but that is for VB i think

Private Sub Form_DblClick()

Dim app_path As String

Dim Icon As String

app_path = App.path + "\" + App.EXEName + ".exe"

Icon = App.path + "\" + "book.ico"

retval = Associate_File(".abd", app_path, "abd_ext", _

         "My own extension",

Icon)

MsgBox ("Associated the file extension .abd with this _

         application!")

End Sub

It would be nice to have such icon registered for .o2bas ?
if i found something more Api i will let you know.
  •  

Zlatko Vid

I found this demo project there with code
but this one looks to me too large ,i think that i have something
in Purebasic ...but i lost that one ::
Attribute VB_Name = "File_Commands"
'=======================================================================================
'=======================================================================================
'                                File_Commands.bas
'                            © Joydeep Biswas 2001-2003
'                              Joydeep_B@Hotmail.com
' Contains function declarations of Associate_File() and File_Command(), used to
' associate specific file types with an application, and to add a file command to
' a file type, respectively.
'=======================================================================================
'=======================================================================================


Option Explicit

' Windows Registry Root Key Constants.
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003

' Windows Registry Key Type Constants.
Public Const REG_OPTION_NON_VOLATILE = 0        ' Key is preserved when system is rebooted
Public Const REG_DWORD = 4                      ' 32-bit number

Public Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Public Const REG_SZ = 1                         ' Unicode nul terminated string

Public Const REG_BINARY = 3                     ' Free form binary

Public Const REG_DWORD_BIG_ENDIAN = 5           ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4        ' 32-bit number (same as REG_DWORD)

' Function Error Constants.
Public Const ERROR_SUCCESS = 0
Public Const ERROR_REG = 1

' Registry Access Rights.
Public Const SYNCHRONIZE = &H100000
Public Const READ_CONTROL = &H20000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

' Windows Registry API Declarations.
' Registry API To Open A Key.
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
  ByVal samDesired As Long, phkResult As Long) As Long

' Registry API To Create A New Key.
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
  (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
  ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
  ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long

' Registry API To Query A String Value.
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  ' Note that if you declare the lpData parameter as String, you must pass it By Value.

' Registry API To Query A Long (DWORD) Value.
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  lpType As Long, lpData As Long, lpcbData As Long) As Long

' Registry API To Query A NULL Value.
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long

' Registry API To Set A String Value.
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
  ' Note that if you declare the lpData parameter as String, you must pass it By Value.

' Registry API To Set A Long (DWORD) Value.
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" _
  (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long

' Registry API To Delete A Key.
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  (ByVal hKey As Long, ByVal lpSubKey As String) As Long

' Registry API To Delete A Key Value.
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  (ByVal hKey As Long, ByVal lpValueName As String) As Long

' Registry API To Close A Key.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

' Constants For Error Messages.
Public Const OpenErr = "Error: Opening Registry Key!"
Public Const DeleteErr = "Error: Deleteing Key!"
Public Const CreateErr = "Error: Creating Key!"
Public Const QueryErr = "Error: Querying Value!"
Public Function File_Command(Extension As String, Action As String, Command As String)


  Dim lRtn    As Long     ' API Return Code
  Dim hKey    As Long     ' Handle Of Open Key
  Dim lCdata  As Long     ' The Data
  Dim lValue  As Long     ' Long (DWORD) Value
  Dim sValue  As String   ' String Value
  Dim lRtype  As Long     ' Type Returned String Or DWORD
  Dim KeyName As String
  Dim lsize As Long
 

 ' Open The Registry Key.
  lRtn = RegOpenKeyEx(HKEY_CLASSES_ROOT, Extension, 0&, KEY_ALL_ACCESS, hKey)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox OpenErr
    RegCloseKey (hKey)
    Exit Function
  End If
 
  ' Query Registry Key For Value Type.
  lRtn = RegQueryValueExNULL(hKey, "", 0&, lRtype, 0&, lCdata)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox QueryErr
    RegCloseKey (hKey)
    Exit Function
  End If
 

      sValue = String(lCdata, 0)
      ' Get Registry String Value.
      lRtn = RegQueryValueExString(hKey, "", 0&, lRtype, sValue, lCdata)
 

 
  ' Close The Registry Key.
  RegCloseKey (hKey)


 
  'MsgBox (sValue)
  sValue = Left$(sValue, (Len(sValue) - 1))
 
  'RegCloseKey (hKey)
  KeyName = sValue + "\shell\" + Action
  'MsgBox (KeyName)
   
  ' Create The New Registry Key.
  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, KeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 


 
  sValue = Action        ' Assign Key Value
  lsize = Len(sValue)      ' Get Size Of String
  ' Set String Value.
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, sValue, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
     MsgBox "Error Setting String Value!"
     RegCloseKey (hKey)
     Exit Function
  End If


  ' Close The Registry Key.
  RegCloseKey (hKey)
 
   KeyName = KeyName + "\command"
     
   
  ' Create The New Registry Key.
  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, KeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 
    sValue = Command        ' Assign Key Value
  lsize = Len(sValue)      ' Get Size Of String
  ' Set String Value.
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, sValue, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
     MsgBox "Error Setting String Value!"
     RegCloseKey (hKey)
     Exit Function
  End If


  ' Close The Registry Key.
  RegCloseKey (hKey)
 
 
End Function
Public Function Associate_File(Extension As String, Application As String, Identifier As String, Description As String, Icon As String)

  Dim lRtn    As Long     ' Returned Value From API Registry Call
  Dim hKey    As Long     ' Handle Of Open Key
  Dim lValue  As Long     ' Setting A Long Data Value
  Dim sValue  As String   ' Setting A String Data Value
  Dim lsize   As Long     ' Size Of String Data To Set
  Dim commandline As String
 

  ' Create The New Registry Key, the file extension
  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, Extension, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
     
  lsize = Len(Identifier)      ' Get Size Of identifier String
  ' Set "(Default)" String Value to identifier
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, Identifier, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
      MsgBox "Error Setting String Value!"
      RegCloseKey (hKey)
      Exit Function
  End If

  ' Close The Registry Key.
  RegCloseKey (hKey)

  ' Create The New Registry Key, the file extension identifier
  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, Identifier, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 
    lsize = Len(Description)      ' Get Size Of file type description String
  ' Set (Default) String Value to description of the file type
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, Description, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
      MsgBox "Error Setting String Value!"
      RegCloseKey (hKey)
      Exit Function
  End If

  ' Close The Registry Key.
  RegCloseKey (hKey)


  ' Create The New Registry Key, the default icon key within the identifier key
  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, (Identifier + "\DefaultIcon"), 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 
    lsize = Len(Icon)      ' Get Size Of String
  ' Set (Default) String Value to the full path name of the icon that will be associated with
  '    this file type
 
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, Icon, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
      MsgBox "Error Setting String Value!"
      RegCloseKey (hKey)
      Exit Function
  End If

  ' Close The Registry Key.
  RegCloseKey (hKey)



Identifier = Identifier + "\shell"
  ' Create The New Registry Key, the "shell" key within the identifier key

  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, Identifier, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 
  ' Close The Registry Key.
  RegCloseKey (hKey)


Identifier = Identifier + "\open"
  ' Create The New Registry Key, the "open" command key within the shell key

  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, Identifier, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If
 
  ' Close The Registry Key.
  RegCloseKey (hKey)


Identifier = Identifier + "\command"
  ' Create The New Registry Key, the "command"  key within the "open" command key

  lRtn = RegCreateKeyEx(HKEY_CLASSES_ROOT, Identifier, 0&, vbNullString, REG_OPTION_NON_VOLATILE, _
                          KEY_ALL_ACCESS, 0&, hKey, lRtn)
 
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
    MsgBox CreateErr
  End If

    commandline = (Chr$(34) + Application + Chr$(34) + " " + Chr$(34) + "%1" + Chr$(34))
    lsize = Len(commandline)      ' Get Size Of String
  ' Set (Default) String Value of the "command" key to the command line to be used to open the file
  lRtn = RegSetValueExString(hKey, "", 0&, REG_SZ, commandline, lsize)
  ' Check For An Error.
  If lRtn <> ERROR_SUCCESS Then
      MsgBox "Error Setting String Value!"
      RegCloseKey (hKey)
      Exit Function
  End If

  ' Close The Registry Key.
  RegCloseKey (hKey)

 




End Function

 
  •  

Zlatko Vid

#2
Ok i found something simple (i think)
program is without GUI ..so need addition
first i will test it with my old PB 4.5

ps .why i need this ?
I often find myself confused with files..which is one
should be nice to have at least two one icon for .inc and one for .o2bas
what you think?

;=======================================
;
; Creating registry entries for
; application file association &
; updating associated file icons
;
; by TI-994A - 6th April, 2012
;
;=======================================

#SHCNE_ASSOCCHANGED = $8000000
#SHCNF_IDLIST = $0

Define.s keyName, keyValue
;Define.i Result, keyPtr

;creates an entry for your application
keyName = "MyApp" ;short name of your app
keyValue = "My Application to do something" ;long description of your app
Result = RegCreateKey_(#HKEY_CLASSES_ROOT, keyName, @keyPtr)
Result = RegSetValue_(keyPtr, "", #REG_SZ, @keyValue, 0)

;creates instructions for command-line launch
keyValue = "C:\Test.exe %1" ;replace with ProgramFilename() + " %1"
Result = RegSetValue_(keyPtr, "Shell\Open\Command", #REG_SZ, @keyValue, #MAX_PATH)

;sets the associated file icons to match your application
;the icon keyValue can even be a direct icon, for eg: "c:\someIcon.ico"
keyValue = ProgramFilename()
Result = RegSetValue_(keyPtr, "DefaultIcon", #REG_SZ, @keyValue, #MAX_PATH)

;creates the actual file association for the specified extension
keyName = ".myext" ;can be anything, for eg: ".mp3"
keyValue = "MyApp"
Result = RegCreateKey_(#HKEY_CLASSES_ROOT, keyName, @keyPtr)
Result = RegSetValue_(keyPtr, "", #REG_SZ, @keyValue, 0)

;effects a system-wide notification of the changes
SHChangeNotify_(#SHCNE_ASSOCCHANGED, #SHCNF_IDLIST, 0, 0)
  •  

Charles Pegge

Thanks Aurel, I have translated some of this, and I will test it with great caution. The registry is a rather scary place to navigate for verification.

Zlatko Vid

Yes Charles ..i don't like to mess with registry too,,
i compiled this example with PB4.5 and something get
desktop icons get refresh ...but app icon or extension not changed
i will ask again on PB forum
  •  

Charles Pegge

Hi Aurel,

When I try to create new keys, programmatically I get an ACCESS_DENIED error. I checked I have full access rights with RegEdit, and I am able to edit registry entries there.

Zlatko Vid

Hi Charles

I don't get any real or good answer from PB forum members
they simply don't know what this code do...

last line of code seems that work and just refresh Windows Explorer
I can try it in o2 code ...just i need to collect api functions for that task
then i will try
  •  

Zlatko Vid

Ahh just to mention
when i uninstlall Theo o2 installer registry was cleared and my old icons
come back ...ouch..that is fine
  •  

Zlatko Vid

Ok

This is last called API function:

Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long,
ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
  •  

Zlatko Vid

Hi Charles

Do you try this api:

QuoteSets the data and type of a specified value under a registry key.

LONG RegSetValueEx(
    HKEY hKey,
    LPCTSTR lpValueName,
    DWORD Reserved,
    DWORD dwType,
    CONST BYTE *lpData,
    DWORD cbData
);

Parameters

hKey
    Handle to a currently open key or any of the following predefined reserved handle values:

    HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS HKEY_PERFORMANCE_DATA
lpValueName
    Pointer to a string that contains the name of the value to set. If a value with this name is not already present in the key, the call adds it to the key.

    If lpValueName is NULL or an empty string, "", the call sets the type and data for the key's unnamed or default value.

    Registry keys do not have default values, but they can have one unnamed value, which can be of any type.
Reserved
    Reserved; must be zero.
dwType
    Specifies the type of information to be stored as the value's data. This parameter can be one of these values: 

or

RegSetValueA

I am confused with different terminology  ::)
  •  

Zlatko Vid

I found this code on codeProject

it should work with VC++
hmm lot of code ...

This is the code to register extenstion
=============================================
int RegisterExtenstion( )
{
HKEY hkey;

if (ERROR_SUCCESS==RegOpenKey(HKEY_CLASSES_ROOT,".mfe",&hkey))
{
RegCloseKey(hkey);
AfxMessageBox("This Extension is already registered");
GetDlgItem(IDOK)->ShowWindow(SW_HIDE);
return 0;
}
CString sExt=".mfe";
CString sDescription="My File Extension";

sDescription.Replace(" ",""); //revoing spaces in Description
sExt.Replace(" ",""); // sExt – extension to register

sExt.Replace(".","");
sExt="."+sExt;


CString app=GetCommandLine(); // Getting the application Path

app.TrimLeft(_T("\"")); app.TrimRight(_T("\""));

CString str,strIcon;
str.Format(_T("%s"),app);
PathRemoveFileSpec((LPTSTR)(LPCTSTR)str) ; //for this #include "shlwapi.h"
//#pragma comment(lib,"Shlwapi.lib")
strIcon.Format(_T("%s\\Sample.ico \0"),str);//place "Sample icon" in exe folder

app+=" \"%1\"";

RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);
DWORD dw;

RegCreateKeyEx(hkey, sExt.operator LPCTSTR() , 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw );

CString key=sDescription;

RegSetValueEx(hkey,"",0,REG_SZ,(BYTE *)key.operator LPCTSTR(),
key.GetLength());
RegCloseKey(hkey);

RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);

RegCreateKeyEx (hkey, key, 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL, &hkey, &dw);

RegCreateKeyEx (hkey, "shell", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegCreateKeyEx (hkey, "open", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegCreateKeyEx (hkey, "command", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)app,app.GetLength());
RegCloseKey(hkey);

RegOpenKeyEx(HKEY_CLASSES_ROOT,key,0,KEY_QUERY_VALUE,&hkey);
RegCreateKeyEx (hkey, "DefaultIcon", 0L, NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,&hkey, &dw);

RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)strIcon,strIcon.GetLength());
RegCloseKey(hkey);

return 1;
}

 
  •  

Zlatko Vid

ok if i understand it
this is function:

Function RegisterExtenstion( ) as INT
{
HKEY hkey

if ERROR_SUCCESS = RegOpenKey(HKEY_CLASSES_ROOT,".mfe",&hkey)

RegCloseKey(hkey)
print "This Extension is already registered"
'GetDlgItem(IDOK)->ShowWindow(SW_HIDE);
return 0
End function

String sExt=".mfe"
String sDescription="Oxgen Basic File"
  •