Einzelnen Beitrag anzeigen

JFB

Registriert seit: 8. Apr 2005
61 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

Aufruf Delphi-Dll von DotNet, Probleme mit struct-Parameter

  Alt 17. Jul 2018, 14:28
Hallo,

ich habe ein merkwürdiges Problem beim Versuch, eine Methode in einer Delphi-Dll (habe ich als Anhang beigefügt) aus einer DotNet-Anwendung aufzurufen.

Ich habe folgenden Code:

Code:
// c#-Programm
// ===========
using System;
using System.Runtime.InteropServices;

namespace ConsoleApp3
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct TPara1Rec
        {
            public double Jahresumsatz1;
            public double Jahresumsatz2;
            public double Aufwendungen;
            public double Privateinlagen;
            public double Privatentnahmen;
            public double LandwFlaeche;
            public double ForstFlaeche;
            public double Teichflaeche;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3 + 1)]
            public string Waehrung; // EUR

            public bool P39A3;
        }

        [DllImport("DStbVV2012.dll", EntryPoint = "Gegenstandswerte", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        private static extern void DStbVV2012_Gegenstandswerte(ref TPara1Rec para, ref double pWertDa, ref double pWertDb, IntPtr ptr);

        static void Main(string[] args)
        {
            TPara1Rec para1 = new TPara1Rec() { Aufwendungen = 1154659.74d, ForstFlaeche = 4.95d, Jahresumsatz1 = 1219758.83d, Jahresumsatz2 = 12, LandwFlaeche = 219.26d, P39A3 = false, Privateinlagen = 67148.41d, Privatentnahmen = 157779.99d, Teichflaeche = 0d, Waehrung = "EUR" };
            double para2 = 0;
            double para3 = 0;
            IntPtr ptr = IntPtr.Zero;

            DStbVV2012_Gegenstandswerte(ref para1, ref para2, ref para3, ptr);
        }
    }
}

// Delphi-Dll DStbVV2012.dll
// =========================

  TPara1Rec = packed record
    Jahresumsatz1   :real;
    Jahresumsatz2   :real;
    Aufwendungen   :real;
    PrivatEinlagen :real;
    PrivatEntnahmen :real;
    LandwFlaeche   :real;
    ForstFlaeche   :real;
    TeichFlaeche   :real;
    VerpachtFlaeche :real;
    Waehrung       :AnsiString;
    P39A3           :boolean;
  end;

procedure Gegenstandswerte(var Para :TPara1Rec;
                           var pWertDA,pWertDB :real;
                           Para4Ptr:TPara4Ptr = nil);

var W_BETRAG_1, W_BETRAG_2, ZWI_BETRAG_1,ZWI_BETRAG_2,ZWI_BETRAG_3,ZWI_BETRAG_4,
    w_jahresumsatz_je_ha, W_MULTIPLIKATOR,mp : real;
begin
  if Para.Waehrung = 'DEM' then mp := cEuroFaktor else mp := 1;

  W_BETRAG_1 := Para.JahresUmsatz1 / mp +
                          Para.PrivatEinlagen / mp;
                         
  // Weiterer Code
end;
Wenn ich die Delphi-Dll debugge, ist der Parameter "Para" nicht verfügbar und es kommt zu einer Schutzverletzung. Entfernt man in der C#-Anwendung im Feld para1.Jahresumsatz1 die Nachkommastellen (statt 1219758.83d nur 1219758 übergeben) funktioniert alles wunderbar.

Ich bin ratlos. Hat jemand eine Idee?
Bin nur noch heute und morgen in der Firma, dann erst nächste Woche wieder. Kann also auf Rückfragen u.U. nicht sofort antworten.

Gruß
Johann
Angehängte Dateien
Dateityp: zip DStbVV2012.zip (357,5 KB, 3x aufgerufen)

Geändert von JFB (17. Jul 2018 um 15:00 Uhr)
  Mit Zitat antworten Zitat