Results 1 to 4 of 4

Thread: Getting error while running batch file on a Comp connected through LAN

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Angry Getting error while running batch file on a Comp connected through LAN

    Hi,

    I am trying to access a folder located in a shared folder of another computer . I have read permissions to that folder. But once I am through with completion of my data, I want to wite that Data to that Read Folder.
    So through this code I am using some developer's login Id and password who has got Write permissions on that folder.

    I am using CreateProcessWithLogonW function for this purpose, but it is throwing return code --"0"
    Code:
    Res = CreateProcessWithLogonW(StrPtr(sUsername), StrPtr(sDomain), _
        StrPtr(spwd), LOGON_WITH_PROFILE, 0&, StrPtr(sCmd), 0&, ByVal 0&, _
        StrPtr(sDir), SInfo, PInfo)
    
    ****************************************************
    ''''In sCmd - I am passing the information to run a batch file which contains the list of parts to be copied and source and destination info.
    
    ****************************************************
    
            If Res <> 0 Then
             dwWait = WaitForInputIdle(PInfo.hProcess, INFINITE)
                 Debug.Print "user input ready..."
         ' do something when app ready for input
               Do
                    dwWait = MsgWaitForMultipleObjects(1, PInfo.hProcess, 0, INFINITE, QS_ALLINPUT)
                   DoEvents
              Loop Until dwWait = WAIT_OBJECT_0
                Debug.Print "process closed/terminated"
               ' do something when launched app terminates
               CloseHandle PInfo.hThread
               CloseHandle PInfo.hProcess
           Else
               MsgBox "CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation
           End If
    And Err.LastDllError throws a return code --"1058"

    The description of this error ---> The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.

    What could possibly be wrong in this code ?
    What kind of services do we need to start before execution or during execution ?

    Please help me with this problem.

    Thanks,
    G
    Last edited by Hack; Nov 7th, 2008 at 08:00 AM. Reason: Added Code Tags

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Re: Getting error while running batch file on a Comp connected through LAN

    Could anyone help me with this problem ?

    Thanks

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Getting error while running batch file on a Comp connected through LAN

    I would assume that the service is "Fast User Switching", or one of the other services it depends on (which you can see if you open "Fast User Switching"), such as "Terminal Services", or one of the ones those services depend on.

  4. #4
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Getting error while running batch file on a Comp connected through LAN

    Ok, first of all, I don't believe that it is dependent on Fast User Switching. We use CreateProcessWithLogonW in our code all the time without any problem and we do it without enabling fast switching or terminal services. You can also use this function without a domain. The one thing that I have found is that it does not work if you have the exe or batch file that you are launching on a non-windows share (like NetWare).

    Let me ask a few questions. First, are you in a domain? Second, is the share that you are trying to write to on a Windows server? And third, is the batch file located on your hard drive or a Windows share?

    If you answered yes to all 3 of the above, then try the code below. Create a new project and put this into a module. Forget about your code for now, first let's see if we can get this code to work. Just substitute your username, password, etc. Put a "pause" as the last line of your batch file and see if the batch file launches.

    One more thing, is it possible to put your copy commands (what you have in the batch file) right into your VB code? If so, I have code where you can switch to another user, run code, then switch back to the original user without launching an external program. If that code will work, let me know and I will post it next time. For now, here is the code we use for launching external programs as another user...

    Code:
    Option Explicit
    '********Begin declarations for RunAsUser********
    'Public Const SW_SHOWNOACTIVATE = 4
    Public Const STARTF_USESHOWWINDOW = &H1
    Public Const SW_HIDE = 0
    Private Const SW_SHOW = 5
    Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
    Private Const LOGON_WITH_PROFILE = &H1
    Private Const LOGON_NETCREDENTIALS_ONLY = &H2
    Private Const LOGON32_LOGON_INTERACTIVE = 2
    Private Const LOGON32_PROVIDER_DEFAULT = 0
    
    Private Type STARTUPINFO
        cb As Long
        lpReserved As Long ' !!! must be Long for Unicode string
        lpDesktop As Long  ' !!! must be Long for Unicode string
        lpTitle As Long    ' !!! must be Long for Unicode string
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type
    
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    
    '  LogonUser() requires that the caller has the following permission
    '  Permission                        Display Name
    '  --------------------------------------------------------------------
    '  SE_TCB_NAME                      Act as part of the operating system
    
    '  CreateProcessAsUser() requires that the caller has the following permissions
    '  Permission                        Display Name
    '  ---------------------------------------------------------------
    '  SE_ASSIGNPRIMARYTOKEN_NAME       Replace a process level token
    '  SE_INCREASE_QUOTA_NAME           Increase quotas
     
    Private Declare Function LogonUser Lib "advapi32.dll" Alias _
            "LogonUserA" _
            (ByVal lpszUsername As String, _
            ByVal lpszDomain As String, _
            ByVal lpszPassword As String, _
            ByVal dwLogonType As Long, _
            ByVal dwLogonProvider As Long, _
            phToken As Long) As Long
    
    Private Declare Function CreateProcessAsUser Lib "advapi32.dll" _
            Alias "CreateProcessAsUserA" _
            (ByVal hToken As Long, _
            ByVal lpApplicationName As Long, _
            ByVal lpCommandLine As String, _
            ByVal lpProcessAttributes As Long, _
            ByVal lpThreadAttributes As Long, _
            ByVal bInheritHandles As Long, _
            ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, _
            ByVal lpCurrentDirectory As String, _
            lpStartupInfo As STARTUPINFO, _
            lpProcessInformation As PROCESS_INFORMATION) As Long
    
    ' CreateProcessWithLogonW API is available only on Windows 2000 and later.
    Private Declare Function CreateProcessWithLogonW Lib "advapi32.dll" _
            (ByVal lpUsername As String, _
            ByVal lpDomain As String, _
            ByVal lpPassword As String, _
            ByVal dwLogonFlags As Long, _
            ByVal lpApplicationName As Long, _
            ByVal lpCommandLine As String, _
            ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, _
            ByVal lpCurrentDirectory As String, _
            ByRef lpStartupInfo As STARTUPINFO, _
            ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
         
    Private Declare Function CloseHandle Lib "Kernel32.dll" _
            (ByVal hObject As Long) As Long
                                
    Private Declare Function SetErrorMode Lib "Kernel32.dll" _
            (ByVal uMode As Long) As Long
           
    Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
    End Type
                                
    ' Version Checking APIs
    Private Declare Function GetVersionExA Lib "Kernel32.dll" _
        (lpVersionInformation As OSVERSIONINFO) As Integer
    
    Private Const VER_PLATFORM_WIN32_NT = &H2
    '********End declarations for RunAsUser********
    
    '********************************************************************
    '                   RunAsUser for Windows 2000 and Later
    '********************************************************************
    Public Function RunAsUser(ByVal UserName As String, _
            ByVal Password As String, _
            ByVal DomainName As String, _
            ByVal CommandLine As String, _
            ByVal CurrentDirectory As String) As Long
    
        Dim si As STARTUPINFO
        Dim pi As PROCESS_INFORMATION
       
        Dim wUser As String
        Dim wDomain As String
        Dim wPassword As String
        Dim wCommandLine As String
        Dim wCurrentDir As String
        Dim Result As Long
       
        si.cb = Len(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_SHOW
           
        wUser = StrConv(UserName + Chr$(0), vbUnicode)
        wDomain = StrConv(DomainName + Chr$(0), vbUnicode)
        wPassword = StrConv(Password + Chr$(0), vbUnicode)
        wCommandLine = StrConv(CommandLine + Chr$(0), vbUnicode)
        wCurrentDir = StrConv(CurrentDirectory + Chr$(0), vbUnicode)
       
        Result = CreateProcessWithLogonW(wUser, wDomain, wPassword, _
              LOGON_WITH_PROFILE, 0&, wCommandLine, _
              CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
        ' CreateProcessWithLogonW() does not
        If Result <> 0 Then
            CloseHandle pi.hThread
            CloseHandle pi.hProcess
            RunAsUser = 0
        Else
            RunAsUser = Err.LastDllError
            MsgBox "CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation
        End If
    End Function
    
    Sub Main()
        Call RunAsUser("YourUserName", "YourPassword", "YourDomain", "\\YourServer\YourPath\YourBatch.bat", "\\YourServer\YourPath")
    End Sub
    If you do not have a domain, use "." for the domain argument and make sure you included the quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width