c#只能調用com+的dll,所以:
1  用delphi建立一個com+ dll
2  注冊dll
3  c#調用.

解决方案 »

  1.   

    如果都是标准Dll话,那么调用DELPHI做的DLL和调用VC做的DLL是一样的,用DllImport。
      

  2.   

    As follows:
    using System.Runtime.InteropServices;
    using System.Text;class DllSample
    {
    [DllImport("DllSample.DLL", EntryPoint="Add",  SetLastError=true,
    CharSet=CharSet.Ansi,
    CallingConvention=CallingConvention.StdCall)]
    public static extern int Add(int a,int b); [DllImport("DllSample.DLL", EntryPoint="ShowString",  SetLastError=true,
    CharSet=CharSet.Ansi,
    CallingConvention=CallingConvention.StdCall)]
    public static extern StringBuilder ShowString();
    };
      

  3.   

    以上是调用一个用VC写的Dll,Dll文件与程序在同一个目录。
      

  4.   

    谢谢 Knight94(愚翁) 先!
    我用了你说的方法试着调用delphi做的dll,
    但程序执行时会报错说 无法找到该dll中add方法的入口点,
    这是为什么呢?
      

  5.   

    你在如何声明dll函数入口的?最好把相关的代码贴出来看看。
      

  6.   

    我用delphi的type library添加的adder方法,
    以下是代码:
    unit Unit_com_datawebgis;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
      Windows, ActiveX, Classes, ComObj, Prj_com_datawebgis_TLB, StdVcl;type
      TComDataWebGIS = class(TTypedComObject, IComDataWebGIS)
      protected
        function Adder(Param1, Param2: Integer; out rslt: Integer): HResult;
          stdcall;
        {Declare IComDataWebGIS methods here}
      end;implementationuses ComServ;function TComDataWebGIS.Adder(Param1, Param2: Integer;
      out rslt: Integer): HResult;
    begin
      rslt:=Param1+Param2;
    end;initialization
      TTypedComObjectFactory.Create(ComServer, TComDataWebGIS, Class_ComDataWebGIS,
        ciMultiInstance, tmApartment);
    end.
    然后在C#中调用的代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;namespace DelphiComTest
    {
    public class MyCom
    {
    [DllImport("E:\\我的文档\\My Projects\\Visual C#\\DelphiComTest\\Prj_com_datawebgis.dll",EntryPoint="Adder"
     ,SetLastError=true,CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] 
    public static extern int Adder(int Param1,int Param2,out int rslt);
    }

    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    //MyCom test=new MyCom();
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //

    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(192, 88);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    int tmp;
    MyCom.Adder(3,4,out tmp);
    MessageBox.Show(tmp.ToString());
    }
    }
    }
    请你帮忙看看,谢谢!
      

  7.   

    你这个Adder是类的函数吧,还有为什么用protected这个关键字呢?
      

  8.   

    我先做了一个Delphi的普通DLL,如下:
    library Project1;
    uses
      SysUtils,
      Classes;{$R *.res}function Add(i, j: integer): integer; stdcall;
    begin
      Result := i + j;
    end;exports
      Add;begin
    end.而后用C#写了一个调用这个DLL的小例子,如下:
    using System;
    using System.Runtime.InteropServices;namespace ConsoleApplication16
    {
    class Class1
    {
    [DllImport("Project1.dll")]
    public static extern int Add(int i, int j);
    [STAThread]
    static void Main(string[] args)
    {
    int sum = Add(1, 2);
    Console.Write(sum.ToString());
    Console.Read();
    }
    }
    }
    把Delphi写的DLL与C#编译出来的可执行程序放在一个目录下面,这样就没有问题啦,我想你是不是没有把DLL和C#写的程序放在一个目录下?或者不是Path包含的路径?
      

  9.   

    我也做了一个例子,用delphi写了个Dll,然后用c#调用没有问题,是否你没用exports来提供你dll的函数接口?如果想要Demo,可以通知我。
      

  10.   

    to Knight94(愚翁),daemonking(daemonking):
    你们的dll都不是用delphi的new->others->activex里生成的吧!
    我从没试过自己写dll,所以不敢随便修改delphi生成的代码。
    修改也是可以的吧?Knight94(愚翁),把demo发到这吧:[email protected]。谢谢!
      

  11.   

    已经发了,调用如下:
    using System.Runtime.InteropServices;  [DllImport("prjDllSample.dll", EntryPoint="Adder",CharSet=CharSet.Auto, 
        ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
      public static extern int Adder(int Param1,int Param2,out int rslt); 调用:
    int nrslt;
    int nRet=Adder(1,3,out nrslt);//既然是Function,为什么还用out???
      

  12.   

    可以了!多谢  Knight94(愚翁) :)
    可我还有些不明白为什么带类型库的dll不可以这么调用呢?
    不过不管怎么说都谢谢大家了!