2

I'm trying to compile some unsafe code from an application using Codedom, but everytime I get an error saying I must use "/unsafe." I've googled the issue and added:

Parameters.CompilerOptions = "/unsafe";

To my codedom code. Are there any simple solutions for this?

Edit: if it wasn't already clear, my solution didn't work.

Edit: Here is the class.

        public static bool Compile(string EXE_Name, string Source)
    {
        var Compiler = new CSharpCodeProvider();
        var Parameters = new CompilerParameters
        {
            CompilerOptions = "/unsafe"
        };
        CompilerResults cResults = default(CompilerResults);

        Parameters.GenerateExecutable = true;
        Parameters.OutputAssembly = EXE_Name;
        Parameters.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);
        Parameters.ReferencedAssemblies.Add("System.dll");
        Parameters.ReferencedAssemblies.Add("System.Core.dll");
        Parameters.ReferencedAssemblies.Add("System.Data.dll");
        Parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
        Parameters.ReferencedAssemblies.Add("System.Deployment.dll");
        Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        Parameters.ReferencedAssemblies.Add("System.Drawing.dll");
        Parameters.ReferencedAssemblies.Add("System.Xml.dll");

        Parameters.CompilerOptions = " /target:winexe";
        Parameters.TreatWarningsAsErrors = false;

        cResults = Compiler.CompileAssemblyFromSource(Parameters, Source);

        if (cResults.Errors.Count > 0)
        {
            foreach (CompilerError CompilerError_loopVariable in cResults.Errors)
            {
                CompilerError error = CompilerError_loopVariable;
                MessageBox.Show("Error: " + error.ErrorText, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return false;
        }
        else if (cResults.Errors.Count == 0)
        {
            return true;
        }
        return true;
    }
2
  • In what way didn't your solution work? You've clearly got more information - please share it with us. Commented Nov 15, 2012 at 6:44
  • I simply got a compiler error saying: "Error: Unsafe code may only appear if compiling with /unsafe." Commented Nov 15, 2012 at 6:51

2 Answers 2

3

It works for me - perhaps you weren't setting the parameters correctly?

using System.CodeDom.Compiler;
using Microsoft.CSharp;

class Test
{
    public static void Main(string[] args)
    {
        var compiler = new CSharpCodeProvider();
        var parameters = new CompilerParameters { 
            CompilerOptions = "/unsafe"
        };
        var source = "unsafe struct Foo {}";
        var result = compiler.CompileAssemblyFromSource(parameters, source);
        // No errors are shown with the above options set
        foreach (var error in result.Errors)
        {
            Console.WriteLine(error);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the input, but my code seems pretty much the same. I tried a few tweaks and I still am getting the same error.
@user1742916: What happens when you run the exact code I've given? If that fails on your box, then maybe it's a version issue. If it doesn't fail, then clearly "pretty much the same" isn't close enough... but without seeing your code, I can't help you.
Sorry Jon. I made a very dumb mistake in my code as most of you can probably see. I changed the CompilerOptions later to make the output an exe and didn't use +=.
0
var Parameters = new CompilerParameters
    {
        CompilerOptions = "/unsafe"
    };

and later:

Parameters.CompilerOptions = " /target:winexe";

You just replace "/unsafe" with " /target:winexe". Use:

Parameters.CompilerOptions += " /target:winexe";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.