Hi,
I'm very new to C# btw. I'm sure someone probably has gone round this loop before....
I have a DLL that I wrote in Delphi to handle encryption of pascal strings, these are the definitions of the two functions in pascal, they are compiled into the file encrypt.dll, I'd like to use these functions from within a c# program :
function decryptstring(input : string) : string; export;
function Encryptstring(input : string) : string; export;
I then wrote an encryption.cs file to provide a C# header to the library and compiled that with the command:
csc /t:Library /out:encryption.dll encryption.cs
The encryption.cs file looks like this:
using System.Runtime.InteropServices;
public class encryption
{
[DllImport("encrypt.dll")]
public static extern string Encryptstring(string input);
[DllImport("encrypt.dll")]
public static extern string decryptstring(string input);
}
Now when I add this as a reference to my project I can access the DLL's functions but they instantly yield an exception error when they are used as follows:
string test = encryption.Encryptstring("test"); Console.Write(test);The error is: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at mas2encryption.Encryptstring(String input)
at TestEncrypt.Program.Main(String[] args) in TestEncrypt\Program.cs:line 15
It looks to me as if the C# string type is incompatible with the pascal string type. Does anyone know how to call the routines correctly and return the correct string values
I have tested the DLL with a delphi program and all works correctly.
Thanks for any help,
Martin Searle
Computing Service, University of Kent.

How do you call a pascal DLL string parameter function with C# strings?
DLdfrd
To avoid that whole ShareMem system, have your Delphi routine return a PChar rather than a Delphi String. PChars don't need ShareMem, and they are normal null terminated "C" style strings, which should be compatible with most other languages likely to consume your library. For instance, they work with C, C++ and C#. By Delphi 7, there was good support for PChar's in the Delphi language, so you don't need to do much special when working with them.
function GetOne(): PChar; export;
begin
Result := 'One';
end;
You can then import your Delphi library into C# in the standard manner:
namespace DelphiLibrary
{
public class Class1
{
[DllImport("TestLibrary.dll")]
public static extern String GetOne();
}
}
And call the method just as your would expect:
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
textBox1.Text = Class1.GetOne();
}
The complete declaration for the Delphi library would look like this:
library TestLibrary;
uses
SysUtils,
Classes;
{$R *.res}
function GetOne(): PChar; export;
begin
Result := 'One';
end;
exports
GetOne;
begin
end.
- Charlie
MikePHall
Charlie,
Hey I had gotten returning a pchar via a function in a DLL working. I'm having problems expanding on that - I need to return mutiple values and I can't seem to get the declaration down that will let me do that.
For instance, a DLL in D2006:
function foo( P1: pchar; P2: pchar; P3: pchar ): pchar; export; cdecl;
begin
P1 := 'blah blah';
Result := 'Blah2';
end
In C#:
[
DllImport("fooshell.dll")] public static extern string foo( string P1, string P2, string P3 );Calling this function will properly return 'Blah2' as the result but does not change P1. What's the trick to pass like a 'var'
Thanks,
Nick H
DARKGuy
In what format are Delphi strings stored I assume they are not null terminated. If they are length prefixed then try using the MarshalAs(UnmanagedType.AnsiBstr) if they are ANSI strings or UnmanagedType.BStr if they are Unicode. This is applied to the parameter itself.
Michael Taylor - 10/11/06