C#中怎样引用c编写的dll

C#中怎样引用c编写的dll?我现在需要引用一个c编写的dll,怎么做?

使用C#生成dll文件并调用
一、创建dll文件:

例如生成一个md5编码判断状态的文件,即,输入一个字符串(string A)和一个32位md5编码(string B),判断此字符串A对应的32位md5编码是否与B相等,如果相等返回true,否则返回false。

打开VS 2005,“文件”--》“新建”--“项目”,选择“Windows 控件库”,命名后点击“确定”,在“UserControl1.cs”中输入以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using System.Text;
using System.Security.Cryptography;

namespace md5
{
public partial class Program : UserControl
{
#region MD5 32位加密:GetMd5Str32
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <returns>加密后的字串</returns>
public static string GetMd5Str32(string strSource)
{
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
StringBuilder sb = new StringBuilder();

for (int i = 0; i < 16; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}

return sb.ToString().ToUpper();
}
#endregion

#region 核对md5编码是否一致:CheckMd5String()

/// <summary>
/// 核对md5编码是否一致
/// </summary>
/// <param name="ConvertString"></param>
/// <returns>如果一致返回true,否则返回false</returns>
///
public static bool CheckMd5String(string str1, string str2)
{
string md5String = str1; //需要验证的字符串
string md5DbString = str2; //需要核对的32位md5编码

int result = string.Compare(md5.Program.GetMd5Str32(str1), md5DbString, true);
if (result == 0)
{
return true;
}
else
{
return false;
}
}
#endregion
}
}

修改“UserControl1.Designer.cs”中的命名空间为“md5”,方法为“Program”,即可生成dll文件。

在...\bin\Debug文件假下,可以找到相应的dll文件。

二、部署dll流程:

首先把dll文件放到应用程序...\bin\Debug\下;
然后在解决方案中添加引用:右键鼠标-->添加引用-->浏览-->选择dll放置路径后点击“确定”。
注意:要在应用文件头处使用using md5;命令。

测试应用程序代码,如下:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using md5;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string str1 = textBox1.Text.ToString();
string md5String = textBox2.Text.ToString();

textBox3.Text = md5.Program.GetMd5Str32(str1);
textBox4.Text = md5.Program.CheckMd5String(str1, md5String).ToString();

}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

三、注意点:

1、在C#应用程序开发过程中,加载dll文件时,报错“未能加载文件或程序集“md5, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。”,请指点一下是什么原因?
解决:这是因为加载dll的路径问题,正确加载方式为:在“解决方案”的“引用”文件上右击鼠标,选择“添加引用”---》在“浏览”选项卡中添加引用(注意:自己定义的dll文件不能在“.NET”选项卡中添加。)

------------------------------------------------------------------------------------------------------------------

c#生成DLL文件,内部函数的问题

用C#编写一组处理XML文档的代码,由于要求生成DLL文件,并由外部的其他工具访问动态库中的文件,
但是用Dependency Walker检测我生成的这个DLL文件没有显示任何的函数,以前没做过这方面的东西,求教了

代码如下:
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
DeleteArg();
}
static void DeleteArg()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\\data1.xml");
XmlNode root = doc.DocumentElement;
XmlNode Node1;
XmlNodeList nodeList = doc.SelectSingleNode("/Entity/Columns").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "SysModuleID")
{
xe.RemoveAll();
//xe.RemoveAttribute("Name");//删除Name属性
}
}
doc.Save("c:\\data1.xml");//保存这个文档到文件中
}
}

以上代码实现删除XML文件中某一节点的功能,如何在生成DLL后能够使用检测工具检测出DeleteArg函数,
使用Dependency Walker没检测出该函数是不是以为着这个动态库文件不能被调用.
----
因为.net的程序不是这样把函数放在导出表的, 我记得.net做的dll只导出了一个_CorDllMain的方法,
所以用Dependency Walker是看不出来的. 如果你想看.net做的dll导出了什么内容,可以用反射查看元数据
----
生成这个DLL库文件,是想要别的工具运行这个动态库文件,实现DELETEARG()这个函数的功能
----
可以的
----
你上面的代码不是生成DLL的,而是一个控制台应用程序.

要想创建动态库(DLL),在新建项目窗口中选择"类库", 默认的代码是这样的:

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

namespace ClassLibrary2
{
public class Class1
{
}
}

// 然后添加你的代码.最后代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ClassLibrary2
{
public class Class1
{
public void DeleteArg()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\\data1.xml");
XmlNode root = doc.DocumentElement;
XmlNode Node1;
XmlNodeList nodeList = doc.SelectSingleNode("/Entity/Columns").ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "SysModuleID")
{
xe.RemoveAll();
//xe.RemoveAttribute("Name");//删除Name属性
}
}
doc.Save("c:\\data1.xml");//保存这个文档到文件中
}
}
}

最后编译一下就可以,
在Debug文件夹下回产生一个dll文件,最后在需要的工程里,将这个dll文件引进进去就可以用.
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-08-18
众所周知C#具有容易上手快速开发的特点,所以是短平快项目不错的选择。笔者长期从事嵌入式系统开发,积累了一些c语言算法。通过dll使C#很容易直接调用c语言写成的算法。下面简单描述这个过程。

1、创建dll。windows平台下有很多c编译器,这里选择小巧的LCC-WIN32。

编写sub.h:

#ifndef
__SUB_H

#define
__SUB_H

int
__stdcall sub(int a, int b);

#endif //
__SUB_H

编写sub.c:

#include

#include

#include "sub.h"

int sub(int a, int b)
{

return
a-b;

}

编写sub.def:

LIBRARY SUB

EXPORTS

sub
@1

编译链接生成sub.dll

lcc sub.c

lcclnk -dll -nounderscores
sub.obj sub.def

2、C#中调用dll。在VS2008中新建一个C#的控制台程序。将生成的sub.dll放入新建工程bin目录下。

using
System.Runtime.InteropServices;

namespace ConsoleApplication1

{

class Program

{

[DllImport("sub.dll")]

public static extern int
sub(int a, int b);

static void Main(string[] args)

{

Console.WriteLine(sub(10, 3));

Console.WriteLine("Helloworld!");

Console.ReadKey();

}

}

}
第2个回答  2015-09-07

C#中引用c编写dl代码l:

#include "stdafx.h"
#include "myDll.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

void HelloWorld()
{
    MessageBox(NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

编译成功!接下来我们便可以添加一个C#控制台项目了

     C#测试代码如下:

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

namespace TestDll
{
    class Program
    {
        [DllImport("myDll.dll")]
        public extern static void HelloWorld();

        static void Main(string[] args)
        {
            HelloWorld();
        }
    }
}

第3个回答  推荐于2017-10-03
添加相应的DLL引用,在VS里面选择“项目”-“添加引用”-“浏览”,就可以添加你想用的DLL了,之后在程序里面即可使用。本回答被提问者采纳
第4个回答  2009-04-27
直接可以添加后就可以引用的。“项目”-“添加引用”-“浏览”
相似回答