#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    Returning a PCHAR


    I have lazily scanned this forum in search of something that might help with the trouble Im having but havent found anything so I thought I would try my luck :-) Im a NOOB so be gentle :-D

    I need to return a PCHAR from this DLL but for some reason I cant! The compiler spits:

    initialization to `CHAR *' from `const char *' discards qualifiers


    Heres my function code:

    Code:
    export PCHAR testing(PCHAR reg_name, PCHAR reg_key)
    {
        //PCHAR result;
        
        string sRegKey;
        sRegKey = reg_key;
        for(int x=0;x<sRegKey.length();x++)
        {
            if(sRegKey.substr(x,1) == "-")
            {
                    sRegKey.erase(x,1);
            }
        }
     
         PCHAR result = sRegKey.c_str()
     
        return result;
    }
    No matter what I do, I always get into trouble when it comes to types :-(

    UPDATE:

    Okay... I managed to cast it and it compiled properly.. However, is there a better/more widely acceptable way to work with this type of thing?

    solution:

    return (char*)sRegKey.c_str();
    Last edited by theAquaNut; June 18th, 2003 at 03:59 PM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    You haven't indicated which line the error is in, but I can guess it is here:
    PCHAR result = sRegKey.c_str();

    The c_str() method returns a const char *. You can always type cast it to char *, if you like, something like this:
    PCHAR result = (char *) sRegKey.c_str();

    However, this will cause bigger problems, because when the function exits, sRegKey will be destroyed (since it's a local variable) and you'll return a pointer pointing to invalid memory. A better way to handle this is to alter your function to accept another parameter (i.e.) a pointer to an output char array, or a reference to a string object. Then your function can return an error code, but return the actual result via the additional parameter.

    Code:
    export DWORD testing(PCHAR reg_name, PCHAR reg_key, PCHAR output_buf, DWORD *output_size)
    {
      //PCHAR result;                                                                                                                   
    
      string sRegKey;
      sRegKey = reg_key;
      for(int x=0;x<sRegKey.length();x++)
        {
          if(sRegKey.substr(x,1) == "-")
            {
              sRegKey.erase(x,1);
            }
        }
    
      if (sRegKey.length() >= output_size) {
        /* Indicate output buf wasn't large enough to hold                                                                              
           the result, and return the size needed */
         *output_size = sRegKey.length() + 1;
        return 0;
      }
    
      strcpy(output_buf, sRegKey.c_str());
      return 1; /* SUCCESS */
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0
    Thanks for your response... Something puzzles me though.

    I think I understand what youre talking about. After I call the DLL from my app, the pointer once pointing to "result" (the address where the value of sRegKey is will no longer be there.

    However, what I dont understand is that if the function has exited, the value of sRegKey will already be returned and Ill have no use for it anymore. So why the need for the checking of the length of sRegKey? In case my reg_key contains nothing but dashes? I dont quite understand that.

    Also, the app that Im calling the DLL from can only pass INTs or PCHARs and the DLL must return those 2 types also.

    Thanks for your help. Looks like I have a bit of learning to do.

IMN logo majestic logo threadwatch logo seochat tools logo