EDN Admin
Well-known member
Hi,
Im very new to C# btw. Im 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, Id 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 DLLs functions but they instantly yield an exception error when they are used as follows: <font size=2>
</font><font color="#0000ff" size=2>string</font><font size=2> test = </font><font color="#008080" size=2>encryption</font><font size=2>.Encryptstring(</font><font color="#800000" size=2>"test"</font><font size=2>);
</font><font color="#008080" size=2>Console</font><font size=2>.Write(test);</font>
<font size=2>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 TestEncryptProgram.cs:line 15</font>
<font size=2>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?</font>
<font size=2>I have tested the DLL with a delphi program and all works correctly.</font>
<font size=2>Thanks for any help,</font>
<font size=2>Martin Searle</font>
<font size=2>Computing Service, University of Kent. </font>
View the full article
Im very new to C# btw. Im 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, Id 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 DLLs functions but they instantly yield an exception error when they are used as follows: <font size=2>
</font><font color="#0000ff" size=2>string</font><font size=2> test = </font><font color="#008080" size=2>encryption</font><font size=2>.Encryptstring(</font><font color="#800000" size=2>"test"</font><font size=2>);
</font><font color="#008080" size=2>Console</font><font size=2>.Write(test);</font>
<font size=2>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 TestEncryptProgram.cs:line 15</font>
<font size=2>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?</font>
<font size=2>I have tested the DLL with a delphi program and all works correctly.</font>
<font size=2>Thanks for any help,</font>
<font size=2>Martin Searle</font>
<font size=2>Computing Service, University of Kent. </font>
View the full article