在C#中,想调用其他开发环境的DLL内的函数

来源:互联网 发布:windows xpe 下载 编辑:程序博客网 时间:2024/06/10 01:44

C#,想调用其他开发环境的DLL内的函数 

 

方法一:(Excel为例,调用Com对象)  
 
第一步:  
 
在项目的引用节点单击右键,在弹出的菜单中单击添加引用...  
 
在弹出的对话框中单击COM项,找到Microsoft   Excel   11.0   Object   Library  
 
并单击选中,然后单击选择按钮。  
 
第二步:  
 
引用Excel2003后,会在引用节点中增加Excel等节点,也就是把Com生成了.net能理解的程序集。  
 
进入项目的bin/Debug目录可以看到,相应的增加了  
  Interop.Excel.dll
等库文件,它们都是.Net   COM   类型库中的  
 
类型定义转换为.Net公共语言运行库程序集中的等效定义生成的文件。  
   
   
 
方法二:  
 
以上是可视化自动生成com对应的程序集,也可以用如下的方法生成。  
 
打开Visual   Studio   .Net2003命令提示,运行  
  TlbImp  
你的原来dll   命一个新的名字.dll

然后在工程的引用节点添对命一个新的名字.dll”的引用,就可以使用了。

 

还有一种引用API函数(using System.Runtime.InteropServices

  [DllImport("kernel32")]

  private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

  [DllImport("kernel32")]

 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

例:

StringBuilder temp = new StringBuilder(200);

string FileName = "C://desck.ini";//NI文件的完整的路径和名称。

 string section = txtRegedit.Text;//INI文件中的段落

 string key = txtuser.Text;//INI文件中的关键字

 string keyValue = txtpwd.Text;//INI文件中的关键字

  int i = GetPrivateProfileString(section, key, "无法读取对应数值!", temp, 200, FileName);//判断是否注册过

  if (temp.ToString() == "无法读取对应数值!")

   {

    WritePrivateProfileString(section, key, keyValue, FileName);

     MessageBox.Show("注册成功写入INI文件!", "信息");

   }

   else

    {

       MessageBox.Show("此信息已注册过了");

    }

原创粉丝点击