Skip to content

Commit d905669

Browse files
authored
EntryPointNotFoundException F# snippets (#7694)
1 parent 53c0cf5 commit d905669

File tree

9 files changed

+124
-0
lines changed

9 files changed

+124
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module badcall1
2+
3+
// <Snippet2>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
8+
extern int MessageBox(IntPtr hwnd, String text, String caption, uint ``type``)
9+
10+
[<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
11+
extern int MessageBoxW(IntPtr hwnd, String text, String caption, uint ``type``)
12+
13+
try
14+
MessageBox(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
15+
|> ignore
16+
with :? EntryPointNotFoundException as e ->
17+
printfn $"{e.GetType().Name}:\n {e.Message}"
18+
19+
try
20+
MessageBoxW(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
21+
|> ignore
22+
with :? EntryPointNotFoundException as e ->
23+
printfn $"{e.GetType().Name}:\n {e.Message}"
24+
// </Snippet2>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module fiximportassembly1
2+
3+
// <Snippet5>
4+
printfn $"""{StringUtilities.SayGoodMorning "Dakota"}"""
5+
// The example displays the following output:
6+
// A top of the morning to you, Dakota!
7+
// </Snippet5>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="stringutilities.fs" />
9+
<Compile Include="nofunction1.fs" />
10+
<Compile Include="badcall1.fs" />
11+
<Compile Include="mangle1.fs" />
12+
<Compile Include="mangle2.fs" />
13+
<Compile Include="importassembly1.fs" />
14+
<Compile Include="fiximportassembly1.fs" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module importassembly1
2+
3+
// <Snippet4>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<DllImport("StringUtilities.dll", CharSet = CharSet.Unicode )>]
8+
extern String SayGoodMorning(String name)
9+
10+
printfn $"""{SayGoodMorning "Dakota"}"""
11+
// The example displays the following output:
12+
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point
13+
// named 'GoodMorning' in DLL 'StringUtilities.dll'.
14+
// at Example.GoodMorning(String& name)
15+
// at Example.Main()
16+
// </Snippet4>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module mangle1
2+
3+
// <Snippet7>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<DllImport "TestDll.dll">]
8+
extern int Double(int number)
9+
10+
printfn $"{Double 10}"
11+
// The example displays the following output:
12+
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find
13+
// an entry point named 'Double' in DLL '.\TestDll.dll'.
14+
// at Example.Double(Int32 number)
15+
// at Example.Main()
16+
// </Snippet7>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module mangle2
2+
3+
// <Snippet8>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")>]
8+
extern int Double(int number)
9+
10+
printfn $"{Double 10}"
11+
// The example displays the following output:
12+
// 20
13+
// </Snippet8>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module nofunction1
2+
3+
// <Snippet1>
4+
open System
5+
open System.Runtime.InteropServices
6+
7+
[<DllImport "user32.dll">]
8+
extern int GetMyNumber()
9+
10+
try
11+
let number = GetMyNumber()
12+
()
13+
with :? EntryPointNotFoundException as e ->
14+
printfn $"{e.GetType().Name}:\n {e.Message}"
15+
16+
// The example displays the following output:
17+
// EntryPointNotFoundException:
18+
// Unable to find an entry point named 'GetMyNumber' in DLL 'User32.dll'.
19+
// </Snippet1>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// <Snippet3>
2+
module StringUtilities
3+
4+
let SayGoodMorning name =
5+
$"A top of the morning to you, %s{name}!"
6+
// </Snippet3>

xml/System/EntryPointNotFoundException.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
- The call to a function in a Windows DLL cannot be resolved because the function cannot be found. In the following example, an <xref:System.EntryPointNotFoundException> exception is thrown because User32.dll does not include a function named `GetMyNumber`.
5858
5959
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/nofunction1.cs" id="Snippet1":::
60+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/nofunction1.fs" id="Snippet1":::
6061
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/nofunction1.vb" id="Snippet1":::
6162
6263
- The call to a function in a Windows DLL cannot be resolved because the name used in the method call does not match a name found in the assembly. Frequently, this occurs because the <xref:System.Runtime.InteropServices.DllImportAttribute.ExactSpelling?displayProperty=nameWithType> field is either implicitly or explicitly set to `true`, the called method includes one or more string parameters and has both an ANSI and a Unicode version, and the name used in the method call does not correspond to the name of this ANSI or Unicode version. The following example provides an illustration by attempting to call the Windows `MessageBox` function in User32.dll. Because the first method definition specifies <xref:System.Runtime.InteropServices.CharSet.Unicode?displayProperty=nameWithType> for string marshaling, the common language looks for the wide-character version of the function, `MessageBoxW`, instead of the name used in the method call, `MessageBox`. The second method definition corrects this problem by calling the `MessageBoxW` instead of the `MessageBox` function.
6364
6465
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/badcall1.cs" id="Snippet2":::
66+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/badcall1.fs" id="Snippet2":::
6567
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/badcall1.vb" id="Snippet2":::
6668
6769
- You are trying to call a function in a dynamic link library by its simple name rather than its decorated name. Typically, the C++ compiler generates a decorated name for DLL functions. For example, the following C++ code defines a function named `Double` in a library named TestDll.dll.
@@ -71,28 +73,33 @@
7173
When the code in the following example tries to call the function, an <xref:System.EntryPointNotFoundException> exception is thrown because the `Double` function cannot be found.
7274
7375
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/mangle1.cs" id="Snippet7":::
76+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/mangle1.fs" id="Snippet7":::
7477
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/mangle1.vb" id="Snippet7":::
7578
7679
However, if the function is called by using its decorated name (in this case, `?Double@@YAHH@Z`), the function call succeeds, as the following example shows.
7780
7881
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/mangle2.cs" id="Snippet8":::
82+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/mangle2.fs" id="Snippet8":::
7983
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/mangle2.vb" id="Snippet8":::
8084
8185
You can find the decorated names of functions exported by a DLL by using a utility such as Dumpbin.exe.
8286
8387
- You are attempting to call a method in a managed assembly as if it were an unmanaged dynamic link library. To see this in action, compile the following example to an assembly named StringUtilities.dll.
8488
8589
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/stringutilities.cs" id="Snippet3":::
90+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/stringutilities.fs" id="Snippet3":::
8691
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/stringutilities.vb" id="Snippet3":::
8792
8893
Then compile and execute the following example, which attempts to call the `StringUtilities.SayGoodMorning` method in the StringUtilities.dll dynamic link library as if it were unmanaged code. The result is an <xref:System.EntryPointNotFoundException> exception.
8994
9095
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/importassembly1.cs" id="Snippet4":::
96+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/importassembly1.fs" id="Snippet4":::
9197
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/importassembly1.vb" id="Snippet4":::
9298
9399
To eliminate the exception, add a reference to the managed assembly and access the `StringUtilities.SayGoodMorning` method just as you would access any other method in managed code, as the following example does.
94100
95101
:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/fiximportassembly1.cs" id="Snippet5":::
102+
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/fiximportassembly1.fs" id="Snippet5":::
96103
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/fiximportassembly1.vb" id="Snippet5":::
97104
98105
- You are trying to call a method in a COM DLL as if it were a Windows DLL. To access a COM DLL, select the **Add Reference** option in Visual Studio to add a reference to the project, and then select the type library from the **COM** tab.

0 commit comments

Comments
 (0)