to Gurus ...Converting a script into FWH

Post Reply
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

to Gurus ...Converting a script into FWH

Post by Silvio.Falconi »

Someone can help me to converte this script into fwh.thanks

http://www.codedisqus.com/0myjWejXXj/ho ... -list.html

I need an array with name and link of application to open a file specific

sample :

aApplicationNames = GetApplicationName(".docx")

return :
Microsoft Office Word
OpenOffice
WordPad.exe
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: to Gurus ...Converting a script into FWH

Post by Antonio Linares »

Silvio,

There are many scripts there. Which one do you need ?

What are you looking for ? Please explain it
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: to Gurus ...Converting a script into FWH

Post by Silvio.Falconi »

I explain :
if i I can for sample GetNameapplication(".Jpg")

it return me an array with the names of appllications (and folder) can open this file type
sample Paint.exe c:\widows\paint.exe

with this array I can create a menupopup and give to final user the possibility to open (or print) the file from my application

I think the last version is the last script

Code: Select all

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace HQ.Util.Unmanaged
{
    /// <summary>
    /// Usage: string executablePath = FileAssociation.GetExecFileAssociatedToExtension(pathExtension, "open");
    /// Usage: string command FileAssociation.GetExecCommandAssociatedToExtension(pathExtension, "open");
    /// </summary>
    public static class FileAssociation
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ext"></param>
        /// <param name="verb"></param>
        /// <returns>Return null if not found</returns>
        public static string GetExecCommandAssociatedToExtension(string ext, string verb = null)
        {
            if (ext[0] != '.')
            {
                ext = "." + ext;
            }

            string  executablePath = FileExtentionInfo(AssocStr.Command, ext, verb);

            // Ensure to not return the default OpenWith.exe associated executable in Windows 8 or higher
            if (!string.IsNullOrEmpty(executablePath) && File.Exists(executablePath) &&
                !executablePath.ToLower().EndsWith(".dll"))
            {
                if (executablePath.ToLower().EndsWith("openwith.exe"))
                {
                    return null; // 'OpenWith.exe' is th windows 8 or higher default for unknown extensions. I don't want to have it as associted file
                }
                return executablePath;
            }
            return executablePath;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ext"></param>
        /// <param name="verb"></param>
        /// <returns>Return null if not found</returns>
        public static string GetExecFileAssociatedToExtension(string ext, string verb = null)
        {
            if (ext[0] != '.')
            {
                ext = "." + ext;
            }

            string executablePath = FileExtentionInfo(AssocStr.Executable, ext, verb); // Will only work for 'open' verb
            if (string.IsNullOrEmpty(executablePath))
            {
                executablePath = FileExtentionInfo(AssocStr.Command, ext, verb); // required to find command of any other verb than 'open'

                // Extract only the path
                if (!string.IsNullOrEmpty(executablePath) && executablePath.Length > 1) 
                {
                    if (executablePath[0] == '"')
                    {
                        executablePath = executablePath.Split('\"')[1];
                    }
                    else if (executablePath[0] == '\'')
                    {
                        executablePath = executablePath.Split('\'')[1];
                    }
                }
            }

            // Ensure to not return the default OpenWith.exe associated executable in Windows 8 or higher
            if (!string.IsNullOrEmpty(executablePath) && File.Exists(executablePath) &&
                !executablePath.ToLower().EndsWith(".dll"))
            {
                if (executablePath.ToLower().EndsWith("openwith.exe"))
                {
                    return null; // 'OpenWith.exe' is th windows 8 or higher default for unknown extensions. I don't want to have it as associted file
                }
                return executablePath;
            }
            return executablePath;
        }

        [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);

        private static string FileExtentionInfo(AssocStr assocStr, string doctype, string verb)
        {
            uint pcchOut = 0;
            AssocQueryString(AssocF.Verify, assocStr, doctype, verb, null, ref pcchOut);

            Debug.Assert(pcchOut != 0);
            if (pcchOut == 0)
            {
                return "";
            }

            StringBuilder pszOut = new StringBuilder((int)pcchOut);
            AssocQueryString(AssocF.Verify, assocStr, doctype, verb, pszOut, ref pcchOut);
            return pszOut.ToString();
        }

        [Flags]
        public enum AssocF
        {
            Init_NoRemapCLSID = 0x1,
            Init_ByExeName = 0x2,
            Open_ByExeName = 0x2,
            Init_DefaultToStar = 0x4,
            Init_DefaultToFolder = 0x8,
            NoUserSettings = 0x10,
            NoTruncate = 0x20,
            Verify = 0x40,
            RemapRunDll = 0x80,
            NoFixUps = 0x100,
            IgnoreBaseClass = 0x200
        }

        public enum AssocStr
        {
            Command = 1,
            Executable,
            FriendlyDocName,
            FriendlyAppName,
            NoOpen,
            ShellNewValue,
            DDECommand,
            DDEIfExec,
            DDEApplication,
            DDETopic
        }



    }
}

and the command is

string applicationName = FileAssoc.GetApplicationName(".docx");
// results in "Microsoft Office Word"




Antonio,
I tried to use Treg class but on this test I can see only one application and not the list of all application
Perhaps I make some error

Code: Select all

#include "Fivewin.ch"


#define KEY_READ        25
#define KEY_WRITE       6
#define KEY_ALL_ACCESS  63

#define REG_SZ      1
#define REG_DWORD   4

#define ERROR_SUCCESS   0


#define  HKEY_CLASSES_ROOT       2147483648        // 0x80000000
#define  HKEY_CURRENT_USER       2147483649        // 0x80000001
#define  HKEY_LOCAL_MACHINE      2147483650        // 0x80000002
#define  HKEY_USERS              2147483651        // 0x80000003
#define  HKEY_PERFORMANCE_DATA   2147483652        // 0x80000004
#define  HKEY_CURRENT_CONFIG     2147483653        // 0x80000005
#define  HKEY_DYN_DATA           2147483654        // 0x80000006


Function test()

   ShowAssociation(".bmp")

   return nil



Function ShowAssociation(cExt)


  LOCAL cKey
  LOCAL cVal

  BEGIN SEQUENCE

    cKey := 'Software\CLASSES\' + cExt


 IF !IsRegistryKey(HKEY_LOCAL_MACHINE, cKey)
       BREAK
    ENDIF

cVal := GetRegistryValue(HKEY_LOCAL_MACHINE, cKey)


  IF EMPTY(cVal)
      BREAK
    ENDIF


 cKey := 'Software\CLASSES\' + cVal + '\Shell\Open\command'



    IF !IsRegistryKey(HKEY_LOCAL_MACHINE, cKey)
      BREAK
    ENDIF


cVal := GetRegistryValue(HKEY_LOCAL_MACHINE, cKey)

    IF EMPTY(cVal)
      BREAK
    ENDIF
                        *GETEXEFILENAME

 IF UPPER(cVal) == UPPER(EXENAME())
      MsgInfo(UPPER(cExt) + ' files are currently associated with MPM.')
    ELSE
      MsgInfo(UPPER(cExt) + ' files are currently associated with ' + ;
        cVal + '.')
    ENDIF
  RECOVER
    Msginfo(UPPER(cExt) + ;
      ' files are not currently associated with any program.')
  END SEQUENCE



     cKey := 'Software\CLASSES\' + cVal + '\OpenWithList'

      IF !IsRegistryKey(HKEY_LOCAL_MACHINE, cKey)
      BREAK
    ENDIF


cVal := GetRegistryValue(HKEY_LOCAL_MACHINE, cKey)



 *HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jnlp\OpenWithList



RETURN nil

//----------------------------------------------------------------------------------//
FUNCTION IsRegistryKey( nKey, cRegKey )

   LOCAL oReg
   LOCAL lExist

   oReg   := TReg32():New( nKey, cRegKey, .F. )
   lExist := ( oReg:lError == .F. )

   oReg:Close()

RETURN lExist
//-------------------------------------------------------------------------------------//
FUNCTION GetRegistryValue( nKey, cRegKey, cRegVar, cType )

   LOCAL oReg
   LOCAL uVal

   DEFAULT cRegVar := '', cType := 'C'

   oReg := TReg32():New( nKey, cRegKey, .F. )

   IF ! oReg:lError

      DO CASE
      CASE cType == 'N'
         uVal := 0
      CASE cType == 'D'
         uVal := CToD( '' )
      CASE cType == 'L'
         uVal := .F.
      OTHERWISE
         uVal := ''
      ENDCASE

      uVal := oReg:Get( cRegVar, uVal )
      IF oReg:nError != ERROR_SUCCESS
         uVal := NIL
      ENDIF

   ENDIF

   oReg:Close()

RETURN uVal
//-------------------------------------------------------------------------------------/
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: to Gurus ...Converting a script into FWH

Post by AntoninoP »

I think it simpler execute only the default program of a file.
You can do it with ShellExecute (https://msdn.microsoft.com/en-us/librar ... 85%29.aspx)
For example:

Code: Select all

ShellExecute( GetActiveWindow() ,nil,'c:\work\testsample.doc','','',5)
Regards,
Antonino
User avatar
Adolfo
Posts: 815
Joined: Tue Oct 11, 2005 11:57 am
Location: Chile
Contact:

Re: to Gurus ...Converting a script into FWH

Post by Adolfo »

AntoninoP wrote:I think it simpler execute only the default program of a file.
You can do it with ShellExecute (https://msdn.microsoft.com/en-us/librar ... 85%29.aspx)
For example:

Code: Select all

ShellExecute( GetActiveWindow() ,nil,'c:\work\testsample.doc','','',5)
Regards,
Antonino
+1 ...as simple as that
;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Lenovo Legion Y520, 16GB Ram, 1 TB NVME M.2, 1 TB SSD, GTX 1050
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: to Gurus ...Converting a script into FWH

Post by Silvio.Falconi »

Sorry but I like to have an array with the exe application can open a specific extension
the fianl user can select the app he want to open that file
sample if I show a jpg the final user can select
Paint.exe
Infarview.exe

as you can see on this picture

Image
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Post Reply