惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - jasonCao

遗传算法在自动组卷中的应用 Windows驱动开发,几个BLOG值得经常看看 IIS的ISAPI接口简介 文件过滤系统驱动开发Filemon学习笔记 Windows平台内核级文件访问 Windows文件系统过滤驱动开发教程(11) Windows文件系统过滤驱动开发教程(10) Windows文件系统过滤驱动开发教程(9) Windows文件系统过滤驱动开发教程(8) Windows文件系统过滤驱动开发教程(7) Windows文件系统过滤驱动开发教程(6) Windows文件系统过滤驱动开发教程(5) Windows文件系统过滤驱动开发教程(4) Windows文件系统过滤驱动开发教程(3) Windows文件系统过滤驱动开发教程(2) Windows文件系统过滤驱动开发教程(0,1) Windows 文件过滤驱动经验总结(转) GMGuard网页防篡改保护系统 从底层了解ASP.NET体系结构 - jasonCao 编写高性能web应用程式的10个技巧_asp.net技巧
Delphi调用C#dll的问题
jasonCao · 2011-08-26 · via 博客园 - jasonCao

转载:

   近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题。在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行:
    编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目TestDll,
using System.Runtime.InteropServices;
namespace TestDll
{
     public   interface  I TestClass
     {
       void YourProcedure(stirng param1);
    }
   [ClassInterface(ClassInterfaceType.None)]
    public   class TestClass:I TestClass
     {
       public void YourProcedure (stirng param1);
        {    //自己的代码    }
    }  
}
完成之后,设置项目的属性“Make assembly COM-Visible”为选中状态。编译之后得到 TestClass.dll,把此dll放到Delphi主程序目录下。打开vs2005自带的工具“Visual Studio 2005命令提示”,输入
Regasm  路径\TestClass.dll 向系统注册此dll。

Delphi程序调用此Dll方式有两种:
一、打开vs2005自带的工具“Visual Studio 2005命令提示”,输入 TlbExp  路径\TestClass.dll 得到一个TestClass.tlb 文件。打开Delphi,选择“Project”--“import type library”找到刚才的TestClass.tlb,点击 CreateUnit,向delphi中引入一个com接口。
delphi 调用代码如下:
  var aClass: TestClass;
  begin
    aClass : =  CoTestClass.Create;
    aClass. YourProcedure ('参数');
  end;
二、不需生成tlb文件,仿照调用Excel的方式。代码如下:
 var aClass: Variant;
begin
  aClass:= CreateOleObject('TestDll.TestClass');
  aClass.YourProcedure ('参数');
end;

以上两种方法都可以调用成功,其中调用regasm.exe向系统注册dll是必需的。第一种方法需要生成tlb文件,并引入delphi中,操作繁琐,但可以看到接口的定义。第二种方法操作简单,但看不到接口的定义。
==============================================================
本人用第二种方法已正常实现功能,但DLL中一些自动创建的方法无法在外部直接调用,需要DLL准备特定的接口函数

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/genispan/archive/2009/06/24/4294487.aspx

最近公司进行产品改造,由于存在部分代码是用delphi写的Com+需要调用C#写的Dll方法,经过网上一些搜索和摸索,调用已经OK。但是至于两者之间的事务问题还需要用到的朋友深入研究。
    现在来说一下调用过程:【本地调试通过,环境是XP,delphi7.0,vs2005】
    首先在vs2005中创建一Class Library项目,添加2个cs文件,代码分别为:
 声明一个接口
 1Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下using System;
 2Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下using System.Collections.Generic;
 3Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下using System.Text;
 4Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下namespace beep_Class
 5{
 6    public interface IGO
 7    {
 8        string GO();
 9    }
10Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下}实现该接口
 1Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下using System.Runtime.InteropServices;
 2Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下namespace beep_Class
 3{   
 4    [ClassInterface(ClassInterfaceType.None)]
 5    public class Class1:IGO
 6    {
 7
 8        public string GO()
 9        {
10            return "aaaaabbbb";
11        }
12    }    
13Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下}然后在生成类库之前设置一下该项目的属性,如下图所示:
Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下
注意红线标示的部分。
然后对此编译成功的DLL【beep_Class.dll】进行处理,打开vs2005自带的命令行工具。输入 tlbexp beep_Class.dll
生成 beep_Class.tlb文件。
Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下元宝注解:
Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下这一步有个更好的方法,即在VS2005中DLL的“项目属性”下“生成事件”添加“生成后事件命令行”:
Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\tlbexp" "$(TargetPath)"
下一步是打开delphi7,新建一个Application,在Form上增加一Button。然后选择Project下的,import type library,把刚才生成的Tlb文件【beep_Class.tlb】添加进来,然后点击 CreateUnit就ok了
delphi中的Button事件代码如下:
 1Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下procedure TForm1.Button1Click(Sender: TObject);
 2Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下var
 3Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下  co:Class1;
 4Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下  a:string;
 5Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下begin
 6Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下  co:= CoClass1.Create;
 7Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下  a:=co.GO();
 8Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下  showmessage(a);
 9Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下end;
10Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下end.编译通过,运行结果如下图:
Delphi调用Cdll的问题 - 炎火 - 千里之行始于足下
注意:运行的时候要把Beep_Class.dll放在程序目录中。