Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中动态加载c dll#_C#_C_Windows - Fatal编程技术网

C# 在c中动态加载c dll#

C# 在c中动态加载c dll#,c#,c,windows,C#,C,Windows,我有一个c dll,希望通过c#动态加载它。我是这样做的: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Dota2Plugins { class Interop { #region Win API [DllImport("ke

我有一个c dll,希望通过c#动态加载它。我是这样做的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Dota2Plugins
{
    class Interop
    {
        #region Win API
        [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(string lpLibFileName);

        [DllImport("kernel32.dll")]
        public extern static IntPtr GetProcAddress(IntPtr hLib, string lpProcName);

        [DllImport("kernel32.dll")]
        public extern static bool FreeLibrary(IntPtr hLib);
        #endregion

        private IntPtr hLib;

        public Interop(String DLLPath)
        {
            hLib = LoadLibrary(DLLPath);
            if (hLib == IntPtr.Zero)
            {
                throw new Exception("not found dll : " + DLLPath);
            }
        }

        ~Interop()
        {
            FreeLibrary(hLib);
        }

        public IntPtr GetIntPtr(string APIName)
        {
            IntPtr api = GetProcAddress(hLib, APIName);
            if (api == IntPtr.Zero)
            {
                throw new Exception("not found api : " + APIName);
            }

            return api;
        }

        public Delegate GetDelegate(string APIName, Type t)
        {
            IntPtr api = GetIntPtr(APIName);
            return Marshal.GetDelegateForFunctionPointer(api, t);
        }

    }
}
像这样放置dll:

Interop Interop=新的Interop(“KeyBoardHook.dll”)`

但当我运行应用程序时,它会抛出错误:

找不到dll:KeyBoardHook.dll

我已将dll复制到应用程序目录

我使用相对论目录和绝对目录进行了尝试,得到了相同的错误结果


如何在c#中动态加载c DLL并调用DLL导出api

您提出了两个问题,第一,未找到DLL,以及如何从C进行动态加载

  • 您是否确保在与DLL相同的体系结构中编译项目
当尝试从C#32位项目加载X64 dll时,这是一个常见问题

  • 您是否尝试过构造函数
    公共互操作(字符串DLLPath)
    中的File.Exists
也许您的应用程序目录不正确,正如Stefan在评论中提到的那样

也可以尝试完整路径


关于动态加载,这是另一个主题。必须在DLL中指定参数,理论上可以在运行时指定

如果您有头文件,您可以创建一个从
System.Dynamic.DynamicObject
派生的类,并在运行时解析头文件,覆盖
TryInvokeMember
(一旦得到它,就可以解析它)

不过我不会用这种方法


恐怕我不知道在没有标题的情况下如何动态地执行此操作。

据我所知,我认为您的声明是错误的:

[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);

[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeLibrary(IntPtr hModule);
我是从你这儿拿的

您面临的问题可能是由于C#将默认情况下将
字符串
封送为
BStr
(长度前缀字符串)(),但
LoadLibrary
将解决这一问题,因为它需要以null结尾的ansi字符串(
LPStr


您可能还对Vanara.PInvoke.Kernel32(,)感兴趣,它及其配套库包含您可能需要的所有Windows API定义。

应用程序目录
,这是exe所在的位置吗?欢迎访问该网站。访问。您不想在dll中使用
DllImport
有什么特别的原因吗?是的,我确信dll文件目录是正确的。我已经解决了这个问题。非常感谢。是的,我确信dll文件目录是正确的。我已经解决了这个问题。非常感谢。我使用了一个C#64位项目和X64 C dll,然后成功加载dll,Thakns!谢谢,这不是程序无法加载dll的原因。但这是找到dll函数项的解决方案。谢谢!