GetUNCName – Convert a file path to a UNC path

GetUNCName – Convert a file path to a UNC path

' Converts a reference to a file in the standard Windows' format (e.g. "H:ServerDirFilename.ext") in the corresponding UNC' format (e.g. "\ServerNameExportedDirServerDirFileName.txt")'' It turns to be very useful when a program running on a workstation' has to pass a file reference to another app running on another workstation' or when the file reference should be stored in a database for use from' every application on the network.' Declares for querying Windows versionConst VER_PLATFORM_WIN32s = 0               'Win32s on Windows 3.1Const VER_PLATFORM_WIN32_WINDOWS = 1        'Win32 on Windows 95Const VER_PLATFORM_WIN32_NT = 2             'Win32 on Windows NTType OSVERSIONINFO    dwOSVersionInfoSize As Long    dwMajorVersion As Long    dwMinorVersion As Long    dwBuildNumber As Long    dwPlatformId As Long    szCSDVersion As String * 128End TypePrivate Declare Function GetVersionEx Lib "Kernel32" Alias "GetVersionExA" _    (lpVersionInformation As OSVERSIONINFO) As Long' Declare for Registry functionsConst HKEY_CLASSES_ROOT = &H80000000Const HKEY_CURRENT_USER = &H80000001Const HKEY_LOCAL_MACHINE = &H80000002Const HKEY_USERS = &H80000003Const HKEY_PERFORMANCE_DATA = &H80000004Const HKEY_CURRENT_CONFIG = &H80000005Const HKEY_DYN_DATA = &H80000006Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _    LongPrivate Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _    ByVal samDesired As Long, phkResult As Long) As LongPrivate Declare Function RegQueryValue Lib "advapi32.dll" Alias _    "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, _    ByVal lpValue As String, lpcbValue As Long) As Long' Note that if you declare lpData as String, then it is necessary to pass it ' with ByValPrivate Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _    ByVal lpReserved As Long, lpType As Long, lpData As Any, _    lpcbData As Long) As LongPrivate Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" _    (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, _    ByVal cbName As Long) As LongPrivate Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" _    (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, _    lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, _    ByVal lpData As String, lpcbData As Long) As LongPrivate Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _    (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPrivate Declare Function GetComputerName Lib "Kernel32" Alias _    "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As LongPrivate Declare Function WNetGetConnection Lib "mpr.dll" Alias _    "WNetGetConnectionA" (ByVal lpszLocalName As String, _    ByVal lpszRemoteName As String, cbRemoteName As Long) As Long' This is the main function of the groupPublic Function GetUNCName(pathName As String) As String    Dim os As OSVERSIONINFO        ' determine if we're running under Windows 9x or NT    os.dwOSVersionInfoSize = Len(os)    GetVersionEx os        If (os.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) Then        ' runnning under Windows 9x        GetUNCName = GetUNCName95(pathName)    ElseIf (os.dwPlatformId = VER_PLATFORM_WIN32_NT) Then        ' running under Windows NT        GetUNCName = GetUNCNameNT(pathName)    End If        End Function' Private function that does the work under Windows 95Private Function GetUNCName95(pathName As String) As String    Dim hKey As Long    Dim hKey2 As Long    Dim exitFlag As Boolean    Dim i As Double    Dim ErrCode As Long    Dim rootKey As String    Dim key As String    Dim computerName As String    Dim lComputerName As Long        ' First of all, verify whether the disk is networked    If Mid(pathName, 2, 1) = ":" Then        Dim UNCName As String        Dim lenUNC As Long                UNCName = String$(260, 0)        lenUNC = 260            ErrCode = WNetGetConnection(Left(pathName, 2), UNCName, lenUNC)        If ErrCode = 0 Then            UNCName = Trim(Left$(UNCName, InStr(UNCName, vbNullChar) - 1))            GetUNCName95 = UNCName & Mid(pathName, 3)            Exit Function        End If    End If        ' else, scan the registry looking for shared resources (Win9x version)    computerName = String$(255, 0)    lComputerName = Len(computerName)    ErrCode = GetComputerName(computerName, lComputerName)    If ErrCode <> 1 Then        GetUNCName95 = pathName        Exit Function    End If        computerName = Trim(Left$(computerName, InStr(computerName, _        vbNullChar) - 1))    rootKey = "SOFTWAREMicrosoftWindowsCurrentVersionNetworkLanman"    ErrCode = RegOpenKey(HKEY_LOCAL_MACHINE, rootKey, hKey)    If ErrCode <> 0 Then        GetUNCName95 = pathName        Exit Function    End If        i = 0    Do Until exitFlag        Dim szValue As String        Dim szValueName As String        Dim cchValueName As Long        Dim szResourceName As String        Dim cchResourceName As Long        Dim dwValueType As Long        Dim dwValueSize As Long        Dim exitw As Boolean        Dim Path As String        Dim j As Double                szResourceName = String(1024, 0)        cchResourceName = Len(szResourceName)                ' loop on all shared resources        ErrCode = RegEnumKey(hKey, i, szResourceName, cchResourceName)                                                   If ErrCode <> 0 Then            exitFlag = True        Else            ' for each shared resource, read the value looking for PATH            szResourceName = Trim(Left$(szResourceName, InStr(szResourceName, _                vbNullChar) - 1))            key = rootKey & "" & szResourceName            RegOpenKey HKEY_LOCAL_MACHINE, key, hKey2                        j = 0            Do Until exitw                szValue = String$(260, 0)                dwValueSize = Len(szValue)                szValueName = String(1024, 0)                cchValueName = Len(szValueName)                        ErrCode = RegEnumValue(hKey2, j, szValueName, cchValueName, 0, _                    dwValueType, szValue, dwValueSize)                If ErrCode <> 0 Then                    exitw = True                Else                    szValueName = Trim(Left$(szValueName, InStr(szValueName, _                        vbNullChar) - 1))                    If UCase(szValueName) = "PATH" Then                        ' we found the path the corresponds to the shared                         ' resource                        Path = Trim(Left$(szValue, InStr(szValue, _                            vbNullChar) - 1))                        If UCase(Path) = UCase(Left(pathName, Len(Path))) Then                            GetUNCName95 = "\" & computerName & "" & _                                szResourceName & Mid$(pathName, Len(Path))                            exitFlag = True                        End If                        exitw = True                    End If                End If                j = j + 1            Loop            exitw = False            RegCloseKey hKey2        End If        i = i + 1    Loop        RegCloseKey hKey        If GetUNCName95 = "" Then GetUNCName95 = pathName    End Function' Private function that does the work under Windows NTPrivate Function GetUNCNameNT(pathName As String) As String    Dim hKey As Long    Dim hKey2 As Long    Dim exitFlag As Boolean    Dim i As Double    Dim ErrCode As Long    Dim rootKey As String    Dim key As String    Dim computerName As String    Dim lComputerName As Long    Dim stPath As String    Dim firstLoop As Boolean    Dim ret As Boolean    ' first, verify whether the disk is connected to the network    If Mid(pathName, 2, 1) = ":" Then        Dim UNCName As String        Dim lenUNC As Long                UNCName = String$(520, 0)        lenUNC = 520        ErrCode = WNetGetConnection(Left(pathName, 2), UNCName, lenUNC)        If ErrCode = 0 Then            UNCName = Trim(Left$(UNCName, InStr(UNCName, vbNullChar) - 1))            GetUNCNameNT = UNCName & Mid(pathName, 3)            Exit Function        End If    End If        ' else, scan the registry looking for shared resources (NT version)    computerName = String$(255, 0)    lComputerName = Len(computerName)    ErrCode = GetComputerName(computerName, lComputerName)    If ErrCode <> 1 Then        GetUNCNameNT = pathName        Exit Function    End If        computerName = Trim(Left$(computerName, InStr(computerName, _        vbNullChar) - 1))    rootKey = "SYSTEMCurrentControlSetServicesLanmanServerShares"    ErrCode = RegOpenKey(HKEY_LOCAL_MACHINE, rootKey, hKey)        If ErrCode <> 0 Then        GetUNCNameNT = pathName        Exit Function    End If        firstLoop = True        Do Until exitFlag        Dim szValue As String        Dim szValueName As String        Dim cchValueName As Long        Dim dwValueType As Long        Dim dwValueSize As Long                        szValueName = String(1024, 0)        cchValueName = Len(szValueName)        szValue = String$(500, 0)        dwValueSize = Len(szValue)                ' loop on "i" to access all shared DLLs        ' szValueName will receive the key that identifies an element        ErrCode = RegEnumValue(hKey, i#, szValueName, cchValueName, 0, _            dwValueType, szValue, dwValueSize)                If ErrCode <> 0 Then            If Not firstLoop Then                exitFlag = True            Else                i = -1                firstLoop = False            End If        Else            stPath = GetPath(szValue)            If firstLoop Then                ret = (UCase(stPath) = UCase(pathName))                stPath = ""            Else                ret = (UCase(stPath) = UCase(Left$(pathName, Len(stPath))))                stPath = Mid$(pathName, Len(stPath))            End If            If ret Then                exitFlag = True                szValueName = Left$(szValueName, cchValueName)                GetUNCNameNT = "\" & computerName & "" & szValueName & stPath            End If        End If        i = i + 1    Loop        RegCloseKey hKey    If GetUNCNameNT = "" Then GetUNCNameNT = pathNameEnd Function' support routinePrivate Function GetPath(st As String) As String    Dim pos1 As Long, pos2 As Long, pos3 As Long    Dim stPath As String    pos1 = InStr(st, "Path")    If pos1 > 0 Then        pos2 = InStr(pos1, st, vbNullChar)        stPath = Mid$(st, pos1, pos2 - pos1)        pos3 = InStr(stPath, "=")        If pos3 > 0 Then            stPath = Mid$(stPath, pos3 + 1)            GetPath = stPath        End If    End IfEnd Function

devx-admin

devx-admin

Share the Post:
Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW)

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional