Thread: Problem with accessing DLL functions

  1. #16
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    I have a follow-up question concerning my use of 'char' as the return type for my fcn1Wrapper() call:

    my main code is similar to this, assuming that the function declaration was performed in the global workspace as discussed in previous posts above:

    Code:
    main(){
    
       char fcn1_out;
    
       dll = LoadLibrary("myLibrary.dll");
    
        if (dll != NULL)
            fcn1Wrapper = (fcn1WrapperPointer)GetProcAddress(dll, "fcn1Wrapper"); 
        
        else
            fcn1Wrapper = NULL;
    	
        fcn1_out = fcn1Wrapper(6, 6, "Setup_File.txt");
    
        printf("fcn1_out = %c.\n", fcn1_out);
    
    }

    If I were working with a bool output type, I would expect my fcn1Wrapper() call to return 1 for success and 0 for failure. Here, when I print out the char return value it is '.' (i.e. a period). I've looked through documentation for converting characters to bools, but I haven't found anything that seems useful. i.e. I'm not even sure whether there *is* a correspondence between the '.' character that my function call is returning, and a 0/1-type boolean value that I can use to determine success/failure of my function call. Does this returned '.' value have any actual *meaning*? If e.g. I remove from my working directory the input file that my fcn1Wrapper() takes as an argument, I still get '.' as my return value...

    Is my code above correct, and if so, is there any way for me to determine the success of my function call using the char return type? Thanks.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    If you print out a char variable using the format specifier "%c" the value of the char is interpreted as a character according to your current character set (probably ASCII). Thus you print out the characters ASCII 0 or ASCII 1. You see the period because you literally print the period in your printf() (the period after "%c" in your format string).

    You are only interested in the numerical value of your char variable. Hence you have to print it out with the format specifier "%d":
    Code:
    printf("fcn1_out  = %d\n", fcn1_out);
    Another problem is that you assign NULL to fcn1Wrapper when the loading of the dll fails. But later you call fcn1Wrapper regardless where fcn1Wrapper points to. In case it points to NULL your program will crash.
    You should only use fcn1Wrapper when it doesn't point to NULL:
    Code:
    if (fcn1Wrapper)
        fcn1_out = fcn1Wrapper(6, 6, "Setup_File.txt");
    Bye, Andreas

  3. #18
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    That was very helpful, Andreas -- thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structs in Functions Problem
    By opeth in forum C Programming
    Replies: 8
    Last Post: 04-02-2012, 09:11 PM
  2. Accessing member functions of sockaddr_in
    By Stack Overflow in forum Linux Programming
    Replies: 8
    Last Post: 01-29-2005, 11:26 AM
  3. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  4. Replies: 2
    Last Post: 11-28-2003, 09:23 AM
  5. Accessing CEdit Member Functions Inside CEditView ::MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 08:52 PM