当前位置:编程学习 > C#/ASP.NET >>

请教C#调用DLL的简单例子

VS2008下用C#如何调用DLL呢?如动态库名为TEST。DLL
在按钮 private void button1_Click(object sender, EventArgs e)
        {
        
            Close();
        }
下如何使用动态库中函数呢?如函数为 test1,如何调用。多谢详细告之
--------------------编程问答-------------------- 首先添加对test.dll的引用,然后import命名空间,然后就可以test1()了。 --------------------编程问答--------------------

[DllImport("test.dll")]
publc static extern void test1();


函数要位于当前目录中。 --------------------编程问答--------------------
引用 2 楼 phy 的回复:
C# code[DllImport("test.dll")]
publcstaticexternvoidtest1();

函数要位于当前目录中。
--------------------编程问答-------------------- [DllImport("test.dll")]
publc static extern void test1();
--------------------编程问答-------------------- test.dll是托管還是非托管的? --------------------编程问答-------------------- 都可以的吧 --------------------编程问答--------------------
C# code
[DllImport("test.dll")]
publc static extern void test1();




函数要位于当前目录中。
--------------------编程问答--------------------
引用 7 楼 clxcxx 的回复:
C# code 
[DllImport("test.dll")] 
publc static extern void test1(); 


函数要位于当前目录中。 

--------------------编程问答-------------------- 得先引用才行的 --------------------编程问答-------------------- 倒,大家说的都很深奥哦 --------------------编程问答-------------------- 1.首先添加对test.dll的引用,然后import命名空间,然后就可以test1()了。
2.[DllImport("test.dll")] 
   publc static extern void test1();  --------------------编程问答--------------------

//Assume you want to dynamic reference the dll, you can do it through reflect

Assembly assembly = Assembly.LoadFrom( //Fill with the absolute path of dll.
                Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"ClassLibrary1.dll"));
            Type t = assembly.GetType(//Fill with the class name.
                "ClassLibrary1.Class1");
            object o = Activator.CreateInstance(t, null);
            t.InvokeMember(//Fill with the method you want to invoke.
                "Test", BindingFlags.InvokeMethod,
                null, o, null);
--------------------编程问答-------------------- 直接在MSDN里查 PInvoke --------------------编程问答-------------------- 楼上们说的正解 --------------------编程问答-------------------- 添加引用 --------------------编程问答-------------------- 还是没有看明白
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,