PDA

Vollständige Version anzeigen : Wie ermittle ich einen UNC-Pfad?


Stefan Kulpa
27.01.2003, 18:48
Um von einem "gemappten" Netzlaufwerk den UNC-Pfad ermitteln zu können, kann man das Win32-API benutzen.

<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Function</span> GetUNCPath(<span class="TOKEN">ByVal</span> sLocalPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Methode: | Konvertiert einen Pfad in UNC-Pfad (\\SERVER\...)</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Parameter: | sLocalPath = g&uuml;ltiger, lokaler Pfad (X:\..)</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | bei Erfolg = UNC-Pfad</span>
<span class="REM">'// | bei Fehler = sLocalPath</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="TOKEN">Const</span> NO_ERROR <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 0
<span class="TOKEN">Dim</span> sUNCPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sResult <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sDrive <span class="TOKEN">As</span> <span class="TOKEN">String</span>
GetUNCPath = sLocalPath
<span class="TOKEN">If</span> VBA.Mid$(sLocalPath, 2, 1) &lt;&gt; &quot;:&quot; <span class="TOKEN">Then</span> <span class="TOKEN">Exit Function</span>
<span class="REM">'// Die API-Funktion ben&ouml;tigt nur das Laufwerk!</span>
sDrive = VBA.Left$(sLocalPath, 2)
sUNCPath = VBA.String(260, 0)
<span class="TOKEN">If</span> WNetGetConnection(sDrive, _
sUNCPath, _
VBA.Len(sUNCPath)) = NO_ERROR <span class="TOKEN">Then</span>
sResult = _
VBA.Left$(sUNCPath, VBA.InStr(sUNCPath, vbNullChar) - 1)
<span class="TOKEN">If</span> VBA.Len(sResult) &gt; 0 <span class="TOKEN">Then</span>
GetUNCPath = sResult &amp; VBA.Mid$(sLocalPath, 3)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

JPA
12.06.2006, 17:18
Vielen dank für das posten dieser fkt!

Den declare-teil ist noch anzugeben:
Declare Function WNetGetConnection Lib "mpr.dll" _
Alias "WNetGetConnectionA" ( _
ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
cbRemoteName As Long) As Long

Gruß
JPA