Entwickler-Ecke

Off Topic - Delphi DLL in VB.net verwenden


Bronstein - Mi 16.11.11 15:58
Titel: Delphi DLL in VB.net verwenden
Hallo,
ich habe eine Funktion in einer Delphi DLL folgendermaßen deklariert.

Quelltext
1:
function GetInfos(Benutzer, PW,Auftrag:Pchar;aPlan,stueli,matVerw,aNetz,klassfz,GetZeichnungen: Boolean): PChar; stdcall;                    

In Visual Basic möchte ich nun diese Funktion nutzen und binde die DLL folgendermaßen ein:

Quelltext
1:
2:
  <DllImport("c:\Dokumente und Einstellungen\PottiezT\Desktop\EGO_Siplace_Desk\SAP_STUELI.dll")> Private Shared Function GetInfos(ByRef Benutzer As Char, ByRef PW As Char, ByRef Auftrag As Char, ByRef aPlan As Boolean, ByRef stueli As Boolean, ByRef matVerw As Boolean, ByRef aNetz As Boolean, ByRef klassfz As Boolean, ByRef GetZeichnungen As Boolean) As Char
  End Function

Wenn ich die Funktion aufrufe bekomme ich folgenden Fehler:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Eine externe Komponente hat eine Ausnahme ausgelöst.
System.Runtime.InteropServices.SEHException wurde nicht behandelt.
  ErrorCode=-2147467259
  Message="Eine externe Komponente hat eine Ausnahme ausgelöst."
  Source="EGO_Siplace_Desk"
  StackTrace:
       bei EGO_Siplace_Desk.Form1.GetInfos(Char& Benutzer, Char& PW, Char& Auftrag, Char& Variante, Boolean& aPlan, Boolean& stueli, Boolean& matVerw, Boolean& aNetz, Boolean& klassfz, Boolean& GetZeichnungen)
       bei EGO_Siplace_Desk.Form1.Button1_Click_1(Object sender, EventArgs e) in c:\Dokumente und Einstellungen\PottiezT\Desktop\EGO_Siplace_Desk\EGO_Siplace_Desk\Form1.vb:Zeile 771.
       bei System.Windows.Forms.Control.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       bei System.Windows.Forms.Control.WndProc(Message& m)
       bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
       bei System.Windows.Forms.Button.WndProc(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(ApplicationContext context)
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       bei EGO_Siplace_Desk.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
       bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()


JDKDelphi - Mi 16.11.11 16:15

Hallo,

Die Parameter Byref xyz as Char können eigentlich so nicht funktionieren, weil in Delphi die Parameter als PCHAR deklariert sind und
somit einen Zeiger auf Chararray bzw. Bytearray verlangen.

Die DLL sollte somit alle PCHAR als Widestring verwalten, da VB.NET nur BSTR bzw. Widestring benutzt und mit Zeigern schlecht umgehen kann.

Probier das mal


Gruß

JDKDELPHI


Bronstein - Do 17.11.11 08:37

Hallo,
leider hat das auch nicht funktioniert.

Ich habe mal eine neue Funktion in der DLL gemacht der man keinen Parameter übergeben muss. Hier der Code der DLL:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
library SAP_STUELI;

uses
  Windows, SysUtils ;

function Test2(): WideString; stdcall;
begin
  result := 'Test2';
end;

exports
  Test2;

begin

end.


Hier der Code der VB Anwendung:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Imports System
Imports System.Runtime.InteropServices

Public Class Form1

  <DllImport("c:\Dokumente und Einstellungen\PottiezT\Desktop\EGO_Siplace_Desk\SAP_STUELI.dll")> Private Shared Function Test2() As String
  End Function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tmpstr As String
    'tmpstr = test(True)
    tmpstr = Test2()
    MsgBox(tmpstr)
  End Sub
End Class


Doch dann bekomme ich auch eine Fehlermeldung:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
System.Runtime.InteropServices.SEHException wurde nicht behandelt.
  ErrorCode=-2147467259
  Message="Eine externe Komponente hat eine Ausnahme ausgelöst."
  Source="TEST"
  StackTrace:
       bei WindowsApplication1.Form1.Test2()
       bei WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\temp_edb\TEST\TEST\TEST\Form1.vb:Zeile 18.
       bei System.Windows.Forms.Control.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       bei System.Windows.Forms.Control.WndProc(Message& m)
       bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
       bei System.Windows.Forms.Button.WndProc(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(ApplicationContext context)
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       bei WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
       bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()