首页 > 解决方案 > System.EntryPointNotFoundException 即使在 c# 代码中指定入口点(函数名)之后

问题描述

浏览了许多文章和堆栈溢出链接,但找不到解决方案,或者可能犯了一些愚蠢的错误。因此,如果有人可以帮助我解决问题,请在此处发布我的代码。非常感激。提前致谢。

外部文件

#define EXPORT extern "C" __declspec( dllexport ) __cdecl FillAndReturnString(char **someString);

#include <string.h>
#include <stdlib.h>

static void _stdcall FillAndReturnString(char **someString)
{
    *someString = (char*)malloc(sizeof(200));
     strcpy_s(*someString, 200, "This is a sample Test");  
} 

程序.cs

using System;
using System.Runtime.InteropServices;

namespace CallingCpp
{
    class Program
    {
        static void Main(string[] args)
        {
            IntPtr ptr = IntPtr.Zero;

            NativeTest.FillAndReturnString(ref ptr);
            String s = Marshal.PtrToStringAnsi(ptr);
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
    public static class NativeTest
    {
        private const string DllFilePath = @"C:\Test\MemoryTest\MemoryTestApp.dll";

        [DllImport(DllFilePath, EntryPoint = "FillAndReturnString")]
        public static extern void FillAndReturnString(ref IntPtr ptr);
    }
}

一些我尝试过但没有成功的设置:

  1. 从 cpp 项目属性启用 CLR 支持。错误:严重性代码描述项目文件行抑制状态错误 D8016“/clr”和“/EHs”命令行选项不兼容 MemoryTestApp [项目路径]

  2. 将 CPP 项目属性中的调用约定设置为 __cdecl(/Gd)。我也尝试使用 __stdcall 在文件中指定。

    [DllImport(DllFilePath, EntryPoint = "FillAndReturnString", CallingConvention = CallingConvention.StdCall)] public static extern void FillAndReturnString(ref IntPtr ptr);

  3. 使用 Dependency Walker 检查了 cpp dll,但它引发了错误:未找到至少一个必需的隐式或转发的依赖项。

标签: c#exception

解决方案


推荐阅读