Pasar cadenas por referencia en una dll
Publicado por rraces (25 intervenciones) el 04/04/2006 13:02:34
Hola amigos, estoy intentando pasar una cadena por referencia a una dll, para que esta la modifique. Pero no me funciona. Este es el codigo de la dll:
/ pruebadll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <string.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) int Somma(int a, int b)
{
return a+b;
}
extern "C" __declspec(dllexport) int Test(char * a)
{
strcpy(a,"DESPUES");
return 1;
}
Y el código del ejecutable que llama a la dll es:
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include "string"
using namespace std;
void main(void)
{
typedef UINT (CALLBACK* LPFNDLLFUNC1)(char *);
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
UINT uReturnVal;
char *cadena = " Antes de llmar a la dll";
hDLL = LoadLibrary("C:\\rraces\\pruebadll.dll");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
"Test");
if (!lpfnDllFunc1)
{
// handle the error
printf("2\n");
FreeLibrary(hDLL);
}
else
{
// call the function
printf("2\n");
uReturnVal = lpfnDllFunc1(cadena);
printf("LA CADENA --> %s\n",cadena);
}
}
system("pause");
}
Decir que la funcion de la suma si me funciona, pero la de Test no. Al ejecutarlo da un mensaje de error.
/ pruebadll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <string.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) int Somma(int a, int b)
{
return a+b;
}
extern "C" __declspec(dllexport) int Test(char * a)
{
strcpy(a,"DESPUES");
return 1;
}
Y el código del ejecutable que llama a la dll es:
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include "string"
using namespace std;
void main(void)
{
typedef UINT (CALLBACK* LPFNDLLFUNC1)(char *);
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
UINT uReturnVal;
char *cadena = " Antes de llmar a la dll";
hDLL = LoadLibrary("C:\\rraces\\pruebadll.dll");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
"Test");
if (!lpfnDllFunc1)
{
// handle the error
printf("2\n");
FreeLibrary(hDLL);
}
else
{
// call the function
printf("2\n");
uReturnVal = lpfnDllFunc1(cadena);
printf("LA CADENA --> %s\n",cadena);
}
}
system("pause");
}
Decir que la funcion de la suma si me funciona, pero la de Test no. Al ejecutarlo da un mensaje de error.
Valora esta pregunta


0