Question

I have created a WIN32 DLL project and its dllmain.cpp is as follows;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

_declspec(dllexport) float RGBCompare()
{
    return 100;
}

My target is to call method RGBCompare from a C# project and as per rule I have mentioned dllexport tag before it.

On the other side in C# project I have defined an entry point as follows;

namespace LogoFinderWrapper
{
    public class LogoFinder
    {
        [DllImport("LogoIdentifier.dll", EntryPoint = "RGBCompare")]
        private static extern float Api_RGBCompare();


        public static float RGBCompare()
        {
            return Api_RGBCompare();
        }
    }
}

When I call DLL it raises exception System.EntryPointNotFoundException.

Please could any one help me in this regard?

Was it helpful?

Solution

Your native code is C++ and the name is mangled before export. Possible solutions:

  1. Use the mangled name in the EntryPoint parameter. Find out the mangled name with dumpbin or Dependency Viewer.
  2. Use a .def file rather than __declspec(dllexport) to control which functions are exported.
  3. Suppress mangling with extern "C" in your C++ source code.

The final option would look like this:

extern "C" 
{
    __declspec(dllexport) float RGBCompare()
    {
        return 100;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top