Solved

How to create a EXE to run command in Visual Studio 2012

Posted on 2013-06-13
28
397 Views
Last Modified: 2013-08-21
Greeting,

I have the following batch file which will run a program in Windows 7 and  I want to encrypt the MyUserName and Password. I know I can create a EXE file in VS2012 which will run the command and display message "Success" or "Failed" when it is done. Please provide detailed instruction.

cd %ProgramFiles(X86)%\xxxxxxx\bin
zac unr -u MyUserName -p MyPassword
pause
exit

Thanks in advance.
0
Comment
Question by:mrong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
  • 5
28 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244565
assume u have the batch file saved as file.bat, u can create simple console applciation using visual studio with the following code:
            Process proc = new Process();
            int timeout = 10000;
            proc.StartInfo.FileName = "file.bat";
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();
            proc.WaitForExit(timeout);

            string errorMessage = proc.StandardError.ReadToEnd();
            proc.WaitForExit();
            if (errorMessage == string.Empty)
            {
                Console.WriteLine("Success");
            }
            else
            {
                Console.WriteLine("Failed: " + errorMessage);
            }

Open in new window

the timeout is optional and currently set to 10 seconds but u can remove it if not required.
0
 

Author Comment

by:mrong
ID: 39244597
so this console application need to be put into the same folder as the batch file? If that is the case, the user can still open the batch file to see the username & password which is what I want to avoid.

Thanks.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244604
it doesn't have to be in the same folder, it can be anywhere in the file system as long as u put the correct bat.file location in your code.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 

Author Comment

by:mrong
ID: 39244615
my bat file will open a dos prompt to exe the command(shows the username and password). Will your suggestion still open the dos prompt?

Thanks.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244633
the code simply runs the batch file and output result, it doesn't interfere with its implementation (if that's what u asking).
0
 

Author Comment

by:mrong
ID: 39244640
Can I include the bat file in the application and compile into one package?
Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244651
Why do you need a whole new application just to print "succes" or "failed"? Can't you just do this in the batch file?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244653
what do u mean package?
like installer?
you actually create the batch file from the you console app and run it right after so the user won't have access to it.
0
 

Author Comment

by:mrong
ID: 39244665
Yes, the installer.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244684
so u need to create setup project using visual studio and add the exe of the console application to the installer package.
0
 

Author Comment

by:mrong
ID: 39244692
ok, I am creating the console application now. But gave me syntax error. Did I do anything wrong?

Module Module1

    Sub Main()
        Process proc = new Process();
            int timeout = 10000;
            proc.StartInfo.FileName = "file.bat";
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();
            proc.WaitForExit(timeout);

            string errorMessage = proc.StandardError.ReadToEnd();
            proc.WaitForExit();
            if (errorMessage == string.Empty)
            {
                Console.WriteLine("Success");
            }
        Else
            {
                Console.WriteLine("Failed: " + errorMessage);
            }
    End Sub

End Module
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244701
which line?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244710
So to expand, your creating 15+ lines of code to do what 2 can do?

e.g.

cd %ProgramFiles(X86)%\xxxxxxx\bin
zac unr -u MyUserName -p MyPassword

IF ERRORLEVEL = 0 ECHO Success
ELSE ECHO Failed

pause
exit

Open in new window


Is there any particular reason why?

P.S.

Curly braces aren't used in VB in that way (If/Then).
0
 

Author Comment

by:mrong
ID: 39244712
They are all over the place...

'Process' is a type and cannot be used as expression.

'proc' is not declared
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244719
You're mixing C# syntax with VB.NET syntax.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39244726
its C# and u need vb.net so here:
Dim proc As New Process()
Dim timeout As Integer = 10000
proc.StartInfo.FileName = "file.bat"
proc.StartInfo.RedirectStandardError = True
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.UseShellExecute = False

proc.Start()
proc.WaitForExit(timeout)

Dim errorMessage As String = proc.StandardError.ReadToEnd()
proc.WaitForExit()
If errorMessage = String.Empty Then
	Console.WriteLine("Success")
Else
	Console.WriteLine("Failed: " & errorMessage)
End If

Open in new window

0
 

Author Comment

by:mrong
ID: 39244734
kaufmed,

My batch file run a program with supplied username and password in dos prompt. Here is what I need: I don't want the username and password show in the dos prompt, neither anyone can open the batch to see the username and password. I want to encrypt the username and password.

I thought I can create EXE to have everything embeded.

Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244738
You do realize that unless you obfuscate your code and encrypt any strings, anyone can download a decompiler and open up your newly-created .exe and see your code, yes?

Example
0
 

Author Comment

by:mrong
ID: 39244772
kaufmed,

oh....that's right:(

How hard is it to obfuscate the code and encrypt any strings? lots of work?

Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244800
I haven't used any of the obfuscators, so I'll have to defer to the others. I know that a "community edition" of DotFuscator comes with VS (not Express edition, I believe).

As far as encryption, who are you trying to keep out? Average Joe, or Joe Hacker? You've got an issue that you are deploying this code to a system that you have no control over. If you embed the encryption key into the program, you've got the same issue with security. Joe Hacker can get at your strings, but Average Joe probably won't know what you're doing. You could probably just use an XOR "encryption" to prevent Average Joe from seeing the strings.
0
 

Author Comment

by:mrong
ID: 39244805
I tried to complie the app but got an error can't find file.

    proc.StartInfo.FileName = "c:\file.bat";
0
 

Author Comment

by:mrong
ID: 39244814
kaufmed,

This is for internal use. I guess as long as put into a EXE should be fine.
Thanks.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244815
did u create the batch file as file.bat and saved under c:?
0
 

Author Comment

by:mrong
ID: 39244868
Ok, I keep the timeout to 10 sec. When I run the app, the dos window just stay on my screen forever .....

thanks.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244880
add this to the code:
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Open in new window

0
 

Author Comment

by:mrong
ID: 39244902
I changed to the following but the dos window still stay on my screen.

Sub Main()
        Dim proc As New Process()
        Dim timeout As Integer = 10000
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        proc.StartInfo.FileName = "c:/MyFile.bat"
        proc.StartInfo.RedirectStandardError = True
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.UseShellExecute = False

       

        proc.Start()
        proc.WaitForExit(timeout)

        Dim errorMessage As String = proc.StandardError.ReadToEnd()
        proc.WaitForExit()
        If errorMessage = String.Empty Then
            Console.WriteLine("Success")
        Else
            Console.WriteLine("Failed: " & errorMessage)
        End If
    End Sub
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39244922
did u try without the timeout?
0
 

Author Comment

by:mrong
ID: 39244940
Yes, same thing.
thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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