Avatar of dentab
dentab
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Permissions & VB6

I have a program in Visual Basic 6 that will be run by limited users on an XP system.

I have portions of the program that I need to run as an administrator - I do not have a problem with building the credentials into the program.

Can anyone help with either running a portion of code or subroutine with elevated permissions, or accessing the hard-drive with them?

I am aware of RUNAS and would rather avoid launching a secondary application or running itself again for this purpose.
Visual Basic Classic

Avatar of undefined
Last Comment
dentab

8/22/2022 - Mon
greenhacks

See this code, which you can add in your vb6 code, and then internally run task as administrator user.

http://www.andreavb.com/forum/viewtopic_4543.html
kyodai

In VB6 you will not have much options to avoid use of RUNAS. It depends a bit on what you want to do, but keep in mind that if you want to use admin rights there is only 2 options - running your VB6 app as an admin or using RUNAS. Very few tasks give you the option to supply different credentials, a handfull of 3rd party applications allow elevated rights by command line - but i dont think you will get around RUNAS here.
dentab

ASKER
@greenhacks PLEASE READ the question properly

"I am aware of RUNAS and would rather avoid launching a secondary application or running itself again for this purpose."

I want to run a portion of code, I want to avoid launching a second application or second instance of my own.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
dentab

ASKER
@kyodai

I am sure there is a way to run a procedure as an elevated user but I cannot for the life of me find out how.  Elevating my currently running process would suffice.  Infact I am sure it is possible as it can be done in .net...

Anything that can be done in .net  2 or below can be done natively... its just knowing how.
nffvrxqgrcfqvvc

What is the 'Task' you're doing that needs the administrative token to run?
dentab

ASKER
Hi egl1044,

Well there have been a few times this would be useful, but in this case it is for access to files for create and delete in a folder that the user running my application does not have permissions on usually.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
nffvrxqgrcfqvvc

Have you considered setting up your application to write files to Application Data() this is the intended location where files should be placed. For this reason even a limited user should have full access to this location. If that is something you can consider it would be the easiest solution before jumping into changing folder permissions.
nffvrxqgrcfqvvc

I wrote a small example that will get this location automatically on the system. (Note:) This is shared amongs all users. If you need a per user basis then you just change the constant value to CSIDL_LOCAL_APPDATA.
Option Explicit
 
Private Declare Function SHGetSpecialFolderPathW Lib "shell32" (ByVal hWnd As Long, ByVal lpszPath As Long, ByVal nFolder As Long, ByVal fCreate As Long) As Boolean
Private Declare Function PathAppendW Lib "shlwapi" (ByVal pszPath As Long, ByVal pszMore As Long) As Long
 
Private Const CSIDL_COMMON_APPDATA = 35
Private Const CSIDL_LOCAL_APPDATA = 28
 
 
Private Function GetRawBuffer(ByVal lpBuffer As String) As String
  
  If LenB(lpBuffer) = 0 Then
    Exit Function ' _leave
  End If
  
  If InStr(lpBuffer, vbNullChar) Then
    
    GetRawBuffer = Left$(lpBuffer, InStr(lpBuffer, vbNullChar) - 1)
    
  Else
  
    GetRawBuffer = lpBuffer
    
  End If
  
End Function
 
 
Public Function AllUserAppData(Optional szAppend As String = vbNullString) As String
 
Dim Buffer(4096 - 1) As Byte
 
' Attempt to get the (application all user data) folder location.
If SHGetSpecialFolderPathW(0, VarPtr(Buffer(0)), CSIDL_COMMON_APPDATA, 0) Then
 
' Check that we want to append data to this location.
  If LenB(szAppend) <> 0 Then
    
    ' We want to append some data to this location.
    If PathAppendW(VarPtr(Buffer(0)), StrPtr(szAppend)) Then
  
      AllUserAppData = GetRawBuffer(Buffer)
      
    Else
    
      AllUserAppData = vbNullString
      
    End If
  
  Else
    
    ' We just want the location only.
    AllUserAppData = GetRawBuffer(Buffer)
    
  End If
 
Else
 
' The function failed.
MsgBox "SHGetSpecialFolderPathW failed. Error= " & Err.LastDllError
 
End If
 
' Free memory.
Erase Buffer
 
End Function
 
Private Sub Form_Load()
  
  
  MsgBox AllUserAppData("\AppName\yourfiles.txt")
  
  
End Sub

Open in new window

dentab

ASKER
Thank you but NO, I do ask the question I mean to ask.

I need the program to edit files that are in a particular folder.  Hence my question being as such, I do not need the user to be able to access their own user areas - I already have such code.

In this case to be specific, we have a problem with some users using a particular piece of software.  This software puts files in the Windows direcrtory - poor design I know but not my fault.  My program diagnoses the problem and can detect when the fix is simply to modify the files in the windows directory.  My program has to be run as an administrator to do this, but it requires some user-specific information to do this.

It would be possible to write a program that does the 1st part as the user, then launch itself again as an administrator passing is switches to tell it what to do, however that is not desireable.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
nffvrxqgrcfqvvc

You can't do this on a limited account I take it you can (READ) the file but attempting to (WRITE) is the part that fails. Your process must take ownership of the administrative token before it can change anything on a limited account. You don't have to use ShellExecute() or launch a seperate process. You can get the administrator token by using LogonUser and ImpersonateLoggedOnUser they will still need to supply account information. This will give your process the administrator token.
aikimark

nffvrxqgrcfqvvc

Hi aikimark,

Only for Vista :( I think dentab wants XP only.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dentab

ASKER
@akimark, thanks but egl is right its an XP issue.

egl, thanks I would like to elevate.  that would do, I have domain admin credentials so as the program would launch as the user in question it could do the user specific stuff, and remember the launching user in a variable if I need to refer to it.

How, with an admin username and password  would I use the admin token?

Thanks for this btw.
ASKER CERTIFIED SOLUTION
nffvrxqgrcfqvvc

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
aikimark

I've got an idea, but not idea if feasible or possible.  Since you have admin rights, you might create a service that will do the work that requires elevated privileges.  Your user application could send a windows message or atom message or use a named pipe to cause the service to do the desired work.
dentab

ASKER
Hi aikimark,
  good idea actually but not quite right in my situation - so thanks.

egl1044:
  Perfect that works and does exactly what I needed - I can now just switch between admin and standard user as needed.  thankyou - I would give you more points if only I could.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
dentab

ASKER
Awesome - thanks!!!
dentab

ASKER
UPDATE:
  Thanks again! I have now implemented the changes and it works exactly as I had hoped - 100% success.  Generally when I ask a problem on EE i have already googled it an puzzled it and the answers I get are irellevant or ones that I have already tried.  This was exactly what I needed. egl1044, I salute you.
dentab

ASKER
egl1044:

I have a follow-on question.  Basicly, how can I get the token for the current user?
If you can answer it I would be greatful - the question is at
https://www.experts-exchange.com/questions/24805816/Current-User-Token.html

thanks!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.