Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB App to Batch file

Posted on 2005-04-25
22
Medium Priority
?
531 Views
Last Modified: 2008-01-09
Could I please get some help on a project I'm doing. I have this one last area to complete and I just can seem to get it. I have written a VB6.0 app that does a numbr of functions inside a WinPE environement and has function that will ask the user to enter: \\ServerName\Share, USerName and Password so they can map a network drive. I need to pass this information from the VB app to a batch file so the user can map the drive. I have DIM ServerName, UserName and Password and I know these work. How do I place the information collected from the Inputboxes in VB6.0 to a Batch file that will allow a drive to be mapped?

My batch file has the Net Use X: preset. What comes next?
0
Comment
Question by:RBrown1375
  • 8
  • 5
  • 4
  • +2
22 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13859484
Something like this perhaps:


@echo off
if [%~3]==[] (
   set errorlevel=-1
   exit /b
)
set drive=%~1
set server=%~2
set password=%~3
net use %drive% "%server%" "%password%"


Then to use that, you call it like so:

   BatchFileName "X:" "\\ServerName\Share" "password"


I hope that helps.
0
 

Author Comment

by:RBrown1375
ID: 13859942
I may have it wrong but it didn't work. I added the @echo off line all the way down to the net use command into the Batch file and it didn't work. I tried to call the batch file with the Shell "BatchfileName.bat" and the VB app won't pass the collected data from VB to batch. I'm close.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13859974
It won't pass the data? Can't you just do something like this?..

    Shell "BatchFileName.bat x: \\servername\share password"

?

..Be warned, I'm anti-VB  ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:RBrown1375
ID: 13860037
The VB app asks the Tech to enter:

A drive letter
Servername and share
Username
Password.

Once this data is collected, I need the VB app to do a net use command and insert the data the tech entered. Each of those are variables stored in the VB app. I just can get the net use command, to use the information collected.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13860276
If that's all that your VB app does, then can't you just do it all in a Batch file? Like so:


@echo off
title Drive Mapper
cls
echo.
:refresh
set drive=
set server=
set username=
set password=
set /p drive="Enter the drive letter: "
set /p server="Enter the servername and share: "
set /p username="Enter the username: "
set /p password="Enter the password: "

if /i "%drive%"=="" echo You must specify a drive& goto :refresh
if /i "%server%"=="" echo You must specify a servername and share& goto :refresh
if /i "%username%"=="" echo You must specify a username& goto :refresh
if /i "%password%"=="" echo You must specify a password& goto :refresh

set drive=%drive::=%
set drive=%drive:~0,1%
if /i "%server:~0,2%" neq "\\" set server=\\%server%

net use %drive% "%server%" "%password%"



?
0
 

Author Comment

by:RBrown1375
ID: 13860347
Sorry, My VB app runs ghost32, PowerQuest imagecenter32, PQideploy, sets single and dual drive systems, runs partition magic and so on. It has a menu that allows a tech to choose what they want to do. One of the fucntions is to install and image from a network drive with iether ghost or powerquest. I added some fields to the menu to allow a tech to collect the information from my last note and make a network connection without having to close the VB menu, just to do a "net use" function in WinPE and then have to restart the menu to perform other tasks. I want the user to be able to do everything from the VB menu. Everything is working fine except for taking the collected data from the VB menu inboxes and adding it to a batch file so the net use command will connect a drive based on what the user input into the VB menu.
0
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 13860698
Is there a reason you can't put the map drive code directly into the VB app - do you have to go to a batch file?
0
 

Author Comment

by:RBrown1375
ID: 13860739
The mapped drive should be a variable but I guess it doesn't matter. I'd like to use X: or Y: since  Z: is for my RAMDrive. I don't have to use a batch file. What would be the code in VB if I always used:

Net Use X: \\

How do I get the ServerName, UserName and Password to run and how can I return a "command completed succsefully " once the Net use command connects the drive?
0
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 13860820
Post back if these are not helpful

Here is a link

http://vbnet.mvps.org/index.html?code/network/netconnect.htm

Here is some simpler code

Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As
String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long

Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
"WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long,
ByVal fForce As Long) As Long

Public Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As String
    lpRemoteName As String
    lpComment As String
    lpProvider As String
End Type

Public Const RESOURCE_GLOBALNET As Long = &H2&
Public Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3&
Public Const RESOURCETYPE_DISK As Long = &H1&
Public Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
Public Const CONNECT_UPDATE_PROFILE = &H1

Public Sub MapDrive(LocalName As String, Remotename As String, Optional
Username As String, Optional Password As String)
    Dim NetR As NETRESOURCE
    With NetR
        .dwScope = RESOURCE_GLOBALNET
        .dwType = RESOURCETYPE_DISK
        .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
        .dwUsage = RESOURCEUSAGE_CONNECTABLE
        .lpLocalName = LocalName
        .lpRemoteName = Remotename
    End With
    WNetAddConnection2 NetR, Password, Username, CONNECT_UPDATE_PROFILE
End Sub

Public Sub DisconnectDrive(LocalName As String)
    WNetCancelConnection2 LocalName, CONNECT_UPDATE_PROFILE, False
End Sub
0
 
LVL 1

Expert Comment

by:schumakers
ID: 13861156

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "X:", "\\Server\Public,  [bUpdateProfile], [strUser], [strPassword])"

[bUpdateProfile]
Optional. Boolean value indicating whether the mapping information is stored in the current user's profile. If bUpdateProfile is supplied and has a value of true, the mapping is stored in the user profile (the default is false).

[strUser]
Optional. String value indicating the user name. You must supply this argument if you are mapping a network drive using the credentials of someone other than the current user.

[strPassword]
Optional. String value indicating the user password. You must supply this argument if you are mapping a network drive using the credentials of someone other than the current user.

For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthmapnetworkdrive.asp 

0
 
LVL 1

Expert Comment

by:schumakers
ID: 13861164
Never mind my post.  I was thinking VBscript. not VB6
0
 

Author Comment

by:RBrown1375
ID: 13861228
Here is the code I'm using:

Dim ComputerName, User, Password, Prompt1, Prompt2, Prompt3


Private Sub LBLPasswordCommand2_Click()
Prompt1 = "Enter User Password."
Password = InputBox(Prompt1, "Enter password")

If Password = "" Then
     MsgBox "User cancelled function.", 48, "Exit"
     
End If
End Sub

Private Sub LBLServerCommand1_Click()
Prompt2 = "Enter Server and Share Name."
ComputerName = InputBox(Prompt2, "Enter Server and Share Name.")


If ComputerName = "" Then
     MsgBox "User Cancelled Function.", 48, "Exit"
     
End If
End Sub

Private Sub LBLUserIDCommand1_Click()
Prompt3 = "Enter User Logon Name."
User = InputBox(Prompt3, "Enter User ID")


If User = "" Then
     MsgBox "User Cancelled Function.", 48, "Exit"
     
End If
End Sub

Private Sub NetworkCommand1_Click()
If NetworkCommand1.Value = True Then

What I need goes here. How do I get the variables from above to run with the Net Use X: \\ command?
I would also like for the window to stay open so the user can see if the command completed correctly.



End If
End Sub



Thanks for the ideas
0
 
LVL 1

Expert Comment

by:dumbo2569
ID: 13861355
Here's an easier solution for you.
Since batch files are plain text, have your VB app create the batch file dynamically.

Use FSO to create/overwrite a file named "MapDrive.bat", then write to that file the batch commands.
"net use X: \\servername\sharename /user:username password"

Then from your VB app, shell out to this batch file, and delete the file if you wish.  This seems to be a simpler solution than creating a batch to accept parameters or whatnot. I, personally, like to stick to the KISS methodology.

If you need help writing the app, let me know.
0
 

Author Comment

by:RBrown1375
ID: 13861374
I do I have never heard of that.
0
 
LVL 1

Expert Comment

by:schumakers
ID: 13861426
This is similar code I use in one of my programs.  Just put the lines of the batch file in the writelines.

Sub CreateBatchFile()
      Dim fs, a, strPath
      
      strPath = "pathandfilename.bat"
      
      Set fs = CreateObject("Scripting.FileSystemObject")
      Set a = fs.CreateTextFile(strPath, True, False)
      a.writeline "Line 1 text"
      a.writeline "Line 2 text"
      a.writeline "Line 3 text"
      a.writeline "Line 4 text"
      a.writeline "Line 5 text"
      
      a.close
      
      Set a = Nothing
      Set fs = Nothing
End Sub
0
 

Author Comment

by:RBrown1375
ID: 13861445
I'll try and let you  know thanks
0
 
LVL 1

Accepted Solution

by:
dumbo2569 earned 2000 total points
ID: 13861451
OK. Scratch the batch file all together. Well if you want to create it, it's up to you.

But, look at http://support.microsoft.com/kb/q129796. (where I got a good chunk of this code). Shows you how to shell out from a VB app.

Private 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

   Private Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessID As Long
      dwThreadID As Long
   End Type

   Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
      hHandle As Long, ByVal dwMilliseconds As Long) As Long

   Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      lpApplicationName As String, 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

   Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long

   Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

   Private Const NORMAL_PRIORITY_CLASS = &H20&
   Private Const INFINITE = -1&

   Public Function ExecCmd(cmdline$)
      Dim proc As PROCESS_INFORMATION
      Dim start As STARTUPINFO

      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)

      ' Start the shelled application:
      ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)

      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
   End Function

Function MapDrive(strDriveLetter as string, strServerShare as string, strUserName as string, strPassword as string)
      Dim retval As Long
      Dim strCommand as string
      strCommand = "net use " & strDriveLetter & ": " & strServerShare & " /user:"&strUserName & " " & strPassword
      retval = ExecCmd(strCommand)
      MapDrive = retval
End Function
0
 
LVL 1

Expert Comment

by:dumbo2569
ID: 13861459
Sorry schumakers, I posted my previous entry before reading your entry. I didn't mean to be offensive to your solution.
0
 
LVL 1

Expert Comment

by:schumakers
ID: 13861488
RBrown1375
 
I would do it programatically as they have shown you.  If you want a less complex way of doing it then creating a batch file also works.

dumbo2569
No offense taken.  Just saw the post and new I had that code already made.  So, I posted it.
0
 
LVL 1

Expert Comment

by:dumbo2569
ID: 13861491
Oh yes, and BTW, the MapDrive function will return the same ERRORLEVEL as the "net use" command. That way you can track if the "net use" succeeded or not.
0 = successful, other errorlevel = error.
0
 

Author Comment

by:RBrown1375
ID: 13861838
Dumbo2569

Thank You. I didn't need all the code but the final area Function MaPdrive worked after I altered it to meet my needs.

Cheers and have a great day!!!!

Thanks to everyone who answered!
0
 
LVL 1

Expert Comment

by:dumbo2569
ID: 13861920
No Problem.

FYI: I'm not sure of the consequences of not including all that code since most of the code are supporting functions for ExecCmd(). Most of it was directly from microsoft. That's what I have in my program and it's been working for 2 years w/o a hitch. But it's up to you if you don't want to include it all.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Progress
Starting up a Project

571 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