代码之家  ›  专栏  ›  技术社区  ›  subham

DLL函数调用不工作

  •  2
  • subham  · 技术社区  · 6 年前

    我创建了一个名为ClassLibrary1的DLL。dll。
    它只包含一个函数 iscalled() 类内 1类

    //Function of DLL
    public bool iscalled()
      {
         return true;
      }
    

    现在,我已经创建了一个新的WINFORM项目,并在其中添加了我自己的dll引用 类别库1

    下面是winForm代码的代码片段

    [DllImport("ClassLibrary1.dll")]
    public static extern bool iscalled();
    
    
    public void mydllcall1()
     {          
         bool ud = iscalled();
         MessageBox.Show(ud.ToString());
     }
    

    运行应用程序时,遇到一个错误

    在DLL“ClassLibrary1”中找不到名为“iscalled”的入口点。动态链接库

    我正在寻找一些解决方案。

    谢谢和问候

    Subham Kumar, Nathcorp公司

    2 回复  |  直到 6 年前
        1
  •  4
  •   TheGeneral    6 年前

    你不能打电话 DLLImport 在上。网络组装。(DLLImport属性用于标准 Dynamic-Link Libraries ).您需要使用 Assembly.Load 或类似

    How to: Load Assemblies into an Application Domain

    有几种方法可以将程序集加载到应用程序域中。 建议使用static(在Visual Basic中共享) Load 的方法 System.Reflection.Assembly 班其他方式组件 可以加载的内容包括:

    • Assembly类的LoadFrom方法加载给定 文件位置。使用此方法加载程序集时使用不同的 加载上下文。

    • 这个 ReflectionOnlyLoad ReflectionOnlyLoadFrom 方法加载 组装到仅反射上下文中。加载到此中的程序集 可以检查但不执行上下文,允许检查 以其他平台为目标的程序集。

    实例

    public static void Main()
    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }
    

    进一步阅读

    Assembly.Load Method (AssemblyName)

        2
  •  0
  •   kritikaTalwar    6 年前

    你必须申报入境点

    [DllImport("ClassLibrary1.dll", EntryPoint = "iscalled", CallingConvention = CallingConvention.Cdecl)]
    public static extern bool iscalled();