如何在delphi应用程序中使用我在VB.NET中创建的DLL [英] How to use a DLL I created in VB.NET in a delphi app

查看:67
本文介绍了如何在delphi应用程序中使用我在VB.NET中创建的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中创建了一个非常简单的dll。我成功注册了它(作为管理员),可以从我创建的简单vb.net WinForms应用程序中的按钮调用它。有用。它符合我的期望。但是,我尝试在我创建的一个简单的Delphi应用程序中使用它,它说它找不到它。



谢谢,



Avian



- Windows 10 Pro 64位

- VS 2017

- 德尔福东京10.2.3



我尝试过:



我的Delphi代码是:



  unit  Unit1; 

interface

使用
Winapi.Windows,Winapi.Messages,System.SysUtils,System。 Variants,System.Classes,Vcl.Graphics,
Vcl.Controls,Vcl.Forms,Vcl.Dialogs,cxGraphics,cxLookAndFeels,cxLookAndFeelPainters,
Vcl.Menus,Vcl.StdCtrls,cxButtons;

功能 CreateExcelWorkbook: boolean ; STDCALL; EXTERNAL ' KTExternalFunctions.dll';

type
TForm1 = class(TForm)
cxButton1:TcxButton;
程序cxButton1Click(发件人:TObject);
private
{ 私人声明}
public
{ 公开声明}
end ;

var
Form1:TForm1;

实施

{ $ R * .dfm}

程序TForm1.cxButton1Click(发件人:TObject);
开始
CreateExcelWorkbook;
end ;

end



Delphi代码编译但是当我尝试运行它时我收到此错误消息:



代码执行无法继续,因为找不到KTExternalFunctions.dll。重新安装程序可能会解决此问题。



我读过的所有内容都说我在使用条款中正确引用它。



唯一差别我可以看到,当我在我的vb.net应用程序中调用DLL时,我必须使用类中包含该类的类来限定它(其中KTFunctions是类的名称):



  Imports  System.Drawing 
导入 DevExpress.Spreadsheet
Imports KTExternalFunctions


公开 XtraForm1
私有 Sub SimpleButton1_Click(发件人 As Object ,e As EventArgs) Handles SimpleButton1.Click

KTFunctions.CreateExcelWorkbook()





我找不到任何关于Delphi的例子,我不是确定如何做到这一点,或者甚至是整个问题。

解决方案

R * .dfm}

程序TForm1。 cxButton1Click(发件人:TObject);
开始
CreateExcelWorkbook;
end ;

end



Delphi代码编译但是当我尝试运行它时我收到此错误消息:



代码执行无法继续,因为找不到KTExternalFunctions.dll。重新安装程序可能会解决此问题。



我读过的所有内容都说我在使用条款中正确引用它。



唯一差别我可以看到,当我在我的vb.net应用程序中调用DLL时,我必须使用类中包含该类的类来限定它(其中KTFunctions是类的名称):



  Imports  System.Drawing 
导入 DevExpress.Spreadsheet
Imports KTExternalFunctions


公开 XtraForm1
私有 Sub SimpleButton1_Click(发件人 As Object ,e As EventArgs) Handles SimpleButton1.Click

KTFunctions.CreateExcelWorkbook()





我找不到任何关于Delphi的例子,我不是确定如何做到这一点,或者甚至是整个问题。


没关系。我放弃了,发现了一种更容易生产的方式。我正在使用AtoZed的CrossTalk在Delphi中使用.net DLL。我在.net中使用EPPlus来创建我需要的Excel文件。这一切都很顺利。


好的,我知道这不是一个解决方案但是没有好的地方可以放这个。我不想把它作为改进放在O.P.因为它可能不会被注意到。如果我将其作为评论发布,它不会让我创建代码标记。所以就在这里。



之前我说我打算用控制台应用来处理这个问题。虽然这有效,但这并不是我想要的方式。我无法从控制台应用程序返回任何有价值的内容,并且调用它根本不优雅。我真的需要这个是一个dll。



所以我创造了一个更简单的。我没有注册。我可以在另一个vb.net表单应用程序中使用它很好,因为它很容易创建它的引用。



这是我的DLL代码(没有特殊的引用,只是.net dll的默认值:



 公共  Class1 
公开 共享 函数 returnJunk(inputParam As Int16
选择 案例 inputParam
案例 1
返回 one
案例 2
返回 two
案例 3
返回
案例 4
返回
案例 5
返回 five
案例 其他
返回 nothing
结束 选择
结束 功能
结束



这是我从vb.net打来的电话

 私有  Sub  SimpleButton2_Click(发件人 As   Object ,e  As  EventArgs)句柄 SimpleButton2.Click 
MessageBox.Show(TestDLLJunk.Class1.returnJunk( 14 ))
结束 Sub



在调用应用程序中,我恰到好处单击引用并浏览到已编译的dll所在的位置。它在dll的引用属性中显示了它的完整路径。



然后我在Delphi中做了同样的事情。这是在我的界面中使用:



 函数 returnJunk(inputParam:字节): string ; STDCALL; EXTERNAL '  TestJunk.dll'; 





编译我的DLL代码会创建3个文件。我将它们全部放在我的Delphi项目的源文件夹中,也放在Win32\debug文件夹中,只是为了咧嘴笑。



这是我的调用代码:

 procedure TForm1.Button2Click(Sender:TObject); 
var
rj: string ;
开始
rj:= returnJunk(4);
end ;





与之前相同。它编译但不运行。我得到了与我的OP相同的信息



然后,为了它,我注册了它:



C:\ Windows \ Mysoftoft.NET \ Framework \v4.0.30319 \RegAsm.exe D:\ Data\1Work\KT \DelphiTokyoVersion \TokyoConversionTestProjects \ TestDMLJunk \ bin \\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I've created a pretty simple dll in vb.net. I successfully registered it (as administrator) and can call it from a button in a simple vb.net WinForms app I created. It works. It does what I expect. However, I tried using it in a simple Delphi app I also created and it says it can't find it.

Thanks,

Avian

- Windows 10 Pro 64 bit
- VS 2017
- Delphi Tokyo 10.2.3

What I have tried:

My Delphi code is:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters,
  Vcl.Menus, Vcl.StdCtrls, cxButtons;

  Function CreateExcelWorkbook: boolean; STDCALL; EXTERNAL 'KTExternalFunctions.dll';

type
  TForm1 = class(TForm)
    cxButton1: TcxButton;
    procedure cxButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.cxButton1Click(Sender: TObject);
begin
  CreateExcelWorkbook;
end;

end.


The Delphi code compiles but when I try to run it I get this error message:

"The code execution cannot proceed because KTExternalFunctions.dll was not found. Reinstalling the program may fix this problem."

Everything I've read about this says that I'm referring to it correctly in my uses clause.

The only difference I can see is that when I call the DLL in my vb.net application I have to qualify it with the class that the function is contained within like this (where KTFunctions is the name of the class):

Imports System.Drawing
Imports DevExpress.Spreadsheet
Imports KTExternalFunctions


Public Class XtraForm1
    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click

        KTFunctions.CreateExcelWorkbook()



I can't find any examples of this for Delphi and am not sure how to do that or if that's even the entire problem.

解决方案

R *.dfm} procedure TForm1.cxButton1Click(Sender: TObject); begin CreateExcelWorkbook; end; end.


The Delphi code compiles but when I try to run it I get this error message:

"The code execution cannot proceed because KTExternalFunctions.dll was not found. Reinstalling the program may fix this problem."

Everything I've read about this says that I'm referring to it correctly in my uses clause.

The only difference I can see is that when I call the DLL in my vb.net application I have to qualify it with the class that the function is contained within like this (where KTFunctions is the name of the class):

Imports System.Drawing
Imports DevExpress.Spreadsheet
Imports KTExternalFunctions


Public Class XtraForm1
    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click

        KTFunctions.CreateExcelWorkbook()



I can't find any examples of this for Delphi and am not sure how to do that or if that's even the entire problem.


Never mind on this. I gave up and found an easier more productive way. I'm using AtoZed's CrossTalk to use .net DLL's in Delphi. And I'm using EPPlus in .net to create the Excel files I need. It's all working quite nicely.


Okay, I know this isn't a solution but there's no good place to put this. I don't want to put it in the O.P. as an "improvement" because it might not get noticed. And if I post it as a comment it doesn't let me create code tags. So it's here.

Earlier I said I was going to handle this with a console application. Although that works, it's not really the way I want to go. I can't return anything of value from a console application and calling it is not elegant at all. I really need this to be a dll.

So I created an even simpler one. I didn't register it. I can use it in another vb.net forms application just fine because it's easy to create a reference to it.

Here's my dll code (there are no special references, just the default ones for a .net dll):

Public Class Class1
    Public Shared Function returnJunk(inputParam As Int16)
        Select Case inputParam
            Case 1
                Return "one"
            Case 2
                Return "two"
            Case 3
                Return "three"
            Case 4
                Return "four"
            Case 5
                Return "five"
            Case Else
                Return "nothing"
        End Select
    End Function
End Class


Here's my call to it from vb.net

Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click
    MessageBox.Show(TestDLLJunk.Class1.returnJunk(14))
End Sub


In the calling application, I just right clicked references and browsed to where the compiled dll is. It shows the full path to it in the properties of the dll's reference.

So then I did the same as before in Delphi. This is in my interface uses:

Function returnJunk(inputParam: Byte): string; STDCALL; EXTERNAL 'TestJunk.dll';



Compiling my dll code creates 3 files. I put them ALL in the source folder of my Delphi project and also in the Win32\debug folder just for grins.

And this is my calling code:

procedure TForm1.Button2Click(Sender: TObject);
var
  rj: string;
begin
  rj := returnJunk(4);
end;



Same as before. It compiles but doesn't run. I get the same message as in my O.P.

Then, for the heck of it, I registered it:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe D:\Data\1Work\KT\DelphiTokyoVersion\TokyoConversionTestProjects\TestDLLJunk\bin\Release\TestDLLJunk.dll /tlb /codebase

Same results as before.


这篇关于如何在delphi应用程序中使用我在VB.NET中创建的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆