Ver Mensaje Individual
  #1  
Antiguo 23-11-2006
Avatar de acertij022
acertij022 acertij022 is offline
Miembro
 
Registrado: may 2003
Ubicación: Argentina-Bs. As.
Posts: 233
Reputación: 22
acertij022 Va por buen camino
Dll de delphi en C#

Hola buenas tardes, les molesto porque tengo un problema y necesito de su ayuda:
Tengo una DLL realizada en delphi 6 para encriptar y desencriptar string con el siguiente codigo:
Código Delphi [-]
library Rijndael;

uses
  DCPcrypt2, DCPblockciphers, DCPrijndael, DCPsha1;

{$R *.res}
function Encriptar(Dato, Clave1, Accion :PChar):PChar; export;
var
  DCP_rijndael1: TDCP_rijndael;
  my_message, key : string;
begin
  my_message := Dato;
  key        := Clave1;

  DCP_rijndael1:= TDCP_rijndael.Create(nil);
  DCP_rijndael1.InitStr(Key,TDCP_sha1);         // initialize the cipher with a hash of the passphrase
  if Accion = PChar('0') then  Result := PChar(DCP_rijndael1.DecryptString(my_message))
  else Result := PChar(DCP_rijndael1.EncryptString(my_message));
  DCP_rijndael1.Burn;
  DCP_rijndael1.Free;
end;
Exports Encriptar;
begin
end.
El cual funciona perfectamente en delphi pero al intentar usarlo en C# con el siguiente codigo:
Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace prueba
{
    public partial class Form1 : Form
    {
        [DllImport("Rijndael.dll")]
        public static extern String Encriptar(string Dato, string Clave1, string Accion);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            textBox2.Text = Encriptar(textBox1.Text, textBox3.Text, "1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = Encriptar(textBox1.Text, textBox3.Text,"0");
        }
    }
}
Al intentar usar la DLL me tira el error:
No se controló System.AccessViolationException
Message="Intento de leer o escribir en la memoria protegida. A menudo, esto indica que hay otra memoria dañada."
Source="prueba"
StackTrace:
en prueba.Form1.Encriptar(String Dato, String Clave1, String Accion)
en prueba.Form1.button1_Click(Object sender, EventArgs e) en C:\Documents and Settings\wloose\Escritorio\prueba123\prueba\Form1.cs:línea 27
en System.Windows.Forms.Control.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
en System.Windows.Forms.Control.WndProc(Message& m)
en System.Windows.Forms.ButtonBase.WndProc(Message& m)
en System.Windows.Forms.Button.WndProc(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
en System.Windows.Forms.Application.Run(Form mainForm)
en prueba.Program.Main() en C:\Documents and Settings\wloose\Escritorio\prueba123\prueba\Program.cs:línea 17
en System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
en System.Threading.ThreadHelper.ThreadStart()

supongo que el error es por incompatibilidad en las variable pero me resulta extraño por que uso variable tipo PChar en la DLL que son compatible con string en C# (el array de char termina con 0x00) alguno me podria ayudar o orientarme cual es el problema.
Desde ya muchas gracias
Responder Con Cita