Home All Groups Group Topic Archive Search About

Using shell command to map drive can't get redirect result to file

Author
19 Oct 2005 8:11 PM
Ralph Malph
I am using the shell command to run the old DOS “net use” function to map a
drive to a network share. (If I could find a VB6/API way to do it I would but
I can’t so I am using the DOS net use option. Any alternative option to the
net use would be greatly appreciated.)

I am setting the string variable “ShellPath” to the net use command string I
need to execute my drive mapping.

If there are no problems with the username, password, servername, etc then
the code works. If the username, or password etc are incorrect, the drive
will not be mapped. This is normal operation. The problem is that if for some
reason the drive does not map, then I need to know about it. I have not found
away to pass the net use error code out of the shell command to my VB6 code.
I have found that if I redirect the out put of my net use command to a .txt
result file I can open it and see the words “The command completed
successfully.” if all worked AOK. If it failed then I will get a file, but it
will be empty. (It would be nice if it would pass the error message, but it
does not.) I have tested the redirection/file creation in DOS and it works
AOK. However, when I try it in code with the shell command the file does not
get created and the drive does not get mapped. If the file exists then the
rest of my code will work to figure out if the net use command was
successful. The Shellpath variable that I use for my Shell command works fine
until I add in the redirection, then it fails to create a result file or to
map the drive. How can I fix this? I have included a snippet of my code for
review. If anyone can spot my problem please let me know. I have tried 2
different ways of using the Shell command, neither one works when I add in
the redirection.

‘********************** Code snippet **********************


‘******************** This “Shellpath variable will work **************

'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" &
ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text & " " &
TxtPassword.Text

‘******************** This “Shellpath variable will not work **************

'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" &
ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text & " " &
TxtPassword.Text & " > %windir%\mapout.txt"


‘************* Start of shell routine **********

Dim ProcID As Long, hProc As Long

‘********************** Tried two different ways of writing the “Shell” line
************

‘***************** Way 1 ******************

ProcID = Shell(ShellPath, vbHide)


‘**************** Way 2 *******************

ProcID = Shell(Environ("ComSpec") & " /c " & ShellPath, vbHide)


hProc = OpenProcess(SYNCHRONIZE, 0, ProcID)
Do While WaitForSingleObject(hProc, 0) = WAIT_TIMEOUT
    Sleep (1)
Loop
CloseHandle hProc


‘************* End of shell routine **********


‘**************** Code works from here if mapout.txt is created
*******************


Dim TestPath As String
Dim ExitStr As String
Dim TestStr As String

TestPath = WinDir & "\mapout.txt"

TestStr = "The command completed successfully.*"

‘*********** This command gives me a “File not Found” error when the Shell
command fails to create the file

‘*********** This command works fine if the file exists *******

Open TestPath For Input As #1

ExitStr = Input(LOF(1), #1)

Close #1

If ExitStr Like TestStr Then

PerformDeleteAction TestPath, Destination, CopResult

‘MsgBox (ExitStr & " Pass")

Else

'MsgBox (ExitStr & " Fail")

PerformDeleteAction TestPath, Destination, CopResult

MsgStr = "Mapping of drive to network share failed." & NL & "Please check
your username and password or" & NL & "Contact your instructor or computer
support department."

reply = MsgBox(MsgStr, vbExclamation, "Error")

TxtUserName.SetFocus

Exit Sub

End If


Thanks to all who can help,

Ralph Malph

Author
19 Oct 2005 8:20 PM
Juergen Thuemmler
> (If I could find a VB6/API way to do it I would but I can't so I am using
> the DOS net use option. Any alternative option to the net use would be
> greatly appreciated.)

Have a look at the
WNetCancelConnection2() and
WNetAddConnection2()
APIs.

Juergen.
Author
19 Oct 2005 8:29 PM
Someone
Sample code:

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


Show quote
"Juergen Thuemmler" <t***@removethisgmx.de> wrote in message
news:OVTsTqO1FHA.1256@TK2MSFTNGP09.phx.gbl...
>> (If I could find a VB6/API way to do it I would but I can't so I am using
>> the DOS net use option. Any alternative option to the net use would be
>> greatly appreciated.)
>
> Have a look at the
> WNetCancelConnection2() and
> WNetAddConnection2()
> APIs.
>
> Juergen.
>
>
Author
19 Oct 2005 8:39 PM
Ralph
Show quote
"Ralph Malph" <ralph.ma***@happydays.com> wrote in message
news:3DDAB9BA-E44E-4C5A-8B6B-5907018AD9B1@microsoft.com...
> I am using the shell command to run the old DOS "net use" function to map
a
> drive to a network share. (If I could find a VB6/API way to do it I would
but
> I can't so I am using the DOS net use option. Any alternative option to
the
> net use would be greatly appreciated.)
>
> I am setting the string variable "ShellPath" to the net use command string
I
> need to execute my drive mapping.
>
> If there are no problems with the username, password, servername, etc then
> the code works. If the username, or password etc are incorrect, the drive
> will not be mapped. This is normal operation. The problem is that if for
some
> reason the drive does not map, then I need to know about it. I have not
found
> away to pass the net use error code out of the shell command to my VB6
code.
> I have found that if I redirect the out put of my net use command to a
..txt
> result file I can open it and see the words "The command completed
> successfully." if all worked AOK. If it failed then I will get a file, but
it
> will be empty. (It would be nice if it would pass the error message, but
it
> does not.) I have tested the redirection/file creation in DOS and it works
> AOK. However, when I try it in code with the shell command the file does
not
> get created and the drive does not get mapped. If the file exists then the
> rest of my code will work to figure out if the net use command was
> successful. The Shellpath variable that I use for my Shell command works
fine
> until I add in the redirection, then it fails to create a result file or
to
> map the drive. How can I fix this? I have included a snippet of my code
for
> review. If anyone can spot my problem please let me know. I have tried 2
> different ways of using the Shell command, neither one works when I add in
> the redirection.
>
> '********************** Code snippet **********************
>
>
> '******************** This "Shellpath variable will work **************
>
> 'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" &
> ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text & " " &
> TxtPassword.Text
>
> '******************** This "Shellpath variable will not work
**************
>
> 'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" &
> ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text & " " &
> TxtPassword.Text & " > %windir%\mapout.txt"
>
>
> '************* Start of shell routine **********
>
> Dim ProcID As Long, hProc As Long
>
> '********************** Tried two different ways of writing the "Shell"
line
>  ************
>
> '***************** Way 1 ******************
>
> ProcID = Shell(ShellPath, vbHide)
>
>
> '**************** Way 2 *******************
>
> ProcID = Shell(Environ("ComSpec") & " /c " & ShellPath, vbHide)
>
>
> hProc = OpenProcess(SYNCHRONIZE, 0, ProcID)
> Do While WaitForSingleObject(hProc, 0) = WAIT_TIMEOUT
>     Sleep (1)
> Loop
> CloseHandle hProc
>
>
> '************* End of shell routine **********
>
>
> '**************** Code works from here if mapout.txt is created
> *******************
>
>
> Dim TestPath As String
> Dim ExitStr As String
> Dim TestStr As String
>
> TestPath = WinDir & "\mapout.txt"
>
> TestStr = "The command completed successfully.*"
>
> '*********** This command gives me a "File not Found" error when the Shell
> command fails to create the file
>
> '*********** This command works fine if the file exists *******
>
> Open TestPath For Input As #1
>
> ExitStr = Input(LOF(1), #1)
>
> Close #1
>
> If ExitStr Like TestStr Then
>
> PerformDeleteAction TestPath, Destination, CopResult
>
> 'MsgBox (ExitStr & " Pass")
>
> Else
>
> 'MsgBox (ExitStr & " Fail")
>
> PerformDeleteAction TestPath, Destination, CopResult
>
> MsgStr = "Mapping of drive to network share failed." & NL & "Please check

> your username and password or" & NL & "Contact your instructor or computer
> support department."
>
> reply = MsgBox(MsgStr, vbExclamation, "Error")
>
> TxtUserName.SetFocus
>
> Exit Sub
>
> End If
>
>
> Thanks to all who can help,
>
> Ralph Malph
>

To amplify on the other's suggestions - there is no simple way to retrieve
the output from a console application that is writing to its own console.
ie, net commands don't send output to stdout they write instead to their own
'screen buffer', thus can't be redirected by normal means.

It is not impossible, (what in programming really is?), but it will take far
more effort than using the API calls for this purpose. (Also appreciate that
net commands are using the same API)

-ralph
Author
19 Oct 2005 8:59 PM
Karl E. Peterson
Ralph wrote:
> To amplify on the other's suggestions - there is no simple way to
> retrieve the output from a console application that is writing to its
> own console. ie, net commands don't send output to stdout they write
> instead to their own 'screen buffer', thus can't be redirected by
> normal means.

Plagiarized with pride... <bg>

   Private Function ExecAndCapture(ByVal CmdLine As String, Optional ByVal
StartFolder As String = vbNullString) As String
      ' http://www.vb-helper.com/howto_capture_console_stdout.html
      Dim hPipeRead As Long
      Dim hPipeWrite As Long
      Dim sa As SECURITY_ATTRIBUTES
      Dim si As STARTUPINFO
      Dim pi As PROCESS_INFORMATION
      Dim Buffer() As Byte
      Dim StdOut As New CStringBuilder
      Dim lBytesRead As Long
      Dim ExitCode As Long
      Const BUFSIZE As Long = 1024 * 10

      With sa
         .nLength = Len(sa)
         .bInheritHandle = 1    ' get inheritable pipe handles
      End With 'SA

      If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
         Exit Function
      End If

      With si
         .cb = Len(si)
         .dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
         .wShowWindow = SW_HIDE
         .hStdOutput = hPipeWrite
         .hStdError = hPipeWrite
      End With 'SI

      If CreateProcess(vbNullString, CmdLine, ByVal 0&, ByVal 0&, 1, 0&, ByVal 0&,
StartFolder, si, pi) Then
         Call CloseHandle(hPipeWrite)
         Call CloseHandle(pi.hThread)

         ReDim Buffer(0 To BUFSIZE - 1) As Byte
         Do
            DoEvents
            If ReadFile(hPipeRead, Buffer(0), UBound(Buffer) + 1, lBytesRead, ByVal
0&) = 0 Then
               Exit Do
            End If
            Debug.Print Replace$(Left$(StrConv(Buffer, vbUnicode), lBytesRead), vbCr
& vbCrLf, vbCrLf);
            StdOut.Append Replace$(Left$(StrConv(Buffer, vbUnicode), lBytesRead),
vbCr & vbCrLf, vbCrLf)
         Loop
         Call CloseHandle(pi.hProcess)
      End If

      ' To make sure...
      Call CloseHandle(hPipeRead)
      Call CloseHandle(hPipeWrite)

      ' Return results.
      ExecAndCapture = StdOut.ToString
   End Function

> It is not impossible, (what in programming really is?), but it will
> take far more effort than using the API calls for this purpose.

Gotta agree, there.  <bg>
--
Working Without a .NET?
http://classicvb.org/petition
Author
19 Oct 2005 9:18 PM
Bob Butler
"Ralph Malph" <ralph.ma***@happydays.com> wrote in message
news:3DDAB9BA-E44E-4C5A-8B6B-5907018AD9B1@microsoft.com
> 'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" &
> ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text &
> " " & TxtPassword.Text & " > %windir%\mapout.txt"

ShellPath = environ$("comspec") & " /c net use " & MappedDrvLet & ": ""\\" &
ServerName & "\" & ShareName & """ /user:" & TxtUserName.Text &
" " & TxtPassword.Text & " > %windir%\mapout.txt"

but use the WNetXXXX api calls...

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
19 Oct 2005 9:29 PM
MikeD
Show quote
"Ralph Malph" <ralph.ma***@happydays.com> wrote in message
news:3DDAB9BA-E44E-4C5A-8B6B-5907018AD9B1@microsoft.com...
>I am using the shell command to run the old DOS “net use” function to
>map a
> drive to a network share. (If I could find a VB6/API way to do it I would
> but
> I can’t so I am using the DOS net use option. Any alternative option to
> the
> net use would be greatly appreciated.)
>
> I am setting the string variable “ShellPath” to the net use command
> string I
> need to execute my drive mapping.


OK.  This is off-topic I know. But can anyone tell my why when reading
"Ralph Malph's" post in Outlook Express 6, I see this:

“net use”

and this

can’t

(as even appears in the above quote)

BUT, when reading a reply (from "Ralph"), it's "corrected", as such:

> I am using the shell command to run the old DOS "net use" function to map
a
> drive to a network share. (If I could find a VB6/API way to do it I would
but
> I can't so I am using the DOS net use option. Any alternative option to
the
> net use would be greatly appreciated.)
>
> I am setting the string variable "ShellPath" to the net use command string
I
> need to execute my drive mapping.

I'm sure it's got to be some setting in OE (code page, language, encoding,
etc.), but I don't know what to do to get it to display correctly and NOT
cause most other posts to display garbage.

Thanks!

(and sorry for making an off-topic post)

--
Mike
Microsoft MVP Visual Basic
Author
19 Oct 2005 9:39 PM
Ralph
Show quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:e4SfsQP1FHA.3336@TK2MSFTNGP12.phx.gbl...
>
> "Ralph Malph" <ralph.ma***@happydays.com> wrote in message
> news:3DDAB9BA-E44E-4C5A-8B6B-5907018AD9B1@microsoft.com...
> >I am using the shell command to run the old DOS â?onet useâ? function to
> >map a
> > drive to a network share. (If I could find a VB6/API way to do it I
would
> > but
> > I canâ?Tt so I am using the DOS net use option. Any alternative option
to
> > the
> > net use would be greatly appreciated.)
> >
> > I am setting the string variable â?oShellPathâ? to the net use command
> > string I
> > need to execute my drive mapping.
>
>
> OK.  This is off-topic I know. But can anyone tell my why when reading
> "Ralph Malph's" post in Outlook Express 6, I see this:
>
> â?onet useâ?
>
> and this
>
> canâ?Tt
>
> (as even appears in the above quote)
>
> BUT, when reading a reply (from "Ralph"), it's "corrected", as such:
>
> > I am using the shell command to run the old DOS "net use" function to
map
> a
> > drive to a network share. (If I could find a VB6/API way to do it I
would
> but
> > I can't so I am using the DOS net use option. Any alternative option to
> the
> > net use would be greatly appreciated.)
> >
> > I am setting the string variable "ShellPath" to the net use command
string
> I
> > need to execute my drive mapping.
>
> I'm sure it's got to be some setting in OE (code page, language, encoding,
> etc.), but I don't know what to do to get it to display correctly and NOT
> cause most other posts to display garbage.
>
> Thanks!
>
> (and sorry for making an off-topic post)
>
> --
> Mike
> Microsoft MVP Visual Basic
>

Was Tuesday a late night?

<g>
-ralph
Author
19 Oct 2005 10:48 PM
MikeD
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:1YWdnXJqCq5xIcvenZ2dnUVZ_sydnZ2d@arkansas.net...
>

>
> Was Tuesday a late night?


Not really. And besides, by 5:30 (when I posted), I've always recovered.
<g>

--
Mike
Microsoft MVP Visual Basic
Author
20 Oct 2005 12:51 AM
Ralph
Show quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:u9hAu8P1FHA.1032@TK2MSFTNGP12.phx.gbl...
>
> "Ralph" <nt_consultin***@yahoo.com> wrote in message
> news:1YWdnXJqCq5xIcvenZ2dnUVZ_sydnZ2d@arkansas.net...
> >
>
> >
> > Was Tuesday a late night?
>
>
> Not really. And besides, by 5:30 (when I posted), I've always recovered.
> <g>
>
> --
> Mike
> Microsoft MVP Visual Basic
>

I think it might be more related to the newsserver / IP than to Outlook?

I have also seen the phenomena where gibberish will appear in a message in
my local news server and then when connecting to news.microsoft.com the
characters appear just fine.

But IE/Outlook is occasionally flaky at times, so it is tough to ever really
point a finger. <g>

-ralph
Author
20 Oct 2005 11:49 AM
Tony Spratt
Show quote
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:ieCdnRkIKcRudMveRVn-gQ@arkansas.net...
>
> "MikeD" <nob***@nowhere.edu> wrote in message
> news:u9hAu8P1FHA.1032@TK2MSFTNGP12.phx.gbl...
> >
> > "Ralph" <nt_consultin***@yahoo.com> wrote in message
> > news:1YWdnXJqCq5xIcvenZ2dnUVZ_sydnZ2d@arkansas.net...
> > >
> >
> > >
> > > Was Tuesday a late night?
> >
> >
> > Not really. And besides, by 5:30 (when I posted), I've always recovered.
> > <g>
> >
> > --
> > Mike
> > Microsoft MVP Visual Basic
> >
>
> I think it might be more related to the newsserver / IP than to Outlook?
>
> I have also seen the phenomena where gibberish will appear in a message in
> my local news server and then when connecting to news.microsoft.com the
> characters appear just fine.
>
> But IE/Outlook is occasionally flaky at times, so it is tough to ever
really
> point a finger. <g>
>
> -ralph

My guess is that Ralph Malph's original post used smart quotes, so the two
quote marks and the apostrophe were unreadable to Mike's newsreader first
time around (assuming it tried to translate to another character set).

They looked OK to me, but I get the funny chars in Mike's reply, so I guess
the substituted chars got "locked in".

This is probably why some newsgroups really scream at people who don't use
"plain text" for their posts... <g>
Author
20 Oct 2005 2:40 PM
MikeD
Show quote
"Tony Spratt" <tony_spr***@hotmail.com> wrote in message
news:uulbVxW1FHA.3560@TK2MSFTNGP15.phx.gbl...
>
> My guess is that Ralph Malph's original post used smart quotes, so the two
> quote marks and the apostrophe were unreadable to Mike's newsreader first
> time around (assuming it tried to translate to another character set).
>
> They looked OK to me, but I get the funny chars in Mike's reply, so I
> guess
> the substituted chars got "locked in".
>
> This is probably why some newsgroups really scream at people who don't use
> "plain text" for their posts... <g>


Ralph's Malph's post was in plain text.  Here's part of the message header:

Thread-Topic: Using shell command to map drive can't get redirect result to
file
thread-index: AcXU6Tqkmj37PNGqRmmQsLtCqYfAwA==
X-WBNR-Posting-Host: 66.194.104.5
From: "=?Utf-8?B?UmFscGggTWFscGg=?=" <ralph.ma***@happydays.com>
Subject: Using shell command to map drive can't get redirect result to file
Date: Wed, 19 Oct 2005 13:11:02 -0700
Lines: 123
Message-ID: <3DDAB9BA-E44E-4C5A-8B6B-5907018AD***@microsoft.com>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"

When I changed encoding to Unicode (UTF-8), it displayed fine.  Now I guess
I'll see how that affects other posts.

--
Mike
Microsoft MVP Visual Basic
Author
20 Oct 2005 3:26 PM
Tony Spratt
Show quote
> Ralph's Malph's post was in plain text.  Here's part of the message
header:
>
> Thread-Topic: Using shell command to map drive can't get redirect result
to
> file
> thread-index: AcXU6Tqkmj37PNGqRmmQsLtCqYfAwA==
> X-WBNR-Posting-Host: 66.194.104.5
> From: "=?Utf-8?B?UmFscGggTWFscGg=?=" <ralph.ma***@happydays.com>
> Subject: Using shell command to map drive can't get redirect result to
file
> Date: Wed, 19 Oct 2005 13:11:02 -0700
> Lines: 123
> Message-ID: <3DDAB9BA-E44E-4C5A-8B6B-5907018AD***@microsoft.com>
> MIME-Version: 1.0
> Content-Type: text/plain;
>  charset="Utf-8"
>
> When I changed encoding to Unicode (UTF-8), it displayed fine.  Now I
guess
> I'll see how that affects other posts.

What I meant by "plain text" was as in the "Tools/Options/Send/News Sending
Format" options - either HTML or plain text. Ralph's post is definitely
displaying here as HTML. All the first-level responses except Bob's are
showing as plain text (Bob's has retained the HTML formatting).

It looks like your settings tried to translate HTML back to unformatted and
got a weird result.

FWIW, I see the OP's quotes and apostrophe fine, but in HTML format.

Show quote
>
> --
> Mike
> Microsoft MVP Visual Basic
>
>
Author
20 Oct 2005 7:41 PM
Ralph Malph
Thanks to ALL who helped. Thank you, Thank you, Thank you !!!!!!!!

The WNet API calls were EXACTLY what I was looking for !!!!! :-)

THANKS !!!!!!

The drive mapping part of my code is now working perfectly. THANKS !!!!!

I have 1 or 2 more hurdles to get over and my app will be done. I will post
these later. (Hint: My program runs a program on the mapped drive that starts
another program, (3rd party can't change that) need ability to know if "other
program" fails to start, and something better than the Shell cmd to start the
"Start up" program.)

Again THANKS TO ALL WHO HELPED !!!!!

VB MVPs RULE !!!!!

Thank you Juergen
Thank you Someone
Thank you Ralph (Like the name)
Thank you Karl
Thank you Bob
Thank you MikeD (I too have seen strange characters reading some posts here
and other groups and VB help sites when using the WEB instead of Agent etc.)

Thanks to one and all !!!!


Ralph Malph

PS... NO VB.NET....Microsoft, are you listening ??????

AddThis Social Bookmark Button