Search code examples
c#mingwwindres

windres: The filename, directory name, or volume label syntax is incorrect


I'm using windows 10.

I'm trying to compile a c++ file within c# using MinGW(the MinGW folder is in the projects directory), but it won't compile a resource script (using windres).

Whenever I use windres in cmd it says: "C:/Users/username/AppData/Local/Temp/my.rc:1: unrecognized escape sequence". but still works. But when I run the exact same command through c# (by creating a process) it doesn't work at all and says: "The filename, directory name, or volume label syntax is incorrect.".

My code:

String tempDir = Path.GetTempPath();
String file = tempDir + "my.rc";

using (StreamWriter writer = new StreamWriter(file, false, Encoding.ASCII))
{
     if (!textIcon.Text.Equals(""))
         await writer.WriteLineAsync("25 ICON \"" + textIcon.Text + "\"");
     if (checkAdmin.Checked)
     {
         String manifest = tempDir + @"\manifest.xml";
         createManifest(manifest);
         await writer.WriteLineAsync("55 24 \"" + manifest + "\"");
     }
}

String args2 = "/c \"" + Path.Combine(gccLocation, "windres.exe") + "\" -o \"" + Path.Combine(tempDir, "my.o").Replace("\\", "/") + "\" \"" + file.Replace("\\", "/") + "\"";

//Debug
//args2 = "/k echo " + args2;

ProcessStartInfo psi2 = new ProcessStartInfo();
psi2.FileName = "CMD.exe";
psi2.Arguments = args2;
psi2.UseShellExecute = false;
psi2.CreateNoWindow = true;

//Debug
//psi2.CreateNoWindow = false;

Process windres = Process.Start(psi2);
windres.WaitForExit();

if(windres.ExitCode != 0)
{
    MessageBox.Show("Error: Could not create resource file (" + windres.ExitCode + ")");
}

Solution

  • Ended up using a batch file to run the command.

                    String args2 = "windres.exe -i \"" + Path.GetFullPath(file) + "\" -o \"" + Path.Combine(tempDir, "my.o") + "\"" ;
    
                    using (StreamWriter writer = new StreamWriter(tempDir + @"\my.bat", false, Encoding.ASCII))
                    {
                        await writer.WriteLineAsync("@echo off");
                        await writer.WriteLineAsync("cd " + Path.GetFullPath(gccLocation));
                        await writer.WriteLineAsync(args2);
                    }
    
                    //Debug
                    //args2 = "/k echo " + args2;
    
                    ProcessStartInfo psi2 = new ProcessStartInfo();
                    psi2.FileName = tempDir + @"\my.bat";
                    psi2.UseShellExecute = false;
                    psi2.CreateNoWindow = true;
    
                    //Debug
                    //psi2.CreateNoWindow = false;
    
                    Process windres = Process.Start(psi2);
                    windres.WaitForExit();