DLL Wrapper

This is a discussion on DLL Wrapper within the C Programming forums, part of the General Programming Boards category; Hi everybody! I'm a newbie with C development, so have patience with me... ^^ Well... Im building a DLL that ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Unhappy DLL Wrapper

    Hi everybody!

    I'm a newbie with C development, so have patience with me... ^^

    Well... Im building a DLL that calls another DLL (a DLL Wrapper to solve JNI problems... or better to say Oracle Webutil problems... but this dont 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 Ive 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, "Im 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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thank&#180;s a lot vart.

    Can I ask you to translate it to C code ? 8-S... I understand the problem, but I don&#180;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&#180;ve guessed in a error on wPCLink6 call...

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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);
    }

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    It&#180;s done... now works fine, thank&#180;s bithub... thank&#180;s vart...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Creating wrapper for C++ DLL
    By steve1_rm in forum C# Programming
    Replies: 2
    Last Post: 06-10-2008, 07:49 PM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. #ifndef wrapper to reference dll
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21