export prototype NUMBER    MakeAssociation(STRING, STRING);
export prototype NUMBER    MakeAssociationEx(STRING, STRING, STRING);
export prototype NUMBER    QueryAssociation(STRING, BYREF STRING);



////////////////////////////////////////////////////////////////////////
//                                                                    //
//  Function:   MakeAssociation                                       //
//                                                                    //
//  Purpose:    Creates a file association in the Registry.  Use      //
//            this function for Explorer shell only, as it will       //
//            not create the extra application identification keys,   //
//            which are necessary for Program Mananger shell.         //
//            For Program Manager shell, you can use the script-      //
//            based function MakeAssociationEx.                       //
//                                                                    //
//  Parameters:                                                       //
//      szApp:    String with full path to the executable to be       //
//                associated with the file extension.                 //
//                                                                    //
//      szExtension:  String with the extension to be associated      //
//                with an application.  Must include the preceding    //
//                period (for example:  ".txt")                       //
//                                                                    //
////////////////////////////////////////////////////////////////////////

  function MakeAssociation(szApp, szExtension)
  BOOL      bResult;
  NUMBER    nResult;

  begin

    RegDBSetDefaultRoot ( HKEY_CLASSES_ROOT );

/*    bResult = RegDBKeyExist(szExtension);
    if (bResult = TRUE) then
      nResult = AskYesNo ("Warning the extension " + szExtension + " is already registered. Overwrite?", YES);
      if (nResult = NO) then
         return -1;
      endif;
    endif;
*/
    LongPathToShortPath (szApp);
    szApp = szApp + " \"%1\"";
    RegDBCreateKeyEx (szExtension, "");
    RegDBCreateKeyEx (szExtension + "\\shell", "");
    RegDBCreateKeyEx (szExtension + "\\shell\\open", "");
    RegDBCreateKeyEx (szExtension + "\\shell\\open\\command", "");
    RegDBSetKeyValueEx (szExtension + "\\shell\\open\\command", "", REGDB_STRING, szApp, -1);

    return 0;
  end;   //End of function MakeAssociation

////////////////////////////////////////////////////////////////////////
//                                                                    //
//  Function:   MakeAssociationEx                                     //
//                                                                    //
//  Purpose:    Same as MakeAssociation, but also creates an          //
//              application identification key, so it can be used     //
//              on Program Manager shell, in addition to Explorer     //
//                                                                    //
//  Parameters:                                                       //
//    szApp:  String with full path to executable for association     //
//                                                                    //
//    szExtension:  String with extension, including preceding '.'    //
//                                                                    //
//    szName: String with name to be used as application              //
//            identification                                          //
//                                                                    //
////////////////////////////////////////////////////////////////////////



  function MakeAssociationEx(szApp, szExtension, szName)
  BOOL      bResult;
  NUMBER    nResult;
  STRING    svKeyName;

  begin
    RegDBSetDefaultRoot ( HKEY_CLASSES_ROOT );

    StrSub ( svKeyName , szExtension , 1 , 3 );
    svKeyName = svKeyName + "File";

/*    bResult = RegDBKeyExist(szExtension);
    if (bResult = TRUE) then
      nResult = AskYesNo ("Warning the extension " + szExtension + " is already registered. Overwrite?", YES);
      if (nResult = NO) then
         return -1;
      endif;
    endif;
*/
    LongPathToShortPath (szApp);
    szApp = szApp + " \"%1\"";
    RegDBCreateKeyEx (szExtension, "");
    RegDBSetKeyValueEx ( szExtension , "" , REGDB_STRING , svKeyName , -1 );

    RegDBCreateKeyEx (svKeyName, "");
    RegDBSetKeyValueEx ( svKeyName , "" , REGDB_STRING , szName , -1 );
    RegDBCreateKeyEx (svKeyName + "\\shell", "");
    RegDBCreateKeyEx (svKeyName + "\\shell\\open", "");
    RegDBCreateKeyEx (svKeyName + "\\shell\\open\\command", "");
    RegDBSetKeyValueEx (svKeyName + "\\shell\\open\\command", "", REGDB_STRING, szApp, -1);

    return 0;
  end;  //End of function MakeAssociationEx


////////////////////////////////////////////////////////////////////////
//                                                                    //
//  Function:   QueryAssociation                                      //
//                                                                    //
//  Purpose:    Searches in Registry for an association for a         //
//              specific file extension.  Returns the command line    //
//              from association.                                     //
//                                                                    //
//  Parameters:                                                       //
//    szExt:    String with file extension to search for.  Must       //
//              include preceding period.                             //
//                                                                    //
//    svCmd:    String that will contain the command line, if an      //
//              association is found.                                 //
//                                                                    //
//  Function will return -1 if no association is found.               //
//                                                                    //
////////////////////////////////////////////////////////////////////////

  function QueryAssociation(szExt, svCmd)
  NUMBER    nResult, nvType, nvSize;
  BOOL      bResult;
  STRING    svName;

  begin
    bResult = RegDBKeyExist(szExt);
    if (bResult = FALSE) then //no association
      svCmd = "";
      return -1;
    endif;
    bResult = RegDBKeyExist(szExt + "\\shell\\open\\command");
    if (bResult = TRUE) then
      nResult = RegDBGetKeyValueEx ( szExt + "\\shell\\open\\command" , "" ,
        nvType , svCmd , nvSize );
      if (nResult = 0) then
        return 0;
      else
        return -1;
      endif;
    else
      nResult = RegDBGetKeyValueEx ( szExt, "", nvType, svName, nvSize);
      if (nResult < 0) then
        return -1;
      endif;
      nResult = RegDBGetKeyValueEx ( svName + "\\shell\\open\\command" , "" ,
        nvType , svCmd , nvSize );
      if (nResult = 0) then
        return 0;
      else
        return -1;
      endif;
    endif;
  end;  // End of function QueryAssociation


