Ver Mensaje Individual
  #1  
Antiguo 11-05-2012
JXJ JXJ is offline
Miembro
 
Registrado: abr 2005
Posts: 2.475
Reputación: 22
JXJ Va por buen camino
Question ¿como hacer dll c puro en bcb2010 y loader en delphi 2010?

hola estoy haciendo una dll
en c
c puro, no es cpp o c++
con el ide de c++ builder 2010

y el programa que lo consume esta hecho en delphi 2010

el problema es que no me devuelve lo que yo quiero un string para saber que paso con
las instrucciones

pongo el codigo. de la dll

Código:
// ---------------------------------------------------------------------------
// ES UNA DLL TIPOP C  SIN NINGUN ESTILO NI VCL NI ESTILO VC
// ---------------------------------------------------------------------------

#include <windows.h>
#include <string.h>
#pragma argsused
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

static char *file2string(FILE *file)
{
  char buffer[256];
  char *ptr;
  char *string=NULL;
  size_t len=0;
  size_t stringlen;

  if(file) {
    while(fgets(buffer, sizeof(buffer), file)) {
      ptr= strchr(buffer, '\r');
      if(ptr)
        *ptr=0;
      ptr= strchr(buffer, '\n');
      if(ptr)
        *ptr=0;
      stringlen=strlen(buffer);
      if(string)
        string = realloc(string, len+stringlen+1);
      else
        string = malloc(stringlen+1);

      strcpy(string+len, buffer);

      len+=stringlen;
    }
    return string;
  }
  else
    return NULL; /* no string */
}

static char *file2memory(FILE *file, long *size)
{
  char buffer[1024];
  char *string=NULL;
  char *newstring=NULL;
  size_t len=0;
  long stringlen=0;

  if(file) {
    while((len = fread(buffer, 1, sizeof(buffer), file))) {
      if(string) {
        newstring = realloc(string, len+stringlen);
        if(newstring)
          string = newstring;
        else
          break; /* no more strings attached! :-) */
      }
      else
        string = malloc(len);
      memcpy(&string[stringlen], buffer, len);
      stringlen+=len;
    }
    *size = stringlen;
    return string;
  }
  else
    return NULL; /* no string */
}


static size_t mywrite(void * ptr , size_t size , size_t nmemb , void * stream){
int ret  = fwrite(ptr,size,nmemb,(FILE *)stream);
fwrite(ptr,size,nmemb,stdout);
return ret;
}



char __declspec(dllexport)WINAPI   suma(char *ELPRIMERPARAMETRO, char *ELSEGUNDOPARAMETRO,
                                      char *ELTERCERPARAMETRO, char *ELCUARTOPARAMETRO)
{

      FILE * header = fopen(ELPRIMERPARAMETRO,"w");
      FILE * body = fopen(ELSEGUNDOPARAMETRO,"w");

 char *TipoResultado;
      TipoResultado = "3333";


   MessageBox( NULL, ELPRIMERPARAMETRO, "1r", MB_OK );
   MessageBox( NULL, ELSEGUNDOPARAMETRO, "2d", MB_OK );
   MessageBox( NULL, ELTERCERPARAMETRO, "3e", MB_OK );
   MessageBox( NULL, ELCUARTOPARAMETRO, "4t", MB_OK );

    return *TipoResultado;
}

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason,
    void* lpReserved)
    {
    return 1;
}
// ---------------------------------------------------------------------------
y este es el de l aaplicacion delphi
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExceptionLog;

type
    TDLLFunc = function(param1: AnsiString; param2: AnsiString; param3: AnsiString; param4: AnsiString): PAnsiChar;

type
  TForm1 = class(TForm)
    Button1: TButton;
    EurekaLog1: TEurekaLog;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  { assign a nil - not loaded function }

  {assign a nil - not loaded function}
 const
   DLLFunc: TDLLFunc = nil;
   DLLName = 'inicio.DLL';
 {handle of loaded dll}
 var
  DLLHandle: THandle;

implementation

{$R *.dfm}
function suma(parametro1: AnsiString; parametro2: AnsiString; parametro3 :AnsiString;  parametro4 :AnsiString): PAnsiChar; stdcall; far; external 'inicio.DLL';

procedure TForm1.FormCreate(Sender: TObject);
var
  resultadosuma: PAnsiChar;
  recibido:string;
begin
  DLLHandle := LoadLibrary(DLLName);

  if (DLLHandle < HINSTANCE_ERROR) then
    raise Exception.Create(DLLName + ' library can not be loaded or not found. ' + SysErrorMessage(GetLastError));
  try
    @DLLFunc := GetProcAddress(DLLHandle, 'suma');
    if Assigned(DLLFunc) then
    begin

resultadosuma :=   suma(
                     PansiChar(AnsiString('a1')),
                     PansiChar(AnsiString('b2')),
                     PansiChar(AnsiString('c3')),
                     PansiChar(AnsiString('d4'))
                     );
    end;
  finally
    FreeLibrary(DLLHandle);
  end;

  ShowMessage(  resultadosuma);

end;

end.


logre que se pasen los parametros de delphi a la dll y que me los muestre
el problema es que no me devuelve nada
yo espero un string
3333
y no me vuelve nada

solo me ha funcionado usando integer en vez de string.
en la dll c

en c estoy aprendiendo. y pues no se que hago mal

gracias.


la funcion se llama suma. por que asi se me ocurrio pero debe de regresar un string para procesarlo
Responder Con Cita