c#只能調用com+的dll,所以:
1 用delphi建立一個com+ dll
2 注冊dll
3 c#調用.
1 用delphi建立一個com+ dll
2 注冊dll
3 c#調用.
解决方案 »
- 带参数启动C#程序,命令行当前不在程序所在目录就会启动失败
- 在线等候,跪求赐教!!!!!!!!
- (求助)不能把基类的地址指向派生类吗?写了段代码研究多态性,但总是报错,帮忙看看。
- 错误“_Default”并不包含“bind”的定义 是什么问题 应该怎么改
- var的作用
- 什么是数据结构!
- 简单,所以分少
- Winform DataGrid添加Combobox的问题 ,难,难,难
- Microsoft Visual Studio .NET 2005在哪里下载?
- 我想用c#三层结构的oa换c#三层结构的全站程序学习。
- 一启动程序抓到错误,提示后,如何不再让程序继续运行,马上关闭
- 关于随机数问题,送分100!!!
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();
};
我用了你说的方法试着调用delphi做的dll,
但程序执行时会报错说 无法找到该dll中add方法的入口点,
这是为什么呢?
以下是代码:
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());
}
}
}
请你帮忙看看,谢谢!
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包含的路径?
你们的dll都不是用delphi的new->others->activex里生成的吧!
我从没试过自己写dll,所以不敢随便修改delphi生成的代码。
修改也是可以的吧?Knight94(愚翁),把demo发到这吧:[email protected]。谢谢!
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???
可我还有些不明白为什么带类型库的dll不可以这么调用呢?
不过不管怎么说都谢谢大家了!