-
DLL Wrapper
Hi everybody!
I'm a newbie with C development, so have patience with me... ^^
Well... I´m building a DLL that calls another DLL (a DLL Wrapper to solve JNI problems... or better to say Oracle Webutil problems... but this don´t matter at this moment).
The problem occours when the method of target DLL is called. I got a Access Violation on MSVCRT.DLL. I believe that's some mistake on parameters passage or anywhere around it.
Please, a little help here... I promisse learn more about C language when I´ve more time (and improve my english too ^^).
The wrapper DLL code:
(wPCLink6.h)
Code:
#ifndef _wPCLINK6_H_
#define _wPCLINK6_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void Display_Erro(int Ip_Setado, char * Message);
#endif /* _wPCLINK6_H_ */
(wPCLink6.c)
Code:
#include "wPCLink6.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
typedef char * (__cdecl *Display_ErroFn)(int Ip_Setado);
DLLIMPORT void Display_Erro(int Ip_Setado, char *Message)
{
DWORD Error = 0;
HINSTANCE libInst = LoadLibrary("pclink6.dll");
if (!libInst)
{
Error = GetLastError();
sprintf(Message, "Error: %d", Error);
return;
}
Display_ErroFn Display_ErroHnd = (Display_ErroFn)GetProcAddress(libInst, "Display_Erro");
if (Display_ErroHnd == 0)
{
Error = GetLastError();
sprintf(Message, "Error: %d", Error);
return;
}
Message = Display_ErroHnd(const Ip_Setado); // Crash happens
sprintf(Message, "I´m here!"); // Unreachable code
FreeLibrary(libInst);
return;
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
Target DLL Code:
(PCLink6.h)
Code:
#ifndef _PCLINK6_H_
#define _PCLINK6_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT char * Display_Erro(int Ip_Setado);
#endif /* _PCLINK6_H_ */
(PCLink6.c)
Code:
#include "teste.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char * Message;
DLLIMPORT char * Display_Erro(int Ip_Setado)
{
strcpy(Message,"Sucess!");
return Message;
return 0;
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
Note that my only desire is recive "Sucess!" string through Message parameter.
-
it has nothing to do with the dll
you copy a string to the uninitialized pointer.
you have 2 reasonable options:
1. return const char* - pointer to the const string
2. provide a buffer as a parameter, so the function will have a place to copy the error code
-
Thank´s a lot vart.
Can I ask you to translate it to C code ? 8-S... I understand the problem, but I don´t know how to write it.
I try:
Code:
DLLIMPORT const char * Display_Erro(int Ip_Setado)
{
strcpy(Message,"Sucess!");
return Message;
return 0;
}
got the same error...
I try:
Code:
char * Message[255];
DLLIMPORT char * Display_Erro(int Ip_Setado)
{
strcpy(*Message,"Success!");
return *Message;
return 0;
}
The error:
Code:
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x77c260c1, pid=3252, tid=3512
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode, sharing)
# Problematic frame:
# C [msvcrt.dll+0x360c1]
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
The PCLink6 is a emulator of the original one, that give me the same error. Because of this behavior I´ve guessed in a error on wPCLink6 call...
-
Code:
char * Message;
DLLIMPORT char * Display_Erro(int Ip_Setado)
{
strcpy(Message,"Sucess!");
return Message;
return 0;
}
Should be changed to:
Code:
char Message[255];
DLLIMPORT char * Display_Erro(int Ip_Setado)
{
strcpy(Message,"Sucess!");
return Message;
//return 0; // commented out this line since you are returning Message on the line above.
}
As vart mentioned, it would be better to pass in a buffer to be filled by the function.
Code:
DLLIMPORT void Display_Erro(int Ip_Setado, char* Message, int MessageLength)
{
strncpy(Message,"Sucess!",MessageLength);
}
-
It´s done... now works fine, thank´s bithub... thank´s vart...