Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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#加载DLL?_C#_Dll_Pinvoke - Fatal编程技术网

如何使用C#加载DLL?

如何使用C#加载DLL?,c#,dll,pinvoke,C#,Dll,Pinvoke,我有一个小DLL,它有3个函数:Init、Load和run。 我是c#新手,所以在阅读这里的问题时,我打开了一个控制台项目,希望加载DLL并使用它的函数。 不幸的是,它不起作用。谁能告诉我哪里出了问题 这是我得到的错误: Unable to find an entry point named 'Init' in DLL '....path to my DLL....'. 代码如下: using System; using System.Collections.Generic; using S

我有一个小DLL,它有3个函数:Init、Load和run。 我是c#新手,所以在阅读这里的问题时,我打开了一个控制台项目,希望加载DLL并使用它的函数。 不幸的是,它不起作用。谁能告诉我哪里出了问题

这是我得到的错误:

Unable to find an entry point named 'Init' in DLL 
'....path to my DLL....'.
代码如下:

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

namespace ConsoleApplication1
{
   class Program
    {

       [DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
        public static extern bool Init();

        [DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
         public static extern bool Load(string file);


         [DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
        public static extern bool Play();



        static void Main()
        {
            Console.WriteLine("got till here!!!");

            Init();

            Load("C:\\Desktop\\the_thing_about_dogs_480x270.mp4");
            Play();

        }
    }
}

我唯一能怀疑的可能是我没有创建实例? 除此之外,没有线索:(

*编辑: 这是DLL:

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

namespace DLLTest
{
    public class DLLTestApi
    {
    myInfo localPlay;

    public bool Init()
    {
        localPlay =  new myInfo();

        return true;

    }


    public bool Load(string file)
    {
        localPlay.Load(file);
        return true;
    }
    public bool Play()
    {
        localPlay.StartNewThread();
        return true;
    }

    public bool Stop()
    {
        localPlay.DxStopWMp();
        return true;

    }
    public bool Pause()
    {
        localPlay.DxPause();
        return true;
    }

    public bool Resume()
    {
        localPlay.DxResume();
        return true;
    }
    public bool Close()
    {
        localPlay.DxClose();
        return true;
    }
}
}

错误消息清楚地告诉您问题所在。DLL没有导出名为
Init
的函数。可能的原因包括:

  • 该DLL不是非托管DLL
  • DLL只是不导出该名称的函数
  • DLL导出该函数,但名称被修饰或损坏
  • 诊断故障的最简单方法可能是使用Dependency Walker之类的工具检查DLL的导出

    更新


    从编辑到问题,很明显第1项是原因。您的DLL是托管DLL,尝试使用p/invoke访问它是不正确的。只需将其作为控制台应用程序项目的引用添加即可。

    要动态加载DLL,不确定它是否适用于托管DLL,请使用Assembly.LoadFrom()从制度上反思


    要创建实例,请使用Activator.CreateInstance()从System.Activator;

    是您的DLL吗?您是否正确导出了
    Init
    函数?是
    DLLTest.DLL
    a.Net程序集?什么类型的DLL?COM?Native?.Net?运行时似乎发现
    DLLTest.DLL
    很好。错误似乎是导出了
    Init
    函数。您可以发布本机数据吗这些函数的定义?这可能是由于缺少信息而导致错误的地方。我已经编辑了它-添加了DLL。它也是用c#编写的。是的,第1项是解释。David是正确的,托管DLL不会像非托管DLL那样导出符号。请在项目中引用DLL,并像使用其他托管DLL一样使用它很好。