Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module badcall1

// <Snippet2>
open System
open System.Runtime.InteropServices

[<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
extern int MessageBox(IntPtr hwnd, String text, String caption, uint ``type``)

[<DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )>]
extern int MessageBoxW(IntPtr hwnd, String text, String caption, uint ``type``)

try
MessageBox(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
|> ignore
with :? EntryPointNotFoundException as e ->
printfn $"{e.GetType().Name}:\n {e.Message}"

try
MessageBoxW(IntPtr 0, "Calling the MessageBox Function", "Example", 0u)
|> ignore
with :? EntryPointNotFoundException as e ->
printfn $"{e.GetType().Name}:\n {e.Message}"
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module fiximportassembly1

// <Snippet5>
printfn $"""{StringUtilities.SayGoodMorning "Dakota"}"""
// The example displays the following output:
// A top of the morning to you, Dakota!
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="stringutilities.fs" />
<Compile Include="nofunction1.fs" />
<Compile Include="badcall1.fs" />
<Compile Include="mangle1.fs" />
<Compile Include="mangle2.fs" />
<Compile Include="importassembly1.fs" />
<Compile Include="fiximportassembly1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module importassembly1

// <Snippet4>
open System
open System.Runtime.InteropServices

[<DllImport("StringUtilities.dll", CharSet = CharSet.Unicode )>]
extern String SayGoodMorning(String name)

printfn $"""{SayGoodMorning "Dakota"}"""
// The example displays the following output:
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point
// named 'GoodMorning' in DLL 'StringUtilities.dll'.
// at Example.GoodMorning(String& name)
// at Example.Main()
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module mangle1

// <Snippet7>
open System
open System.Runtime.InteropServices

[<DllImport "TestDll.dll">]
extern int Double(int number)

printfn $"{Double 10}"
// The example displays the following output:
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find
// an entry point named 'Double' in DLL '.\TestDll.dll'.
// at Example.Double(Int32 number)
// at Example.Main()
// </Snippet7>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module mangle2

// <Snippet8>
open System
open System.Runtime.InteropServices

[<DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")>]
extern int Double(int number)

printfn $"{Double 10}"
// The example displays the following output:
// 20
// </Snippet8>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module nofunction1

// <Snippet1>
open System
open System.Runtime.InteropServices

[<DllImport "user32.dll">]
extern int GetMyNumber()

try
let number = GetMyNumber()
()
with :? EntryPointNotFoundException as e ->
printfn $"{e.GetType().Name}:\n {e.Message}"

// The example displays the following output:
// EntryPointNotFoundException:
// Unable to find an entry point named 'GetMyNumber' in DLL 'User32.dll'.
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// <Snippet3>
module StringUtilities

let SayGoodMorning name =
$"A top of the morning to you, %s{name}!"
// </Snippet3>
7 changes: 7 additions & 0 deletions xml/System/EntryPointNotFoundException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@
- 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`.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/nofunction1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/nofunction1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/nofunction1.vb" id="Snippet1":::

- 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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/badcall1.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/badcall1.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/badcall1.vb" id="Snippet2":::

- 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.
Expand All @@ -71,28 +73,33 @@
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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/mangle1.cs" id="Snippet7":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/mangle1.fs" id="Snippet7":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/mangle1.vb" id="Snippet7":::

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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/mangle2.cs" id="Snippet8":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/mangle2.fs" id="Snippet8":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/mangle2.vb" id="Snippet8":::

You can find the decorated names of functions exported by a DLL by using a utility such as Dumpbin.exe.

- 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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/stringutilities.cs" id="Snippet3":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/stringutilities.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/stringutilities.vb" id="Snippet3":::

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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/importassembly1.cs" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/importassembly1.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/importassembly1.vb" id="Snippet4":::

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.

:::code language="csharp" source="~/snippets/csharp/System/EntryPointNotFoundException/Overview/fiximportassembly1.cs" id="Snippet5":::
:::code language="fsharp" source="~/snippets/fsharp/System/EntryPointNotFoundException/Overview/fiximportassembly1.fs" id="Snippet5":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.entrypointnotfoundexception.class/vb/fiximportassembly1.vb" id="Snippet5":::

- 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.
Expand Down