Visualizzazione dei risultati da 1 a 6 su 6

Discussione: [vb6] - WNI o API ??

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36

    [vb6] - WNI o API ??

    ciao ragazzi, salve moderatori

    intanto voglio ringraziare tutti quelli che mi rispondono (o che lo hanno fatto in passato...leggi moderatori, che sono gentilissimi e bravissimi) e quanti lo faranno ora.

    questione:

    sto scrivendo un po di codice per creare le icone della mia applicazione sul desktop, nella barra di avvio, e nella barra veloce in basso (non uso ancora un installer, ma mi ci butterò subitissimo).
    Mi sono imbattuto in due metodi (cosi come mi capitato anche per quanto riguarda la lettura dei dati del sistema, tipo codice seriale dell'HD, MotherBoard, SO, ecc.)

    Domanda (e perdonate se è una castroneria!):

    è meglio usare il metodo con le WMI, oppure con le API ?

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36
    ...mi sa che ho scritto una fesseria.

    allora:
    credo di essermi sbagliato nel titolo. Credo che WMI non c'entri nulla.
    La questione non è WMI o API, ma OBJECT o API.
    Nel senso che per creare una icona nella barra di avvio veloce in basso (credo si chiami Quick launch bar) ho trovato questo codice:

    codice:
    Const APPLICATION_DATA = &H1a&
    Dim MyShortcut 
    Dim QuickLaunchPath
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(APPLICATION_DATA)
    Set objFolderItem = objFolder.Self 
    On Error resume next 
    Set WSHShell = CreateObject("WScript.Shell") 
    If not WSHShell Is Nothing Then 
      QuickLaunchPath = objFolderItem.Path & "\Microsoft\Internet Explorer\Quick Launch"
      Set MyShortcut = WSHShell.CreateShortCut(QuickLaunchPath & "\MyApp.lnk") 
      MyShortcut.TargetPath = "c:\MyPath\MyApp.exe" 
      MyShortcut.WorkingDirectory = "c:\MyPath" 
      MyShortcut.WindowStyle = 1 
      MyShortcut.Arguments = "" 
      MyShortcut.IconLocation = "c:\MyPath\MyApp.exe,0" 
      MyShortcut.Save 
      Set MyShortcut = Nothing 
    end if
    mentre da un'altra parte ho trovato questo cosice:

    codice:
    Option Explicit
    
    Private Declare Function VB4CreateShellLink Lib "STKIT432.DLL" Alias "fCreateShellLink" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long
    Private Declare Function VB5CreateShellLink Lib "VB5STKIT.DLL" Alias "fCreateShellLink" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As Long
    Private Declare Function VB6CreateShellLink Lib "VB6STKIT.DLL" Alias "fCreateShellLink" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String, ByVal fPrivate As Long, ByVal sParent As String) As Long
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    
    Private Enum VersioneVB
        VisualBasicUnknown = 0
        VisualBasic4 = 4
        VisualBasic5 = 5
        VisualBasic6 = 6
    End Enum
    
    Private AppPath As String
    
    Private Function CreateShellLink(ByVal intVersione As VersioneVB, ByVal strGruppo As String, ByVal strTitolo As String, ByVal strPath As String) As Boolean
        Dim intConta As Integer
        Dim lngRis As Long
    
        On Error GoTo Err_Handler
        If intVersione = VisualBasicUnknown Then
            For intConta = 4 To 6
                lngRis = LoadLibrary(Choose(intConta - 3, "STKIT432", "VB5STKIT", "VB6STKIT"))
                Call FreeLibrary(lngRis)
                If lngRis <> 0 Then intVersione = intConta
            Next intConta
        End If
    
        lngRis = 0
        If intVersione = VisualBasic6 Then
            lngRis = VB6CreateShellLink(strGruppo, strTitolo, strPath, "", True, "$(Programs)")
        ElseIf intVersione = VisualBasic5 Then
            lngRis = VB5CreateShellLink(strGruppo, strTitolo, strPath, "")
        ElseIf intVersione = VisualBasic4 Then
            lngRis = VB4CreateShellLink(strGruppo, strTitolo, strPath, "")
        End If
        CreateShellLink = CBool(lngRis)
        Exit Function
    Err_Handler:
        CreateShellLink = False
    End Function
    
    Private Sub Form_Load()
        AppPath = App.Path
        If Right$(AppPath, 1) <> "\" Then AppPath = AppPath & "\"
        AppPath = AppPath & App.EXEName & ".exe"
        txtTitoloCollegamento.Text = App.Title
    End Sub
    
    Private Sub cmdCollegamentoMenuAvvio_Click()
        Call CreateShellLink(VisualBasicUnknown, ".", txtTitoloCollegamento.Text, AppPath)
        
    End Sub
    
    Private Sub cmdCollegamentoDesktop_Click()
        Call CreateShellLink(VisualBasicUnknown, "..\..\Desktop", txtTitoloCollegamento.Text, AppPath)
    End Sub

    che crea collegamento sul desktop e nella barra di avvio a sinistra.
    La mia questione era: come faccio a fondere le due, visto che una usa le api, l'altra un altro sistema?
    Ppotrei mettere la prima routine in un modulo insieme alle routine del metodo con le api, ma mi sembra brutto.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36
    sto rislvendo cosi:


    se per creare l'icona sul desktop ci vuole il seguente codice (che funziona):
    codice:
    'Crea sul DESKTOP
    'C:\Documents and Settings\<nome utente>\Desktop
    Public Sub LinkShellDesktop(Optional NameLinkAppl As String)
        AppPath = App.Path
        If Right$(AppPath, 1) <> "\" Then AppPath = AppPath & "\"
        AppPath = AppPath & App.EXEName & ".exe"
        If NameLinkAppl = "" Then
            NameLinkAppl = App.Title
        End If
        
        Call CreateShellLink(VisualBasicUnknown, "..\..\Desktop", NameLinkAppl, AppPath)
    
    End Sub
    e per creare sul menu di avvio:

    codice:
    'Crea nel MENU AVVIO (TryBar - a sin)
    Public Sub LinkShellMenuAvvio(Optional NameLinkAppl As String)
        AppPath = App.Path
        If Right$(AppPath, 1) <> "\" Then AppPath = AppPath & "\"
        AppPath = AppPath & App.EXEName & ".exe"
        If NameLinkAppl = "" Then
            NameLinkAppl = App.Title
        End If
        Call CreateShellLink(VisualBasicUnknown, ".", NameLinkAppl, AppPath)
    End Sub
    dovrebbe funzionare anche il seguente:

    codice:
    'Crea nella BARRA AVVIO VELOCE IN BASSO (quick launch bar)
    'C:\Documents and Settings\<UserName>\Application Data\Microsoft\Internet Explorer\Quick Launch
    Public Sub LinkShellBarraAvvioVeloce(Optional NameLinkAppl As String)
        AppPath = App.Path
        If Right$(AppPath, 1) <> "\" Then AppPath = AppPath & "\"
        AppPath = AppPath & App.EXEName & ".exe"
        If NameLinkAppl = "" Then
          NameLinkAppl = App.Title
        End If
        
        Call CreateShellLink(VisualBasicUnknown, "..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", NameLinkAppl, AppPath)
    
    End Sub
    ... ma non funziona!!
    mi sa che è il path della barra sbagliato.
    ...ci sto studiando...

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36
    ...sto facendo il post da solo, eheh...ma va bene..sto imparando.

    allora..
    mi sa che posso fare tutto con il seguente codice:

    codice:
    Option Explicit
    
    
    Const APPLICATION_DATA = &H1A&
    
    
    
    Public Sub CreateShellLink2()
    Dim MyShortcut
    Dim QuickLaunchPath
    
    Dim objShell
    Dim objFolder
    Dim objFolderItem
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(APPLICATION_DATA)
    Set objFolderItem = objFolder.Self
    
    On Error Resume Next
    Dim WSHShell
    Set WSHShell = CreateObject("WScript.Shell")
    
      If Not WSHShell Is Nothing Then
      
      QuickLaunchPath = objFolderItem.Path & "\Microsoft\Internet Explorer\Quick Launch"
      Set MyShortcut = WSHShell.CreateShortCut(QuickLaunchPath & "\MyApp.lnk")
      MyShortcut.TargetPath = "c:\MyPath\MyApp.exe"
      MyShortcut.WorkingDirectory = "c:\MyPath"
      MyShortcut.WindowStyle = 1
      MyShortcut.Arguments = ""
      MyShortcut.IconLocation = "c:\MyPath\MyApp.exe,0"
      MyShortcut.Save
      
      Set MyShortcut = Nothing
    
      End If
    
    End Sub

    ..per l'icona sulla quick launch bar (quella sotto) funziona.

    ...sto provando sul desktop e sul menu avvio (in questo caso, elimino le api)...

    vai che ce la fò!!!

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36
    comunque il path della barra sotto è:

    codice:
    ..\..\Dati Applicazioni\Microsoft\Internet Explorer\Quick Launch
    e non

    codice:
    ..\..\Application data\Microsoft\Internet Explorer\Quick Launch


    cioe...doveva essere messo in italiano...!!! (beh, nelle versioni di win in italiano suppongo)..e qui quindi occorrerebbe trovare il sistema di mettere il path per tutte le versioni di win.

    ..mas to risolvendo con l'altro sistema...un mom...

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    36
    CI SONO RIUSCITO !!!! eureka!!

    ecco il codice:

    codice:
    Option Explicit
    
    Const APPLICATION_DATA = &H1A&
    Const DESKTOP = &H0
    Const PROGRAMS = &H2
    
    
    Public Sub CreateShellLink2(dest As String)
    
    Dim DestPath
    Dim MyShortcut
    Dim QuickLaunchPath
    
    Dim objShell
    Set objShell = CreateObject("Shell.Application")
    
    Dim objFolder
    Set objFolder = objShell.Namespace(APPLICATION_DATA)
    Dim objFolderItem
    Set objFolderItem = objFolder.Self
    
    Dim objDesktop
    Set objDesktop = objShell.Namespace(DESKTOP)
    Dim objDesktopItem
    Set objDesktopItem = objDesktop.Self
    
    Dim objPrograms
    Set objPrograms = objShell.Namespace(PROGRAMS)
    Dim objProgramsItem
    Set objProgramsItem = objPrograms.Self
    
    
    On Error Resume Next
    Dim WSHShell
    Set WSHShell = CreateObject("WScript.Shell")
    
      If Not WSHShell Is Nothing Then
      
      Select Case dest
        Case Is = "Desktop"         'DESKTOP
            DestPath = "\"
            QuickLaunchPath = objDesktopItem.Path & DestPath
        
        Case Is = "MenuAvvio"         'MENU AVVIO (a sin)
            DestPath = ""
            QuickLaunchPath = objProgramsItem.Path & DestPath
        
        Case Is = "QuickBar"         'QUICK LAUNCH BAR (quella sotto)
            DestPath = "\Microsoft\Internet Explorer\Quick Launch"
            QuickLaunchPath = objFolderItem.Path & DestPath
      End Select
      
       
      Set MyShortcut = WSHShell.CreateShortCut(QuickLaunchPath & "\MyApp.lnk")
      MyShortcut.TargetPath = "c:\MyPath\MyApp.exe"
      MyShortcut.WorkingDirectory = "c:\MyPath"
      MyShortcut.WindowStyle = 1
      MyShortcut.Arguments = ""
      MyShortcut.IconLocation = "c:\MyPath\MyApp.exe,0"
      MyShortcut.Save
      
      Set MyShortcut = Nothing
    
      End If
    
    End Sub


    non so se forse ci sono troppe dichiarazioni di oggetti. Però funziona.
    Ovviamente myApp e MyPath vanno modificate secondo l'esigenza.

    So che questa cosa la fanno gli installer..ma m'ero messo di puntiglio. E alla fine ci sono riuscito.

    Per eliminare i link, credo vadano cancellati i link (.lnk) appena creati.
    Vediamo se riesco...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.