我不是只要在c#里调用,而是要在所有语言中都适用,就像系统的API. 如果要在c#里调用的话,要的效果是用这种方式来调用: [DllImport(@"E:\My Documents\Visual Studio 2008\Projects\实验\MyDLL\MyDLL\bin\Debug\MyDLL.dll")] static extern int ADD(int a, int b); 而不是using XXX;
做成com、activex、webservice做成com调用 http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.htmlYou were definitely closer with your second attempt as you can't load a .NET assembly as if it was a normal C-style DLL. If you're getting "Class not registered" error messages in Delphi it simply means the required registry entries were not available.To use your C# class as a COM object, the best thing to do is put all your methods in one or more interfaces then make the class implement each of those interfaces. Example: // IAddInterface.cs public interface IAddInterface { int Add(int i1, int i2); }// SimpleDll.cs using System.Runtime.InteropServices;[ClassInterface(ClassInterfaceType.None)] public class SimpleDLL : IAddInterface { public int Add(int i1, int i2) { return i1+i2; } }Compile using: csc /t:library /out:SimpleDll.dll *.csNext, register the assembly as a COM object in the registry using: regasm SimpleDll.dll /tlb:Add.tlb(The above command will also generate a type library for use in Delphi.)Now you can switch over to Delphi, and click Project->Import Type Library (like you did before). Select your .tlb (Add.tlb in this case) and click Create Unit.In your main unit add "ComObj" and "SimpleDLL_TLB" (or whatever your generated unit is called) to your uses list and write the code to call your .NET methods. Example:procedure TForm1.Button1Click(Sender: TObject); var intfRef: IAddInterface; result: Integer; begin intfRef := CreateComObject(CLASS_SimpleDLL_) as IAddInterface; result := intfRef.Add(2, 2); . .For it to work your Delphi .exe needs to be in the same directory as the .NET DLL.
用c#能不能创建动态链接库DLL供外部程序调用 这个应该可一二
你把DLL当成自己的API就行了
我要提供给别的程序使用,就像你想做在使用api一样.不是在内部使用.
或者理解成时我想用c#写一个[user32.dll]之类的东西.
在其他项目引用DLL。using 命名空间
看看petshop
我不是只要在c#里调用,而是要在所有语言中都适用,就像系统的API.
如果要在c#里调用的话,要的效果是用这种方式来调用: [DllImport(@"E:\My Documents\Visual Studio 2008\Projects\实验\MyDLL\MyDLL\bin\Debug\MyDLL.dll")]
static extern int ADD(int a, int b);
而不是using XXX;
那你说说,比如是c吧,怎么调用c#写出了的dll呢.
非托管代码可以调用托管DLL 不过还是要依赖CLR的在windows平台下 使用win api 加载托管DLL和动态加载非托管DLL 类似
是这样的,我想做一个DLL文件,里边就一个函数,供各种语言的程序员在编程时直接进行调用.
请问最方便最简单的方式是什么呢?优先考虑用c#来写这个文件.实在不行再去玩c++.
http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.htmlYou were definitely closer with your second attempt as you can't load a .NET assembly as if it was a normal C-style DLL. If you're getting "Class not registered" error messages in Delphi it simply means the required registry entries were not available.To use your C# class as a COM object, the best thing to do is put all your methods in one or more interfaces then make the class implement each of those interfaces.
Example:
// IAddInterface.cs
public interface IAddInterface
{
int Add(int i1, int i2);
}// SimpleDll.cs
using System.Runtime.InteropServices;[ClassInterface(ClassInterfaceType.None)]
public class SimpleDLL : IAddInterface
{
public int Add(int i1, int i2)
{
return i1+i2;
}
}Compile using: csc /t:library /out:SimpleDll.dll *.csNext, register the assembly as a COM object in the registry using: regasm SimpleDll.dll /tlb:Add.tlb(The above command will also generate a type library for use in Delphi.)Now you can switch over to Delphi, and click Project->Import Type Library (like you did before). Select your .tlb (Add.tlb in this case) and click Create Unit.In your main unit add "ComObj" and "SimpleDLL_TLB" (or whatever your generated unit is called) to your uses list and write the code to call your .NET methods. Example:procedure TForm1.Button1Click(Sender: TObject);
var
intfRef: IAddInterface;
result: Integer;
begin
intfRef := CreateComObject(CLASS_SimpleDLL_) as IAddInterface;
result := intfRef.Add(2, 2);
.
.For it to work your Delphi .exe needs to be in the same directory as the .NET DLL.
强命名DLL 就可以像System.dll一样,但如果在其他计算机上运行,需要复制本地=true