使用itida保护时反序列化失败

时间:2012-09-29 10:13:39

标签: c# xml-deserialization copy-protection

正如标题所说,在使用以下例外保护我的应用程序后反序列化失败:

  

无法生成临时类(result = 1)。   错误CS0009:无法打开元数据文件'c:\ Path \ to \ protected.exe' - '尝试加载格式不正确的程序。 “

这是我用于反序列化的代码(它在exe不受保护时有效):

MyClass myClass;
try
{
    using (var stream = new MemoryStream(Data))
    {
        var serializer = new XmlSerializer(typeof(ComSec.MyClass));
        myClass = serializer.Deserialize(stream) as MyClass;
    }
}
catch (Exception e)
{
    return null;
}

奇怪的是代码+ itida保护在我的机器上工作正常但在VM和同事的机器上失败

我正在使用(与我的同事相同的配置):

  • VS2012专业版
  • Windows 7 x64 Ultimate
  • Themida 2.1.2.0 x86(使用.Net支持)

VM是全新安装的Windows 7 x86。

2 个答案:

答案 0 :(得分:4)

我最终使用DataContract属性并使用DataContractSerializer来序列化和反序列化对象(它现在可以在任何地方使用,有/无保护)。

我的研究:

[DataContract(Name = "TestClass")]
public class TestClass
{
    [DataMember(Name = "Name")]
    public string Name { get; set; }
    [DataMember(Name = "Age")]
    public int Age { get; set; }
}

序列化/反序列化:

var serializer = new DataContractSerializer(typeof(TestClass));

using (var stream = new MemoryStream())
{
    serializer.WriteObject(stream, this);
    File.WriteAllBytes("TestClass.xml", stream.ToArray());
}

TestClass o = null;
using (var stream = new MemoryStream(File.ReadAllBytes("TestClass.xml")))
{
    o = serializer.ReadObject(stream) as TestClass;
}

答案 1 :(得分:0)

我也遇到过这个问题,提出的答案也很好。原始问题是由于文件访问权限。只需添加此信息,以便其他人了解DataContract的工作原理(来自https://stackoverflow.com/a/10340155/1111380):

DataContractSerializer,NetDataContractSerializer和DataContractJsonSerializer不需要磁盘空间,也不会将程序集发送到磁盘。相反,它们在运行中(在内存中)生成IL并在后续序列化集中使用它来在它们正在运行的AppDomain中进行序列化和反序列化。但是,XmlSerializer确实需要磁盘空间并解释错误(文件的路径)无法打开/访问)。