Hallo Allerseits, wie muss der Quelltext aussehen damit die Delphi-Dll fehlerfrei importiert wird?<BR>
Delphi-Dll:<BR>
<PRE>
library TestLib;
uses
SysUtils, Classes, Windows, Forms;
{$R *.RES}
function TestFunction(const pInput: PChar; pOutput: PChar): Integer; stdcall;
begin
Result:=0;
try
StrPCopy(pOutput, pInput + ' (ok)');
Result:=1;
except
Application.MessageBox('Fehler', 'Message', 0);
end;
end;
exports
TestFunction;
begin
end.
</PRE><BR>
C#-Programm:<BR>
<PRE>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace Test_Dll
{
public class Form1:System.Windows.Forms.Form
{
[DllImport("TestLib.dll",
EntryPoint="TestFunction", ExactSpelling=true)]
public static extern int TestFunction(string pInput, string pOutput);
private System.Windows.Forms.Label lblAnzahl;
private System.Windows.Forms.TextBox txtText;
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.Button btnEnde;
private System.ComponentModel.Container components=null;
public Form1()
{
InitializeComponent();
lblAnzahl.Text="";
txtText.Text="";
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.lblAnzahl = new System.Windows.Forms.Label();
this.txtText = new System.Windows.Forms.TextBox();
this.btnTest = new System.Windows.Forms.Button();
this.btnEnde = new System.Windows.Forms.Button();
this.SuspendLayout();
// lblAnzahl
this.lblAnzahl.Location = new System.Drawing.Point(0, 0);
this.lblAnzahl.Name = "lblAnzahl";
this.lblAnzahl.Size = new System.Drawing.Size(100, 16);
this.lblAnzahl.TabIndex = 0;
this.lblAnzahl.Text = "lblAnzahl";
// txtText
this.txtText.AcceptsReturn = true;
this.txtText.Location = new System.Drawing.Point(0, 24);
this.txtText.MaxLength = 0;
this.txtText.Multiline = true;
this.txtText.Name = "txtText";
this.txtText.Size = new System.Drawing.Size(568, 208);
this.txtText.TabIndex = 1;
this.txtText.Text = "txtText";
// btnTest
this.btnTest.Location = new System.Drawing.Point(408, 240);
this.btnTest.Name = "btnTest";
this.btnTest.TabIndex = 2;
this.btnTest.Text = "Test";
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
// btnEnde
this.btnEnde.Location = new System.Drawing.Point(488, 240);
this.btnEnde.Name = "btnEnde";
this.btnEnde.TabIndex = 3;
this.btnEnde.Text = "Ende";
this.btnEnde.Click += new System.EventHandler(this.btnEnde_Click);
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(568, 269);
this.Controls.Add(this.btnEnde);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.txtText);
this.Controls.Add(this.lblAnzahl);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnTest_Click(object sender, System.EventArgs e)
{
string inputString, outputString;
int Anzahl;
inputString=txtText.Text;
outputString="";
Anzahl=TestFunction(inputString, outputString);
lblAnzahl.Text=Anzahl.ToString();
txtText.Text=outputString;
}
private void btnEnde_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
}
}</PRE>In Vb.net funktioniert alles einwandfrei, aber nicht in c#. Was mache ich falsch? MfG Jürgen
Delphi-Dll:<BR>
<PRE>
library TestLib;
uses
SysUtils, Classes, Windows, Forms;
{$R *.RES}
function TestFunction(const pInput: PChar; pOutput: PChar): Integer; stdcall;
begin
Result:=0;
try
StrPCopy(pOutput, pInput + ' (ok)');
Result:=1;
except
Application.MessageBox('Fehler', 'Message', 0);
end;
end;
exports
TestFunction;
begin
end.
</PRE><BR>
C#-Programm:<BR>
<PRE>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace Test_Dll
{
public class Form1:System.Windows.Forms.Form
{
[DllImport("TestLib.dll",
EntryPoint="TestFunction", ExactSpelling=true)]
public static extern int TestFunction(string pInput, string pOutput);
private System.Windows.Forms.Label lblAnzahl;
private System.Windows.Forms.TextBox txtText;
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.Button btnEnde;
private System.ComponentModel.Container components=null;
public Form1()
{
InitializeComponent();
lblAnzahl.Text="";
txtText.Text="";
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.lblAnzahl = new System.Windows.Forms.Label();
this.txtText = new System.Windows.Forms.TextBox();
this.btnTest = new System.Windows.Forms.Button();
this.btnEnde = new System.Windows.Forms.Button();
this.SuspendLayout();
// lblAnzahl
this.lblAnzahl.Location = new System.Drawing.Point(0, 0);
this.lblAnzahl.Name = "lblAnzahl";
this.lblAnzahl.Size = new System.Drawing.Size(100, 16);
this.lblAnzahl.TabIndex = 0;
this.lblAnzahl.Text = "lblAnzahl";
// txtText
this.txtText.AcceptsReturn = true;
this.txtText.Location = new System.Drawing.Point(0, 24);
this.txtText.MaxLength = 0;
this.txtText.Multiline = true;
this.txtText.Name = "txtText";
this.txtText.Size = new System.Drawing.Size(568, 208);
this.txtText.TabIndex = 1;
this.txtText.Text = "txtText";
// btnTest
this.btnTest.Location = new System.Drawing.Point(408, 240);
this.btnTest.Name = "btnTest";
this.btnTest.TabIndex = 2;
this.btnTest.Text = "Test";
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
// btnEnde
this.btnEnde.Location = new System.Drawing.Point(488, 240);
this.btnEnde.Name = "btnEnde";
this.btnEnde.TabIndex = 3;
this.btnEnde.Text = "Ende";
this.btnEnde.Click += new System.EventHandler(this.btnEnde_Click);
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(568, 269);
this.Controls.Add(this.btnEnde);
this.Controls.Add(this.btnTest);
this.Controls.Add(this.txtText);
this.Controls.Add(this.lblAnzahl);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnTest_Click(object sender, System.EventArgs e)
{
string inputString, outputString;
int Anzahl;
inputString=txtText.Text;
outputString="";
Anzahl=TestFunction(inputString, outputString);
lblAnzahl.Text=Anzahl.ToString();
txtText.Text=outputString;
}
private void btnEnde_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
}
}</PRE>In Vb.net funktioniert alles einwandfrei, aber nicht in c#. Was mache ich falsch? MfG Jürgen
Comment