delphi 调用指定路径的C# dll

genispan 2009-07-29 03:47:09
C#的dll已经用Regasm指令注册过了
现在情况是 dll必须和exe在同一个目录下, 才可以正常调用
不在同一目录下 就提示找不到文件
调用dll的代码如下:

var
aClass: Variant;
begin
CoInitialize(nil); ;
aClass:= CreateOleObject('NewProject.Test');
aClass.ShowForm;
aClass:=Unassigned;
CoUnInitialize;
end;

有哪位高人知道 麻烦指点一下 谢谢~
...全文
363 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
genispan 2009-07-30
  • 打赏
  • 举报
回复
UP 召唤达人!!
genispan 2009-07-30
  • 打赏
  • 举报
回复
还是没有搞定 哎 结贴算了 如果谁知道的 可以接着留言 本人可以另开贴给分 O(∩_∩)O哈哈~
genispan 2009-07-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 gyk120 的回复:]
如果实在不行的话,把DLL作为资源形式加到EXE里面,运行的时候释放到当前目录加载……
[/Quote]
会楼上 如果这样的话 dll就没有存在的意义了
gyk120 2009-07-29
  • 打赏
  • 举报
回复
如果实在不行的话,把DLL作为资源形式加到EXE里面,运行的时候释放到当前目录加载……
wxsan 2009-07-29
  • 打赏
  • 举报
回复
帮忙顶一下!
zjny520 2009-07-29
  • 打赏
  • 举报
回复
微软做得也太差了吧,继续跟贴
genispan 2009-07-29
  • 打赏
  • 举报
回复
召唤达人来帮解决问题啊 ~ 着急啊
yhf365 2009-07-29
  • 打赏
  • 举报
回复
果然很打击人,
copy那几个dll过来吧~
genispan 2009-07-29
  • 打赏
  • 举报
回复
You 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:
Code:
// IAddInterface.cs
public interface IAddInterface
{
int Add(int i1, int i2);
}Code:
// 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:
Code:
csc /t:library /out:SimpleDll.dll *.cs
Next, register the assembly as a COM object in the registry using:
Code:
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:
Code:
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.
=============================
国外论坛上找的 最后一句话 太打击人了 ~~~~~~ 郁闷
ysai 2009-07-29
  • 打赏
  • 举报
回复
那就不知道什么原因了
按照自动化COM对象的用法,即使是用脚本也能调用这个对象,而不管你DLL与vbs文件是不是放一起.

可以重新建个对象,写一个最简单的方法,show个messagebox测试一下
genispan 2009-07-29
  • 打赏
  • 举报
回复
回楼上:
注册表里的路径是对的啊
ysai 2009-07-29
  • 打赏
  • 举报
回复
是COM对象?
那注册时应该把DLL路径写到注册表中了啊,找找是不是正确的路径
zjny520 2009-07-29
  • 打赏
  • 举报
回复
沙发,我想下哦,会陆续跟贴哦

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧