0

I created a C++ (native) DLL that exports one function and I'm trying to call in from C# (managed) code and I receive runtime error that the function name can't be found in my DLL.

Here's the code of the DLL:

#include "pch.h"
#include "framework.h"
#include "Device.h"

// This is an example of an exported function.
DEVICE_API bool DeviceAvailable(void)
{
    return false;
}

The header exports the function like:

#ifdef DEVICE_EXPORTS
#define DEVICE_API __declspec(dllexport)
#else
#define DEVICE_API __declspec(dllimport)
#endif

DEVICE_API bool DeviceAvailable(void);

In C# I import the function as such:

.. enclosing code ...
internal static class NativeMethods
{
    [DllImport("Device.dll", CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeviceAvailable();

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.I4)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
}

When I call NativeMethods.DeviceAvailable(), I get an error on invocation:

System.EntryPointNotFoundException
HResult=0x80131523
Message=Unable to find an entry point named 'DeviceAvailable' in DLL 'Device.dll'.

The MessageBox function works fine when called by the program (C#). A C++ test program is able to find and invoke the DLL function.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 3
    Possible duplicate of [Unable to find an entry point when calling C++ dll in C#](https://stackoverflow.com/questions/10109590/unable-to-find-an-entry-point-when-calling-c-dll-in-c-sharp) – GSerg Dec 03 '19 at 22:21
  • On top of that, the C's `bool` is not `UnmanagedType.Bool`. `UnmanagedType.Bool` is [`BOOL`](https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types#BOOL). You want [`MarshalAs(UnmanagedType.I1)`](https://stackoverflow.com/a/53899333/11683). – GSerg Dec 03 '19 at 22:24

1 Answers1

-1

I guess your problem related to access settings on function level when creating the mentioned functions ic DLL.

here

you can find a useful links could help you to understand the case better and to find the best solution.

Mazen Ak
  • 152
  • 7