c写dll怎么调用

1.C语言如何调用DLL//dll
#include <windows.h>
extern "C" //保持C语言文件
void _declspec ( dllexport ) tryProc() //定义函数
{
MessageBox(NULL,_T("a"),_T("a"),MB_OK);
}-------------------------------------
//C
#include <windows.h>
int main()
{
HMOUDLE dll = LoadLibrary(/*DLL文件名*/);
if(dll != NULL)
{
FARPROC try = GetProcAddress(dll,"tryProc");
if(try != NULL)
{
tryProc();//假如函数有返回值,可以用try()得到返回值
/*_asm call tryProc*/
}
}
return 0;
}
2.C程序如何调用dll 文件用的IDE不一样,生成DLL文件的方法也有差异,建议上网查找你所用的IDE生成DLL文件的方法 。
***********************************************
这个关键是IDE(比如是VC++还是BCB),C语言在编DLL和其它的程序时都用相同的规则 。
***********************************************
看不懂建议暂时就不要自己做了,在这里说和你看网上资料是一样的,而且这里可以说的内容还不及网上多呢 。
3.C#中怎样引用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/// /// 32位MD5加密/// /// 待加密字串/// 加密后的字串 public static string GetMd5Str32(string strSource) { byte[] bytes = Encoding.ASCII.GetBytes(strSource); byte[] hashValue = http://www.xuexi88.com/zhishi/((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i { sb.Append(hashValue[i].ToString("x2")); } return sb.ToString().ToUpper(); }#endregion#region 核对md5编码是否一致:CheckMd5String()/// /// 核对md5编码是否一致/// /// /// 如果一致返回true,否则返回false/// 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”或它的某一个依赖项 。系统找不到指定的文件 。”