Link to home
Start Free TrialLog in
Avatar of gaurav_inf
gaurav_inf

asked on

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

Hi,
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
           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
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gaurav_inf
gaurav_inf

ASKER

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

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

Many thanks,
G
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.
 
Regards,
G
What does your sDir and sCmd look like?

You're running this code as an exe or did you install VB on another computer?
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 LOGON_NETCREDENTIALS_ONLY = &H2&
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 CREATE_UNICODE_ENVIRONMENT = &H400&
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
 
 
Private Type PROCESS_INFORMATION
    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.dwFlags = STARTF_USESHOWWINDOW 'STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
 
    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

Hi,
Thanks for the reply. Please find below my declations of sDir and sCmd.
sDir --->
    If Directory = "" Then
        sDir = CurDir
    Else
        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.

Thanks,
G
So is UserRootFolder something like C:\Folder or is it just Folder?
UserRootFolder is something like E:\User\work
Hi,
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?

Rgds,
G
Hi,

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.

Thanks.
G
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