Getting error while running a batch file on a Computer connected through LAN

Posted on 2008-11-07
Last Modified: 2013-11-10
I Posted the question recently, but was unable to present in a proper manner. So started another question with specific information.

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"

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.

Res = CreateProcessWithLogonW(StrPtr(sUsername), StrPtr(sDomain), _
    StrPtr(spwd), LOGON_WITH_PROFILE, 0&, StrPtr(sCmd), 0&, ByVal 0&, _
    StrPtr(sDir), SInfo, PInfo)

        If Res <> 0 Then
         dwWait = WaitForInputIdle(PInfo.hProcess, INFINITE)
             Debug.Print "user input ready..."
     ' do something when app ready for input
                dwWait = MsgWaitForMultipleObjects(1, PInfo.hProcess, 0, INFINITE, QS_ALLINPUT)
          Loop Until dwWait = WAIT_OBJECT_0
            Debug.Print "process closed/terminated"
           ' do something when launched app terminates
           CloseHandle PInfo.hThread
           CloseHandle PInfo.hProcess
           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.

Question by:gaurav_inf
    LVL 21

    Accepted Solution

    Is the service Secondary Logon enabled on the workstation?

    Author Comment

    Hi AmazingTech,
    Could  you please explain in detail ?
    Where can I check for this service and how to activate it ?

    Author Closing Comment

    Hi AmazingTech,

    That was a great help.
    The code is working fine now.

    Many thanks,

    Author Comment

    Hi ,
    Need help once again, with the same code.
    I tried using this code in different computer, but it is throwing an error.
    Err.LastDllError throws a return code --"267"
    The description of theis error code is --> The directory name is invalid.
    I have checked every directory, but unable to find any flaw.
    Please help me out with this problem.
    LVL 21

    Expert Comment

    What does your sDir and sCmd look like?

    You're running this code as an exe or did you install VB on another computer?
    LVL 21

    Expert Comment

    Here's my code I ended up using. It was a hard fought battle with some information on the internet not being exactly correct.

    I wanted to be able to run this code silently to the logged on user. Most code on the internet did not work even when I set the StartInfo.wShowWindow = SW_HIDE.

    In the end I think it was the Public Type STARTUPINFO that needed to be changed. Can't remember exactly now.
    Private Const INFINITE = &HFFFF
    Private Const LOGON_WITH_PROFILE = &H1&
    Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
    Private Const CREATE_NEW_CONSOLE = &H10&
    Private Const CREATE_NEW_PROCESS_GROUP = &H200&
    Private Const CREATE_SEPARATE_WOW_VDM = &H800&
    Private Const CREATE_SUSPENDED = &H4&
    Private Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000&
    Private Const BELOW_NORMAL_PRIORITY_CLASS = &H4000&
    Private Const HIGH_PRIORITY_CLASS = &H80&
    Private Const IDLE_PRIORITY_CLASS = &H40&
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const REALTIME_PRIORITY_CLASS = &H100&
    Private Const SW_SHOW = 5
    Private Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const SW_HIDE = 0
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    Public Type STARTUPINFO
            cb As Long
            lpReserved As String
            lpDesktop As String
            lpTitle As 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
          Public Declare Function OpenProcess Lib "Kernel32.dll" _
            (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
                ByVal dwProcId As Long) As Long
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" (ByVal lpUsername As Long, ByVal lpDomain As Long, ByVal lpPassword As Long, ByVal dwLogonFlags As Long, ByVal lpApplicationName As Long, ByVal lpCommandLine As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInfo As PROCESS_INFORMATION) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Sub Main()
        Dim lpUsername As String, lpDomain As String, lpPassword As String, lpApplicationName As String
        Dim lpCommandLine As String, lpCurrentDirectory As String
        Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
        lpUsername = "UserID"
        lpDomain = "DomainName"
        lpPassword = "Password"
        lpApplicationName = Environ("comspec")
        lpCommandLine = " /c \\Server\Share\batch.cmd " & Command()
        lpCurrentDirectory = "C:\Program Files\Softricity\SoftGrid for Windows Desktops"
        'lpCurrentDirectory = vbNullString 'use standard directory
        StartInfo.cb = LenB(StartInfo) 'initialize structure
        StartInfo.wShowWindow = SW_HIDE
        CreateProcessWithLogon StrPtr(lpUsername), StrPtr(lpDomain), StrPtr(lpPassword), LOGON_WITH_PROFILE, StrPtr(lpApplicationName), StrPtr(lpCommandLine), CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, ByVal 0&, StrPtr(lpCurrentDirectory), StartInfo, ProcessInfo
        CloseHandle ProcessInfo.hThread 'close the handle to the main thread, since we don't use it
        CloseHandle ProcessInfo.hProcess 'close the handle to the process, since we don't use it
    End Sub

    Open in new window


    Author Comment

    Thanks for the reply. Please find below my declations of sDir and sCmd.
    sDir --->
        If Directory = "" Then
            sDir = CurDir
            sDir = Directory
        End If

    sCmd --->
    sCmd = UserRootFolder & "\$$Copy-File$$.bat"
    And the Content of that batch file is -->
    copy "E:\work\data.doc"  "\\Remotemachine\RoA\data.doc"

    UserRootFolder is a folder in the local machine.

    LVL 21

    Expert Comment

    So is UserRootFolder something like C:\Folder or is it just Folder?

    Author Comment

    UserRootFolder is something like E:\User\work

    Author Comment

    What ideally should be  lpCurrentDirectory  or sDir (in my case) ?
    I have kept it as the Temp folder.
    Can that be a problem as the configurations of the different computers can be different?


    Author Comment


    I changed the location of sDir from default to some folder in E drive.
    And this is working fiine now.
    This was the problem of different configurations.

    LVL 21

    Expert Comment

    I try to keep the sDir as the same folder where the command I'm trying to run is in.  Temp folder is not consistent. User temp folder is located %UserProfile%\Local Settings\Temp and the system temp folder is %SystemRoot%\Temp

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    This article is in response to a question ( here at Experts Exchange. The Original Poster (OP) requires a utility that will accept a list of IP addresses …
    David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now