繁体   English   中英

C#程序在C DLL中找不到函数

[英]C# program doesn't find function in C DLL

我遇到一个C#程序的问题,该程序调用C DLL中的函数。 我正在使用VS 2015。

对XBaseResolveAll的第一次调用有效但是对XBaseSeek的调用导致弹出窗口说:

An unhandled exception of type 'System.EntryPointNotFoundException' occurred in Call_C.exe

Additional information: Unable to find an entry point named 'XBaseSeek' in DLL 'W:\C_sharp\Call_C\Debug\C_dll.dll'.

请注意,dll的dumpbin显示已按预期导出的所有函数。 请参阅下面的输出。

我试过搜索,但没有发现这个问题的讨论。 所以我必须做一些愚蠢的事情,但我只是没有看到它。 我创建了一个小样本来说明问题。

有人可以帮忙吗?

以下是示例文件:

----------
Program.cs
----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using XBaseNamespace.SecondNamespace;


namespace Call_C
{
    class Program
    {

        static void Main()
        {
            XBaseFunctions.XBaseResolveAll();
            XBaseFunctions.XBaseSeek(UIntPtr.Zero, 0, 0);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

-----------------
XBaseNamespace.cs
-----------------
//  XBase functions
using System;
using System.Runtime.InteropServices;

namespace XBaseNamespace.SecondNamespace
{
    class XBaseFunctions
    {
        [DllImport("W:\\C_sharp\\Call_C\\Debug\\C_dll.dll", CharSet = CharSet.Ansi)]
        public static extern int XBaseResolveAll();

        [DllImport("W:\\C_sharp\\Call_C\\Debug\\C_dll.dll", CharSet = CharSet.Ansi)]
        public static extern int XBaseSeek(UIntPtr myhandle, long offset, int origin);
    }

} 

-------
c_dll.c
-------
//  C DLL experiment

#include <stdio.h>

#define DSI_DLL __declspec(dllexport)
#define CALL_TYPE __stdcall

DSI_DLL int CALL_TYPE XBaseResolveAll()
{
    return 0;
}


DSI_DLL int CALL_TYPE XBaseSeek(unsigned int *handle, long offset, int origin)
{
    return 0;
}

---------------
Output of build
---------------
1>------ Rebuild All started: Project: C_dll, Configuration: Debug Win32 ------
1>  c_dll.c
1>     Creating library W:\C_sharp\Call_C\Debug\C_dll.lib and object W:\C_sharp\Call_C\Debug\C_dll.exp
1>  C_dll.vcxproj -> W:\C_sharp\Call_C\Debug\C_dll.dll
2>------ Rebuild All started: Project: Call_C, Configuration: Debug Any CPU ------
2>  Call_C -> W:\C_sharp\Call_C\bin\Debug\Call_C.exe
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========

-----------------------
output from dumbbin.exe
-----------------------

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\dumpbin.exe" /exports "W:\C_sharp\Call_C
\Debug\C_dll.dll"
Microsoft (R) COFF/PE Dumper Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file W:\C_sharp\Call_C\Debug\C_dll.dll

File Type: DLL

  Section contains the following exports for C_dll.dll

    00000000 characteristics
    562D7696 time date stamp Sun Oct 25 20:40:54 2015
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00011028 _XBaseResolveAll@0 = @ILT+35(_XBaseResolveAll@0)
          2    1 00011082 _XBaseSeek@12 = @ILT+125(_XBaseSeek@12)

  Summary

        1000 .00cfg
        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        5000 .text
       10000 .textbss

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>

根据您的dumpbin输出,DLL导出表中的函数名称已经“修饰”以包含有关参数数量的信息。 您需要禁用名称修饰,或在C#代码中使用DllImport中的修饰名称。 就个人而言,我会禁用名字装饰。 您可以通过在DLL项目中创建模块定义文件(.def)并列出导出的函数名来禁用导出函数的名称修饰。 例:

LIBRARY C_dll
EXPORTS
    XBaseResolveAll
    XBaseSeek

此外,在您的C#代码中包含作为DllImport一部分的调用约定:

[DllImport("W:\\\\C_sharp\\\\Call_C\\\\Debug\\\\C_dll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]

编辑(2015年10月26日):

仔细检查.def文件中的语法,并确保.def文件已添加到解决方案中,并在链接器设置下列为模块定义文件 这是一个简短的例子,表明这确实有效:

  1. 我创建了一个带有单个源文件的新解决方案(Win32 DLL)(Example.c):
#include <Windows.h>

__declspec(dllexport) int __stdcall ExampleFunction(int param1, int param2)
{
    return 0;
}
  1. 我构建了解决方案,然后在生成的ExampleDll.dll上运行dumpbin 结果如下(显示装饰名称):
Dump of file ExampleDll.dll

File Type: DLL

  Section contains the following exports for ExampleDll.dll

    00000000 characteristics
    562E5A83 time date stamp Mon Oct 26 12:53:23 2015
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 0001100F _ExampleFunction@8 = @ILT+10(_ExampleFunction@8)
  1. 我增加了以下DEF文件(Example.def)到我的解决方案,然后双重检查该文件被列为该项目的设置(链接,输入,模块定义文件) 模块定义文件
LIBRARY ExampleDll
EXPORTS
    ExampleFunction
  1. 最后,我重新构建了然后在新的ExampleDll.Dll上重新运行dumpbin (显示名称修饰已被删除):
Dump of file ExampleDll.dll

File Type: DLL

  Section contains the following exports for ExampleDll.dll

    00000000 characteristics
    562E5CBF time date stamp Mon Oct 26 13:02:55 2015
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 0001100F ExampleFunction = @ILT+10(_ExampleFunction@8)

关于在C#代码中向[DllImport]添加CallingConvention的问题,请仔细检查语法。 如果没有明显错误,请将您的代码与错误消息一起发布。

这有效:

1)删除#define CALL_TYPE __stdcall

2)将“,CallingConvention = CallingConvention.Cdecl”添加到DLLImport

3)不要使用.DEF文件

它的工作原理是因为cdecl没有装饰: https ://msdn.microsoft.com/en-us/library/vstudio/x7kb4e2f( v = vs.100).aspx

我在我的示例代码和我的实际代码中测试了这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM