C# Error Finding Method in DLLImport

半城伤御伤魂 提交于 2019-12-12 23:36:54

问题


I have a C++ assembly that I am importing using DLLImport.

I am attempting to call its method:

namespace Testing
{
class Test{
int Run(char* filePath, bool bEntry, double duration){//code}
};
}

by

[DllImport(dllName, CharSet = CharSet.Auto)]
        public static extern int Run(string filePath, bool bEntry, double duration)
            );

When I call its method, I get the error message:

Unable to find an entry point named Run in dll


回答1:


The "Run" looks to be a non-static class method. Although, it's possible to call such methods from C# this is not the primary use-case. It would be way easier to consume it from .NET if you expose it via COM, or at-least as a plain C interface:


extern "C" __declspec(dllexport) void* Testing_Test_Create();
extern "C" __declspec(dllexport) void Testing_Test_Destroy(void* self);
extern "C" __declspec(dllexport) int Testing_Test_Run(void* self, char* filePath, bool bEntry, double duration);

And here is a sample how to call C++ class methods from C#:


// Test.cpp in NativeLib.dll
namespace Testing
{
    class __declspec(dllexport) Test
    {
    public:
        explicit Test(int data)
            : data(data)
        {
        }

        int Run(char const * path)
        {
            return this->data + strlen(path);
        }

    private:
        int data;
    };
}


// Program.cs in CSharpClient.exe
class Program
    {
        [DllImport(
            "NativeLib.dll",
            EntryPoint = "??0Test@Testing@@QAE@H@Z",
            CallingConvention = CallingConvention.ThisCall,
            CharSet = CharSet.Ansi)]
        public static extern void TestingTestCtor(IntPtr self, int data);

        [DllImport(
            "NativeLib.dll",
            EntryPoint = "?Run@Test@Testing@@QAEHPBD@Z",
            CallingConvention = CallingConvention.ThisCall,
            CharSet = CharSet.Ansi)]
        public static extern int TestingTestRun(IntPtr self, string path);

        static void Main(string[] args)
        {
            var test = Marshal.AllocCoTaskMem(4);
            TestingTestCtor(test, 10);

            var result = TestingTestRun(test, "path");

            Console.WriteLine(result);

            Marshal.FreeCoTaskMem(test);
        }
    }

Entry point names might be different for your build configuration/compiler, so use dumpbin utility to obtain them. Again, this is just a proof of concept, in real code it would be better to use COM.




回答2:


See here: http://dotnetperls.com/dllimport




回答3:


I'm not sure this will help if the function is a member of a class, but to locate the entry point by name, not ordinal, you'll need a .def file in your dll..

LIBRARY  mylib
    Run @1


来源:https://stackoverflow.com/questions/4885091/c-sharp-error-finding-method-in-dllimport

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!