2

I am developing a C# program to mask the user password. I am successful with that. I want the set the same user password as varible to the batch script( parent process ) from where the C# EXE is triggered. any help is greatly appreciated!!!

I also like to understand if there is any better approach to pass the value from C# to the parent process( *.bat file).

The idea here is ...

  1. Batch Script

  2. C# program ( mask the password / set the password as variable to the Batch script)

  3. Batch use that variable pass that as argument to the PLM tool .

------------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Diagnostics;

namespace ConsoleApplicationUtil
{
    class Program
    {
        static int Main(string[] args)
        {
            int PLM_RETURN = 0;
            string password = "";

            for (int iNx = 0; iNx < args.Length; iNx++)
            {
                Console.WriteLine("args[ {0} ]::< {1} >", iNx,args[iNx]);
            }
            Console.Write("Enter your password: ");
            ConsoleKeyInfo key;

            do
            {
                key = Console.ReadKey(true);

                // Backspace Should Not Work
                if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
                {
                    password += key.KeyChar;
                    Console.Write("*");
                }
                else
                {
                    if (key.Key == ConsoleKey.Backspace && password.Length > 0)
                    {
                        password = password.Substring(0, (password.Length - 1));
                        Console.Write("\b \b");
                    }
                }
            }
            // Stops Receving Keys Once Enter is Pressed
            while (key.Key != ConsoleKey.Enter);

            Console.WriteLine();
            Console.WriteLine("The Password You entered is : " + password);
           // Environment.SetEnvironmentVariable(args[3], password, EnvironmentVariableTarget.User);

            int ParentPID = Process.GetProcessById(Process.GetCurrentProcess().Id).Parent().Id;

            foreach (Process proc in Process.GetProcesses())
            {
                Console.WriteLine("Checking Process: " + proc.ProcessName + ":" + proc.Id);
                //StringDictionary sd = proc.StartInfo.EnvironmentVariables;
                //if (proc.ProcessName.Equals("cmd"))
                if (proc.Id.Equals(ParentPID) && proc.ProcessName.Equals("cmd"))
                {
                    string s1 = proc.StartInfo.Arguments;
                    StringDictionary env_val = proc.StartInfo.EnvironmentVariables;                    
                    Console.WriteLine("================================================");
                    if (env_val.ContainsKey("PASS_APP"))
                    {
                        Console.WriteLine("FOUND IT!!!");
                        env_val.Add(args[3], password);                       
                        //proc.StartInfo.Arguments.IndexOf(password);                       
                    }
                    else
                    {
                        Console.WriteLine("NOOOOOOOOOOOOOO!!!");
                    }
                    Console.WriteLine("================================================");
                }

            } 

           return PLM_RETURN;
        }
    }
}
Community
  • 1
  • 1

2 Answers2

2

You can't modify the environment variables for another process (at least not without some sort of debugging privileges or something silly like that). Modifying the environment variables for another process requires modifying the environment block of that process, i.e. is essentially modifying the memory of that process.

The normal way of passing information back from a child process to its parent is to use the standard output (i.e. Console.WriteLine), although batch files make it a bit of a pain to assign that output to a variable so it can be used.

If you want to write a batch file that accepts a masked text input then you can use some sneaky VBScript magic.

A completely different approach to your problem would be to not bother trying to pass the password back to the batch file and instead just get the C# application to use the password itself (i.e. do whatever the batch file was going to do, possibly just by calling another batch file passing the password in as an argument).

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
0

I suggest that if you want to manipulate the variables produced by the console application, why not manipulate them in the console application itself, instead of going through all the hassle to return the variables back to the caller?

AFAIK the only simple way to "return" a variable is to simply return an integer at the end of the entry point, for example,

static int Main(string[] args)
{
    return 912;
}

The ERRORLEVEL variable will be set to the value that you return. it can be accessed as follows

test.exe
echo %errorlevel%

Unfortunately you can't return a string this way, and to do that you will have to do what Justin suggested.

Yiyuan Lee
  • 659
  • 8
  • 23