Skip to content

Commit fcb2620

Browse files
authored
System.BadImageFormatException F# snippets (#7527)
1 parent a8891ba commit fcb2620

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module condition1
2+
3+
// <Snippet1>
4+
open System
5+
open System.Reflection
6+
7+
// Windows DLL (non-.NET assembly)
8+
let filePath =
9+
let filePath = Environment.ExpandEnvironmentVariables "%windir%"
10+
let filePath =
11+
if not (filePath.Trim().EndsWith @"\") then
12+
filePath + @"\"
13+
else filePath
14+
filePath + @"System32\Kernel32.dll"
15+
16+
try
17+
Assembly.LoadFile filePath |> ignore
18+
with :? BadImageFormatException as e ->
19+
printfn $"Unable to load {filePath}."
20+
printfn $"{e.Message[0 .. e.Message.IndexOf '.']}"
21+
22+
// The example displays an error message like the following:
23+
// Unable to load C:\WINDOWS\System32\Kernel32.dll.
24+
// Bad IL format.
25+
// </Snippet1>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="condition1.fs" />
8+
<Compile Include="stringlib.fs" />
9+
<Compile Include="loadstringlib.fs" />
10+
<Compile Include="targetplatform1.fs" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module loadstringlib
2+
3+
// <Snippet3>
4+
open System.Reflection
5+
6+
let title = "a tale of two cities"
7+
8+
// Load assembly containing StateInfo type.
9+
let assem = Assembly.LoadFrom @".\StringLib.dll"
10+
11+
// Get type representing StateInfo class.
12+
let stateInfoType = assem.GetType "StringLib"
13+
14+
// Get Display method.
15+
let mi = stateInfoType.GetMethod "ToProperCase"
16+
17+
// Call the Display method.
18+
let properTitle =
19+
mi.Invoke(null, [| box title |]) :?> string
20+
21+
printfn $"{properTitle}"
22+
23+
// </Snippet3>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace global
2+
3+
// <Snippet2>
4+
open System
5+
6+
module StringLib =
7+
let private exceptionList = [ "a"; "an"; "the"; "in"; "on"; "of" ]
8+
let private separators = [| ' ' |]
9+
10+
[<CompiledName "ToProperCase">]
11+
let toProperCase (title: string) =
12+
title.Split(separators, StringSplitOptions.RemoveEmptyEntries)
13+
|> Array.mapi (fun i word ->
14+
if i <> 0 && List.contains word exceptionList then
15+
word
16+
else
17+
word[0..0].ToUpper() + word[1..])
18+
|> String.concat " "
19+
20+
// Attempting to load the StringLib.dll assembly produces the following output:
21+
// Unhandled Exception: System.BadImageFormatException:
22+
// The format of the file 'StringLib.dll' is invalid.
23+
// </Snippet2>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module targetplatform1
2+
3+
// <Snippet4>
4+
open System
5+
open System.IO
6+
open System.Reflection
7+
8+
let args = Environment.GetCommandLineArgs()
9+
10+
if args.Length = 1 then
11+
printfn "\nSyntax: PlatformInfo <filename>\n"
12+
else
13+
printfn ""
14+
// Loop through files and display information about their platform.
15+
for i = 1 to args.Length - 1 do
16+
let fn = args[i]
17+
if not (File.Exists fn) then
18+
printfn $"File: {fn}"
19+
printfn "The file does not exist.\n"
20+
else
21+
try
22+
let an = AssemblyName.GetAssemblyName fn
23+
printfn $"Assembly: {an.Name}"
24+
if an.ProcessorArchitecture = ProcessorArchitecture.MSIL then
25+
printfn "Architecture: AnyCPU"
26+
else
27+
printfn $"Architecture: {an.ProcessorArchitecture}"
28+
printfn ""
29+
30+
with :? BadImageFormatException ->
31+
printfn $"File: {fn}"
32+
printfn "Not a valid assembly.\n"
33+
34+
// </Snippet4>

xml/System/BadImageFormatException.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@
7070
- You are trying to load an unmanaged dynamic link library or executable (such as a Windows system DLL) as if it were a .NET assembly. The following example illustrates this by using the <xref:System.Reflection.Assembly.LoadFile%2A?displayProperty=nameWithType> method to load Kernel32.dll.
7171
7272
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.badimageformatexception.class/cs/condition1.cs" id="Snippet1":::
73+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.badimageformatexception.class/fs/condition1.fs" id="Snippet1":::
7374
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.badimageformatexception.class/vb/condition1.vb" id="Snippet1":::
7475
75-
To address this exception, access the methods defined in the DLL by using the features provided by your development language, such as the `Declare` statement in Visual Basic or the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute with the `extern` keyword in C#.
76+
To address this exception, access the methods defined in the DLL by using the features provided by your development language, such as the `Declare` statement in Visual Basic or the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute with the `extern` keyword in C# and F#.
7677
7778
- You are trying to load a reference assembly in a context other than the reflection-only context. You can address this issue in either of two ways:
7879
@@ -86,18 +87,21 @@
8687
- Your application's components were created using different versions of .NET. Typically, this exception occurs when an application or component that was developed using the .NET Framework 1.0 or the .NET Framework 1.1 tries to load an assembly that was developed using the .NET Framework 2.0 SP1 or later, or when an application that was developed using the .NET Framework 2.0 SP1 or .NET Framework 3.5 tries to load an assembly that was developed using the .NET Framework 4 or later. The <xref:System.BadImageFormatException> may be reported as a compile-time error, or the exception may be thrown at run time. The following example defines a `StringLib` class that has a single member, `ToProperCase`, and that resides in an assembly named StringLib.dll.
8788
8889
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.badimageformatexception.class/cs/stringlib.cs" id="Snippet2":::
90+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.badimageformatexception.class/fs/stringlib.fs" id="Snippet2":::
8991
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.badimageformatexception.class/vb/stringlib.vb" id="Snippet2":::
9092
9193
The following example uses reflection to load an assembly named StringLib.dll. If the source code is compiled with a .NET Framework 1.1 compiler, a <xref:System.BadImageFormatException> is thrown by the <xref:System.Reflection.Assembly.LoadFrom%2A?displayProperty=nameWithType> method.
9294
9395
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.badimageformatexception.class/cs/loadstringlib.cs" id="Snippet3":::
96+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.badimageformatexception.class/fs/loadstringlib.fs" id="Snippet3":::
9497
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.badimageformatexception.class/vb/loadstringlib.vb" id="Snippet3":::
9598
9699
To address this exception, make sure that the assembly whose code is executing and that throws the exception, and the assembly to be loaded, both target compatible versions of .NET.
97100
98101
- The components of your application target different platforms. For example, you are trying to load ARM assemblies in an x86 application. You can use the following command-line utility to determine the target platforms of individual .NET assemblies. The list of files should be provided as a space-delimited list at the command line.
99102
100103
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.badimageformatexception.class/cs/targetplatform1.cs" id="Snippet4":::
104+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.badimageformatexception.class/fs/targetplatform1.fs" id="Snippet4":::
101105
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.badimageformatexception.class/vb/targetplatform1.vb" id="Snippet4":::
102106
103107
- Reflecting on C++ executable files may throw this exception. This is most likely caused by the C++ compiler stripping the relocation addresses or the .Reloc section from the executable file. To preserve the .relocation address in a C++ executable file, specify /fixed:no when linking.

0 commit comments

Comments
 (0)