如何用 C# 编写 DLL 或 COM 以便 DELPHI 可以调用? ( 积分: 100 )

B

bundur

Unregistered / Unconfirmed
GUEST, unregistred user!
如题?
急!
 
如题?
急!
 
很不幸,C#编写的DlL,Delphi是不能调用的。因为C#的代码是在托管的。
Delphi写的DLL如果C#想调用还需要跟C#的类型匹配。
 
DELPHI 可以通过编写成 COM 给 C# 使用,
C# 可不可以编写标准 COM 呢?
 
C#编写的DlL 注册 成COM delphi是可以调用的
具体参考一下相关的资料
 
愿闻其祥!
其实,我只是想利用 Microsoft.VisualBasic 命名空间中的
Strings.StrConv 进行繁简转换, 如能直接调用则更佳!
 
.NET的DLL可以调用COM.也可以从COM调用.
Delphi开发的COM就可以调用.NET的DLL.
 
to: wr960204 能否举个例?
多谢了
 
在你的环境变量Path中加入
C:/Program Files/Microsoft Visual Studio .NET 2003/SDK/v1.1/Bin;
C:/WINNT/Microsoft.NET/Framework/v1.1.4322

随便建立一个ClassLibrary编译成DLL文件
切换到MS-DOS命令行下面,运行:
C:/>regasm out ClassLibrary1.DLL /regfile:ClassLibrary1.reg
上面的命令行是注册我们的.NET组件,并且产生了一个备用的注册表文件。对应Windows的注册COM文件命令:regsvr32 c:/test.dll .
在.NET下面,注册.NET组件就需要上面的regasm命令了
然后在MS-DOS命令行下面,运行:
C:/>tlbExp ClassLibrary1.dll /out:ClassLibrary1.tlb
上面的命令行表示将会产生一个.NET组件的类型库
有了TLB文件也就和正常的COM组件一样了,运行Delphi导入类型库文件TLB......
和正常的COM调用一摸一样
 
To: wr960204
老大, 导出的 ClassLibrary1.tlb 看不到写在里边的方法呀!
 
可能要做特殊处理,前两天看 Delphi.NET的资料,说除Delphi.net及VC.NET生成的DLL可导出到WIN32外,其他的都不行。
下次帮你查查看具体是怎么说的。
 
.NET的DLL作为COM导出是有很多限制的,因为COM的表现力远不及.NET灵活。
这种应用其实用C++.NET更合适,很容易封装.
我需要看看
 
这个功能是通过kernel32.LCMapString来实现的,[ http://tech.99soft.net/h/2004-4-1/106948.htm ]可以参考.没必要因此引入.NET的依赖.
这个应用其实用DELPHI.NET更合适.
 
多谢 zjan521
你所贴的参考代码,我有用 DELPHI 实现过,只是不如 Strings.StrConv 好用,
因为使用 Strings.StrConv 如果你输入与你想要转的代码页一致,致辞它会原样
输出,不会转乱的,因为我的输入事先不知道它是不是繁体,还有GBK,可我想让
它给我输出的一定是 936 的中文。所以很烦恼。
我知道可以用 WORD 去转效果和 Strings.StrConv 转一致,可以调用 WORD 很慢,
而且还需要用户安装 WORD 。如有更好的办法,希望大家不吝多多指点。
 
假设你的C#源文件为abc.cs
1. sn.exe -k "abc.snk" //产生強名文件
2. csc.exe /target:library /keyfile:abc.snk abc.cs //帶强名文件编译成装配件
3. gacutil.exe /i abc.dll //安裝到全局高速缓存中
4. regasm.exe abc.dll //注册装配件
5. tlbexp.exe abc.dll    //导出类库abc.tlb
6. 在Delphi中导入类库abc.tlb就可以调用了。
 
你可以用DELPHI.NET来实现,这个是最简单的.在D9的DEMO当中,就有一个例子,生成一个DELPHI.NET的DLL,并且用export导出函数,然后在Win32下边直接调用.
 
TO: zjan521,
请指点一二, 今天特地安装了 D9 没有找到您说的那个DEMO, 还望详细说明及用法.
多谢!
 
你要安装的是完整版的D2005,因为那个DEMO是DELPHI.NET的。
不过其实很简单,建一个DELPHI.NET的DLL,然后已经可以访问.NET的类库(添加你需要的引用),然后就可以向Win32一样,在DPR中声明一个函数,并且导出.这样就可以。
 
我写了这样一个 DLL
但是不能在 WIN32 的 Delphi 程序里调用,请帮助看看——
library DNDllTest;

{%DelphiDotNetAssemblyCompiler '$(SystemRoot)/microsoft.net/framework/v1.1.4322/Microsoft.VisualBasic.dll'}
uses
SysUtils,
Classes,
System.Reflection,
System.Runtime.InteropServices,
Microsoft.VisualBasic;
[assembly: AssemblyTitle('')]
[assembly: AssemblyDescription('')]
[assembly: AssemblyConfiguration('')]
[assembly: AssemblyCompany('')]
[assembly: AssemblyProduct('')]
[assembly: AssemblyCopyright('')]
[assembly: AssemblyTrademark('')]
[assembly: AssemblyCulture('')]
//
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion('1.0.*')]
//
// In order to sign your assembly you must specify a key to use. Refer to the
// Microsoft .NET Frameworkdo
cumentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing.
//
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your machine. KeyFile refers to a file which contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyNamedo
es not exist and the KeyFiledo
es exist, the key
// in the KeyFile is installed into the CSP and used.
// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile should be
// relative to the project output directory. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile('mykey.snk')], provided your output
// directory is the project directory (the default).
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// do
cumentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile('')]
[assembly: AssemblyKeyName('')]
//
// Use the attributes below to control the COM visibility of your assembly. By
// default the entire assembly is visible to COM. Setting ComVisible to false
// is the recommended default for your assembly. To then
expose a class and interface
// to COM set ComVisible to true on each one. It is also recommended to add a
// Guid attribute.
//
[assembly: ComVisible(False)]
//[assembly: Guid('')]
//[assembly: TypeLibVersion(1, 0)]

{$UNSAFECODE ON}
function StrConvert(InStr: string): string;
export;
begin
Result := Strings.StrConv(InStr, VBStrConv.SimplifiedChinese);
end;

exports
StrConvert;

begin
end.
 
身边没有环境,下午我看一下.
首先你写一个简单的函数比如
function DllRetInt(int inVal) : Integer;
begin
Result:= inVal;
end;
测试一下,我想应该可以进行操作的.
至于你所说的不能使用,我想可能是类型的问题.在DELPHI.NET中的string应该已经是.NET的System::String,这个和Win32_DELPHI的string不是一回事.
虽然肯定看到并运行了那个例子,但是没有具体使用过,因此细节我还不是很确定
 
顶部