Unable to device as Administrator

I have a weird issue. I have created a program that maps a drive to a folder using DefineDosDevice.  The program works great as a standard user.  I see the folder with the appropriate drive letter.  Once I run the program as an administrator it won;t map the folder.  Here is the function that does the work.

Private Function CreateDevice(DeviceName As String, PathName As String) As String
    If DefineDosDevice(0, DeviceName, PathName) = 0 Then
        MsgBox "Unable to create device " & DeviceName, vbCritical
        CreateDevice = vbNullString
        CreateDevice = DeviceName
    End If
End Function

How can I make it work as a standard user and a Administrator.  Thanks.
Who is Participating?
nffvrxqgrcfqvvcConnect With a Mentor Commented:
You may want to use SetVolumeMountPoint() function instead of DefineDosDevice

Option Explicit

Private Declare Function SetVolumeMountPointW Lib "Kernel32.dll" (ByVal lpszVolumeMountPoint As Long, ByVal lpszVolumeName As Long) As Long
Private Declare Function GetVolumeNameForVolumeMountPointW Lib "Kernel32.dll" (ByVal lpszVolumeMountPoint As Long, ByVal lpszVolumeName As Long, ByVal cchBufferLength As Long) As Long

Private Function GetVolumeGuid(ByVal drive As String) As String
    Dim Buffer(128) As Byte
    If GetVolumeNameForVolumeMountPointW(StrPtr(drive), VarPtr(Buffer(0)), 128) Then
        GetVolumeGuid = Left$(Buffer, InStr(Buffer, vbNullChar) - 1)
        GetVolumeGuid = vbNullString
    End If
End Function

Private Sub Form_Load()
    Call SetVolumeMountPointW(StrPtr("c:\test\"), StrPtr(GetVolumeGuid("E:\")))
End Sub

Open in new window

what is an error?
print out what is err.number and err.description
elizarrjAuthor Commented:
No error is generated. It just does not map the drive when running the program in administor mode.  It does map a drive when run as non administrator.  I did verify that the folder is correct under both scenerios.
EDDYKTConnect With a Mentor Commented:
that's strange,
you may want to use WMI or net use to map the drive
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.