Search code examples
c#c#-4.0dllcmdgoogle-workspace

Cannot call cmd.exe from any application when it is called via the MIM .dll


What I currently have is a MIM .dll extension for the metaverse that needs to call the command line to run some console commands. These console commands are for a web tool call GAM that will create G-Suite users. I have a test console app built-in .NET Framework 4.7.2 that works just fine. When running the code with my .dll it seems to process just fine, even returns a positive exit code of 0, but does not perform the function in cmd that is expected. The .dll is in .NET Framework 4.5. The code block is the same for both applications:

using (Process gamProcess = new Process())
            {
                gamProcess.StartInfo.UseShellExecute = false;
                gamProcess.StartInfo.RedirectStandardOutput = true;
                gamProcess.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
                gamProcess.StartInfo.CreateNoWindow = true;
                gamProcess.StartInfo.Arguments = "/c gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
                gamProcess.Start();
                gamProcess.WaitForExit();
            }

If I call this block of code directly from the .dll, it will act as if it performs the action but nothing happens. If I call this code from the standalone application, it works just fine. I have even attempted to just call this application from the .dll and pass arguments directly to it. This will work if I set the arguments in Visual Studio but it will not work if I call the application from the .dll. I'm at a loss here, I cannot figure out what is the difference between running from my extension and running from the application.

Another side note, I built an even more simplistic console application that just writes to a file. I was able to call this application from the .dll, and it works just fine. It is only the commands above that the .dll seems to not want to process correctly.


Solution

  • I managed to fix the above code. I did two things at the same time, so I'm not sure which was the key to this issue but it is working flawlessly now. I changed the /c in the code to a /C uppercase. The code now looks as follows:

    using (Process gamProcess = new Process())
            {
                gamProcess.StartInfo.UseShellExecute = false;
                gamProcess.StartInfo.RedirectStandardOutput = true;
                gamProcess.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
                gamProcess.StartInfo.CreateNoWindow = true;
                gamProcess.StartInfo.Arguments = "/C gam create user " + username + " firstname " + firstname + " lastname " + lastname + " password " + pass;
                gamProcess.Start();
                gamProcess.WaitForExit();
            }
    

    The second action I performed was a restart of the dev server I was working on. I'm not sure which worked, especially since I was running a lowercase /c in my console app, but the code is working now. Thank you all who viewed this question.