|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using shell command to map drive can't get redirect result to filedrive 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 > (If I could find a VB6/API way to do it I would but I can't so I am using Have a look at the> the DOS net use option. Any alternative option to the net use would be > greatly appreciated.) WNetCancelConnection2() and WNetAddConnection2() APIs. Juergen. 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. > >
Show quote
"Ralph Malph" <ralph.ma***@happydays.com> wrote in message To amplify on the other's suggestions - there is no simple way to retrievenews: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 > 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 Ralph wrote:
> To amplify on the other's suggestions - there is no simple way to Plagiarized with pride... <bg>> 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. 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 Gotta agree, there. <bg>> take far more effort than using the API calls for this purpose. "Ralph Malph" <ralph.ma***@happydays.com> wrote in message ShellPath = environ$("comspec") & " /c net use " & MappedDrvLet & ": ""\\" &news:3DDAB9BA-E44E-4C5A-8B6B-5907018AD9B1@microsoft.com > 'ShellPath = "net use " & MappedDrvLet & ": " & """" & "\" & "\" & > ServerName & "\" & ShareName & """" & " /user:" & TxtUserName.Text & > " " & TxtPassword.Text & " > %windir%\mapout.txt" 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..."
Show quote
"Ralph Malph" <ralph.ma***@happydays.com> wrote in message OK. This is off-topic I know. But can anyone tell my why when reading 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. "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 I'm sure it's got to be some setting in OE (code page, language, encoding, 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. 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
Show quote
"MikeD" <nob***@nowhere.edu> wrote in message Was Tuesday a late night?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 > <g> -ralph "Ralph" <nt_consultin***@yahoo.com> wrote in message Not really. And besides, by 5:30 (when I posted), I've always recovered. news:1YWdnXJqCq5xIcvenZ2dnUVZ_sydnZ2d@arkansas.net... > > > Was Tuesday a late night? <g> -- Mike Microsoft MVP Visual Basic
Show quote
"MikeD" <nob***@nowhere.edu> wrote in message I think it might be more related to the newsserver / IP than to Outlook?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 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
Show quote
"Ralph" <nt_consultin***@yahoo.com> wrote in message My guess is that Ralph Malph's original post used smart quotes, so the twonews: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 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>
Show quote
"Tony Spratt" <tony_spr***@hotmail.com> wrote in message Ralph's Malph's post was in plain text. Here's part of the message header: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> 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 fileDate: 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
Show quote
> Ralph's Malph's post was in plain text. Here's part of the message What I meant by "plain text" was as in the "Tools/Options/Send/News Sendingheader: > > 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. 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 > > 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 ??????
Other interesting topics
|
|||||||||||||||||||||||