Re: API Question - UNC path with VB extraction vs. mapped drives

Jul 10, 2003 1 Replies

Folks,



I found what I need to use. Thanks to NathanN!


Option Explicit '// 32-bit Function version. '// Note: Declare Function WNetGetConnection32 Lib "mpr.dll" _ Alias "WNetGetConnectionA" ( _ ByVal lpszLocalName As String, _ ByVal lpszRemoteName As String, _ lSize As Long) As Long



'// 32-bit declarations: Dim lpszRemoteName As String Dim lSize As Long



'// Use for the return value of WNetGetConnection() API. Const NO_ERROR As Long = 0



'// The size used for the string buffer. Adjust this if you '// need a larger buffer. Const lBUFFER_SIZE As Long = 255



Function UNCPath(sDriveLetter As String) '// Takes specified Local Drive Letter '// eg E,D,H Etc and converts to UNC



Dim cbRemoteName As Long Dim lStatus As Long



'// Add a colon to the drive letter entered. sDriveLetter = sDriveLetter & ":"



'// Specifies the size in charaters of the buffer. cbRemoteName = lBUFFER_SIZE



'// Prepare a string variable by padding spaces. lpszRemoteName = lpszRemoteName & Space(lBUFFER_SIZE)



' Return the UNC path (eg.\\Server\Share). lStatus = WNetGetConnection32( _ sDriveLetter, _ lpszRemoteName, _ cbRemoteName)



'// Has WNetGetConnection() succeeded. '// WNetGetConnection()returns 0 (NO_ERROR) '// if it succesfully retrieves the UNC path. If lStatus = NO_ERROR Then ' Display the UNC path. UNCPath = lpszRemoteName Else ' Unable to obtain the UNC path. UNCPath = "NO UNC path" End If



End Function



Sub GetUNCPath() Dim sDriveLetter As String Dim cbRemoteName As Long Dim lStatus As Long



Again: '// Prompt the user to type the mapped drive letter. sDriveLetter = UCase(InputBox( _ "Enter Drive Letter of Your Network" & _ "Connection." & Chr(10) & "eg. Y (NO colon)")) If sDriveLetter = "" Then End If Len(sDriveLetter) > 1 Then GoTo Again



'// Add a colon to the drive letter entered. sDriveLetter = sDriveLetter & ":"



'// Specifies the size in charaters of the buffer. cbRemoteName = lBUFFER_SIZE



' Prepare a string variable by padding spaces. lpszRemoteName = lpszRemoteName & Space(lBUFFER_SIZE)



' Return the UNC path (eg.\\Server\Share). lStatus& = WNetGetConnection32(sDriveLetter, _ lpszRemoteName, _ cbRemoteName)



'// Has WNetGetConnection() succeeded. '// WNetGetConnection()returns 0 (NO_ERROR) '// if it succesfully retrieves the UNC path. If lStatus& = NO_ERROR Then '// Display the UNC path. MsgBox lpszRemoteName, vbInformation Else '// Unable to obtain the UNC path. MsgBox "Unable to obtain the UNC path for " & _ sDriveLetter, vbInformation End If



End Sub


Guy Edk> > For all you clever systems people out there:
> >
> > When getting the path of an item in SW (GetPathName)it returns the
> > mapped drive for the current session. Is there any way to get the UNC
> > path instead? Using subclassing the Windows API perhaps? This avoids
> > problems on systems without the same mappings for an application
> > currently under development.
>
> SW doesn't seem to understand the notion of UNC paths. This is whay you
> can't have two different parts with the same name open at the same time.
> Despite being two unique, easily distinguishable files, SW dumbly
> looks at the file name only.
>
> On top of that, as you have discovered, you have the problem with mapped
> drives. Everyone working on a common project must have the same drive
> mappings or they must browse using the full UNC paths from Network
> Neighborhood. Aggravating.
>
> Jim S.
>

Folks,

I was successful in getting what I wanted done. So now when I do a VB BOM extraction from an assembly, no matter if the files are local, appear on a local mapped drive, or were browsed to via network neighborhood the path returned is always UNC. Local drives even become the correct hidden UNC (D$) with the computer name correctly prefixed. This means when it is later manipulated into an Excel spreadsheet that is then published to the web, all the hyperlinks for the file names are unbreakable regardless of different local machine maps! Viola! Instant web based vault via a browser for all SW objects, parts, assemblies, and drawings! If anyone is interested in this really simple web based viewing system (done in Excel, and any browser) that contains a plethora of data including Cost Roll-Up, Total Weight, Total Cost, NHA, Where Used, Discrete Part Count, Item Master List, and Assembly Hierachy, just drop me an e-mail.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required