C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2003, 03:49 PM
theAquaNut theAquaNut is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 theAquaNut User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old June 18th, 2003, 04:14 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 1 m 37 sec
Reputation Power: 1001
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 */
}

Reply With Quote
  #3  
Old June 18th, 2003, 05:29 PM
theAquaNut theAquaNut is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 2 theAquaNut User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Returning a PCHAR


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT