I'm writting a program with Skype API (Skype4COM) and decided to make a runtime compilation. Everything worked perfectly before I decided to use in my compilation Skype API's library. When I tried to do it, program told me: "Metadata file 'c:\Users\Алексей\Dropbox\Skype Bot\Skype Bot\bin\Debug\Skype4COM.dll' could not be opened -- 'Attempt was made to load a program with an incorrect format'". How I can fix it? Here is code:
public delegate object Do();
protected static string Compile(string str)
{
try
{
var csc = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v3.5" } });
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll", "System.dll", "SKYPE4COM.dll" }) { GenerateInMemory = true };
//parameters.LinkedResources.Add("Skype4COM.dll");
//parameters.EmbeddedResources.Add("SKYPE4COMLib.dll");
//parameters.ReferencedAssemblies.Add("");
CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console");
CompilerResults results = csc.CompileAssemblyFromSource(parameters, str);
if (results.Errors.Count >= 1) return results.Errors[0].ErrorText;
Type t = results.CompiledAssembly.GetType("Program");
MethodInfo m = t.GetMethod("Method");
Do DoMethod;
DoMethod = (Do)Delegate.CreateDelegate(typeof(Do), m);
return DoMethod().ToString();
}
catch (Exception e)
{
return e.Message;
}
}
First be sure your dll has been registered.
If you create a test project and you add a COM reference to Skype4COM.dll
then Visual Studio will create an interop assembly Interop.Skype4COM.dll
. You have to add a reference to that file (in the same way you added a reference to System.Core.dll
, for example).