3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: BadImageFormatException
11 ** Purpose: Exception to an invalid dll or executable format.
14 ===========================================================*/
15 // <OWNER>Microsoft</OWNER>
20 using System.Runtime.Serialization;
21 using FileLoadException = System.IO.FileLoadException;
22 using System.Security.Permissions;
23 using SecurityException = System.Security.SecurityException;
24 using System.Globalization;
26 [System.Runtime.InteropServices.ComVisible(true)]
28 public class BadImageFormatException : SystemException {
30 private String _fileName; // The name of the corrupt PE file.
31 private String _fusionLog; // fusion log (when applicable)
33 public BadImageFormatException()
34 : base(Environment.GetResourceString("Arg_BadImageFormatException")) {
35 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
38 public BadImageFormatException(String message)
40 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
43 public BadImageFormatException(String message, Exception inner)
44 : base(message, inner) {
45 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
48 public BadImageFormatException(String message, String fileName) : base(message)
50 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
54 public BadImageFormatException(String message, String fileName, Exception inner)
55 : base(message, inner) {
56 SetErrorCode(__HResults.COR_E_BADIMAGEFORMAT);
60 public override String Message
68 private void SetMessageField()
70 if (_message == null) {
71 if ((_fileName == null) &&
72 (HResult == System.__HResults.COR_E_EXCEPTION))
73 _message = Environment.GetResourceString("Arg_BadImageFormatException");
76 _message = FileLoadException.FormatFileLoadExceptionMessage(_fileName, HResult);
80 public String FileName {
81 get { return _fileName; }
84 public override String ToString()
86 String s = GetType().FullName + ": " + Message;
88 if (_fileName != null && _fileName.Length != 0)
89 s += Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", _fileName);
91 if (InnerException != null)
92 s = s + " ---> " + InnerException.ToString();
94 if (StackTrace != null)
95 s += Environment.NewLine + StackTrace;
103 s+=Environment.NewLine;
104 s+=Environment.NewLine;
108 catch(SecurityException)
116 protected BadImageFormatException(SerializationInfo info, StreamingContext context) : base(info, context) {
117 // Base class constructor will check info != null.
119 _fileName = info.GetString("BadImageFormat_FileName");
122 _fusionLog = info.GetString("BadImageFormat_FusionLog");
130 private BadImageFormatException(String fileName, String fusionLog, int hResult)
133 SetErrorCode(hResult);
134 _fileName = fileName;
135 _fusionLog=fusionLog;
140 public String FusionLog {
141 [System.Security.SecuritySafeCritical] // auto-generated
142 [SecurityPermissionAttribute( SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)]
143 get { return _fusionLog; }
147 #if FEATURE_SERIALIZATION
148 [System.Security.SecurityCritical] // auto-generated_required
149 public override void GetObjectData(SerializationInfo info, StreamingContext context) {
150 // Serialize data for our base classes. base will verify info != null.
151 base.GetObjectData(info, context);
153 // Serialize data for this class
154 info.AddValue("BadImageFormat_FileName", _fileName, typeof(String));
157 info.AddValue("BadImageFormat_FusionLog", FusionLog, typeof(String));
159 catch (SecurityException)