C# code can't "find" exported function in a DLL, Works in C++ test program

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.

c#
c++
.net
pinvoke
asked on Stack Overflow Dec 3, 2019 by C.V. Vick • edited Dec 3, 2019 by GSerg

1 Answer

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

answered on Stack Overflow Dec 3, 2019 by Mazen Ak

User contributions licensed under CC BY-SA 3.0