“Unable to find an entry point named [function] in

2019-01-22 13:01发布

I have a dll which comes from a third party, which was written in C++. Here is some information that comes from the dll documentation:

//start documentation

RECO_DATA{
wchar_t Surname[200];
wchar_t Firstname[200];
}

Description: Data structure for receiving the function result. All function result will be stored as Unicode (UTF-8).

Method:

bool recoCHN_P_Name(char *imgPath,RECO_DATA *o_data);

Input:

char * imgPath

the full path of the image location for this function to recognize

RECO_DATA * o_data

data object for receiving the function result. Function return: True if Success, otherwise false will return.

//end documentation

I am trying to call the recoCHN_P_Name from my C# application. To this end, I came up with this code:

The code to import the dll:

    public class cnOCRsdk
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct RECO_DATA{
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)]
        public string FirstName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
        public string Surname;
        }

    [DllImport(@"cnOCRsdk.dll", EntryPoint="recoCHN_P_Name")]
    public static extern bool recoCHN_P_Name(byte[] imgPath, RECO_DATA o_data);
}

The code to call the function:

            cnOCRsdk.RECO_DATA recoData = new cnOCRsdk.RECO_DATA();

        string path = @"C:\WINDOWS\twain_32\twainrgb.bmp";

        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] bytes = encoding.GetBytes(path);

        bool res = cnOCRsdk.recoCHN_P_Name(bytes, recoData);

And the error I'm getting is ""Unable to find an entry point named 'recoCHN_P_Name' in DLL 'cnOCRsdk.dll'." I'm suspecting that I'm having an error in converting a type from C++ to C#. But where exactly ... ?

7条回答
ら.Afraid
2楼-- · 2019-01-22 13:37

I solved the same problem in these steps:

step 1) If you program your custom DLL in C++ using Visual studio,then at the property page of your project set the Common Language Runtime Support (/clr)parameter to Common Language Runtime Support (/clr).

step 2) To function deceleration in .h file use __declspec(dllexport) keyword like below:

__declspec(dllexport) double Sum(int a,int b);

step 3) Build and export DLL file, then use the Dependency Walker software to get your function EntryPoint.

step4) Import DLL file In the C# project and set EntryPoint and CallingConvention variable like below:

[DllImport("custom.dll", EntryPoint = "?Sum@@YAXHHHHHHNNN@Z", CallingConvention = CallingConvention.Cdecl)]

    public static extern double Sum(int a,int b);
查看更多
趁早两清
3楼-- · 2019-01-22 13:37

Correct EntryPoint string could be found in ".lib" file that comes along with main unmanaged dll.

查看更多
4楼-- · 2019-01-22 13:45

First make sure the function is actually exported:

In the Visual Studio Command Prompt, use dumpbin /exports whatever.dll

查看更多
\"骚年 ilove
5楼-- · 2019-01-22 13:46

I'd write a wrapper using C++/CLI. This wrapper will be able to include the .h files and link to the .lib files you got from the third party vendor. Then it is both easy and safe to write a managed interface for your C# program.

查看更多
叛逆
6楼-- · 2019-01-22 13:48

C# doesn't support C++ name mangling and you either need to declare the C++ functions with

extern "C" {...}

(may not an option if they're from a third party), or call the mangled name directly if you can get it to work. It may be easier to get the third party to provide a non-mangled interface to the functionality.

查看更多
我只想做你的唯一
7楼-- · 2019-01-22 13:53

You could try using the unmangled name while specifying a CallingConvention in the DllImport

查看更多
登录 后发表回答