Freitag, 25. Juni 2010

Erzeugen von UNC Pfadnamen / Creating UNC Pathnames (Revisited)

Nachdem ich heute zufälligerweise in eine alte Funktion von mir reingeschaut habe (Erzeugen von UNC-Pfadnamen), gab es auch gleich Verbesserungsbedarf. Im Blogeintrag vom September 2008 konnte nur ein Laufwerksbuchstabe (bspw. H:) übergeben werden. Die neue Funktion habe ich nun dahingehend erweitert, dass es jetzt sowohl reine Laufwerksbuchstaben, als auch komplette Verzeichnis- und/oder Dateinamen sein dürfen. Die verschiedenen Möglichkeiten sind im vorgelagerten Funktionstest zu ersehen.

* // Funktionstest Start    
CLEAR 
?GetFullUNCPath( [H:] )
?GetFullUNCPath( [H:\] )
?GetFullUNCPath( [H:\Foxprogs] )
?GetFullUNCPath( [H:\Foxprogs\] )
?GetFullUNCPath( [H:\Foxprogs\mware71] )
?GetFullUNCPath( [H:\Foxprogs\mware71\] )
?GetFullUNCPath( [H:\Foxprogs\mware71\konf.exe] )
* // Funktionstest Ende        

FUNCTION GetFullUNCPath as String
LPARAMETERS vMappedName as String
    * // Datendeklaration und Initialisierung            
    LOCAL    lcUNCBuffer as String, liLength as Integer, ;
            lcUNCName as String, llContinue as Boolean, ;
            lcMessage as String, lcPath as String
    lcUNCBuffer    = []
    liLength    = 0
    lcUNCName    = []
    llContinue    = .T.
    lcMessage    = []
    vMappedName    = IIF(DIRECTORY( vMappedName ) , ADDBS( vMappedName ) , vMappedName )
    lcPath        = IIF( LEN( vMappedName ) > 2 , SUBSTR( vMappedName , 3 ) , [] )
    vMappedName    = JUSTDRIVE( vMappedName )
    * // Deklaration der API-Funktion                    
    TRY
        DECLARE INTEGER WNetGetConnection IN WIN32API ;
            STRING @ lpLocalName, ;
            STRING @ lpRemoteName, ;
            INTEGER @ lpliLength
    CATCH
        TEXT TO lcMessage NOSHOW TEXTMERGE PRETEXT 2
            Deklaration von WNetGetConnection in WIN32API
            ist fehlgeschlagen. Funktion wird vorzeitig beendet.
        ENDTEXT
        MESSAGEBOX(lcMessage,0+16+0,[Information])
        llContinue = .F.
    ENDTRY
    * // Umsetzung des lokalen Pfades in einen UNC Pfad    
    IF llContinue
        IF !EMPTY( vMappedName ) AND VARTYPE( [vMappedName] ) = [C]
            lcUNCBuffer    = REPL( CHR( 0 ) , 261 )
            liLength    = LEN( lcUNCBuffer )
            vMappedName    = ALLTRIM( vMappedName )
            IF LEN( vMappedName ) = 1
                vMappedName = vMappedName + [:]
            ENDIF
            TRY
                IF WNetGetConnection( vMappedName , @lcUNCBuffer , @liLength ) = 0
                    lcUNCName = LEFT( lcUNCBuffer , AT( CHR( 0 ) , lcUNCBuffer ) - 1 )
                ENDIF
            CATCH
                TEXT TO lcMessage NOSHOW TEXTMERGE PRETEXT 2
                    Aufruf von WNetGetConnection in WIN32API
                    ist fehlgeschlagen. UNC-Pfad konnte nicht
                    generiert werden.
                ENDTEXT
                MESSAGEBOX( lcMessage , 0+16+0 , [Information] )
            ENDTRY
        ENDIF
    ENDIF
    RETURN lcUNCName + lcPath
ENDFUNC

2 Kommentare:

  1. Hi Tom,
    You've been posting some very cool things. But in German :-D
    I'm not asking you to post them in English, but maybe you could just add at the title the translated english title, JUST THE TITLE !
    For example, for this last post, the title could be:
    "Erzeugen von UNC Pfadnamen / Creating UNC path names". That would make Google make your cool article to be found using its engines, and if people get interested, they can use a tranlation tool to get the contents.
    That was just a suggestion!
    Keep the good job !
    Cheers
    Cesar

    AntwortenLöschen
  2. Hi Cesar,

    thanks for that input. I will modify the titles as soon as I get some spare time.

    -Tom

    AntwortenLöschen