| View previous topic :: View next topic |
| Author |
Message |
Roger Guest
|
Posted: Mon Apr 09, 2007 9:40 pm Post subject: char to string conversion |
|
|
I am having trouble with a char to string conversion, e.g.
char *pStr;
pStr = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; // points to
"Some Text";
string sText = pStr; // sText = { }
How can I set sText to equal "Some Text" ???
Roger |
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Mon Apr 09, 2007 10:19 pm Post subject: Re: char to string conversion |
|
|
Roger <aretae (AT) magma (DOT) ca> writes:
| Quote: | I am having trouble with a char to string conversion, e.g.
char *pStr;
pStr = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; // points to
"Some Text";
string sText = pStr; // sText = { }
And what exactly is the trouble? |
|
|
| Back to top |
|
 |
Roger Guest
|
Posted: Mon Apr 09, 2007 10:44 pm Post subject: Re: char to string conversion |
|
|
| Quote: | And what exactly is the trouble?
I want sText = "Some Text"; |
Not sText = { };
Roger |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Apr 09, 2007 11:04 pm Post subject: Re: char to string conversion |
|
|
"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a7b5c$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I want sText = "Some Text";
Not sText = { };
|
You did not show what lpDevCaps is to begin with. Obviously, you are
not actually pointing to the right memory offset.
Gambit |
|
| Back to top |
|
 |
Roger Guest
|
Posted: Tue Apr 10, 2007 12:30 am Post subject: Re: char to string conversion |
|
|
| Quote: | You did not show what lpDevCaps is to begin with. Obviously, you are
not actually pointing to the right memory offset.
Gambit
|
My apologies, I was obviously not clear. I did use debug to inspect
pStr which is actually equal to "AOpen FM56-EXV".
and debug showed sText = { };
So I know pStr is pointing to "Some Text", what I don't know is why I
can't set
string sText = what pStr is point to, i.e. in my specific instance
"AOpen FM56-EXV"
instead sText = { }
EXCEPT now I just tried it again, and it WORKS!!! I have no idea why it
didn't work for a couple of hours, I don't know what I did between the
time it didn't and now to make it work. I can't replicate the original
problem.
What I am doing now that seems to work is:
string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; //
for debug
Roger |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Apr 10, 2007 12:40 am Post subject: Re: char to string conversion |
|
|
"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a9443$1 (AT) newsgroups (DOT) borland.com...
| Quote: | My apologies, I was obviously not clear. I did use debug to
inspect pStr which is actually equal to "AOpen FM56-EXV".
and debug showed sText = { };
|
That would not be possible given the code you have shown. sText would
contain the actual string value. Now, it could be that the debugger
itself simply can't show the contents of sText correctly. That is a
whole separate issue.
| Quote: | So I know pStr is pointing to "Some Text", what I don't know is
why I can't set
string sText = what pStr is point to, i.e. in my specific instance
"AOpen FM56-EXV"
instead sText = { }
|
The code you showed suggests that you are using a std::string from the
STL. The STL is not the easiest thing to debug because it uses a lot
of internal structures. Try using AnsiString as a test and see what
happens. The debugger can handle AnsiString very easily. If it
contains the desired result, the the code itself is fine.
Gambit |
|
| Back to top |
|
 |
Roger Guest
|
Posted: Tue Apr 10, 2007 12:55 am Post subject: Re: char to string conversion |
|
|
| Quote: | The code you showed suggests that you are using a std::string from the
STL. The STL is not the easiest thing to debug because it uses a lot
of internal structures. Try using AnsiString as a test and see what
happens. The debugger can handle AnsiString very easily. If it
contains the desired result, the the code itself is fine.
Thanks Remy, I will try and remember this. |
|
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Tue Apr 10, 2007 12:56 am Post subject: Re: char to string conversion |
|
|
Roger <aretae (AT) magma (DOT) ca> writes:
| Quote: | What I am doing now that seems to work is:
string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset;
|
Be wary of code that "seems" to work! I can't help but wonder why you
need the cast? What is the type of lpDevCaps?
--
Chris (TeamB); |
|
| Back to top |
|
 |
Roger Guest
|
Posted: Tue Apr 10, 2007 1:02 am Post subject: Re: char to string conversion |
|
|
Chris Uzdavinis (TeamB) wrote:
| Quote: | Roger <aretae (AT) magma (DOT) ca> writes:
What I am doing now that seems to work is:
string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset;
Be wary of code that "seems" to work! I can't help but wonder why you
need the cast? What is the type of lpDevCaps?
If I leave the cast out I get the following error: |
[C++ Error] unitTAPI.cpp(188): E2034 Cannot convert 'linedevcaps_tag *'
to 'string'
lpDevCaps is defined as:
LINEDEVCAPS *lpDevCaps = NULL;
Roger |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Apr 10, 2007 1:45 am Post subject: Re: char to string conversion |
|
|
"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a9bc5$1 (AT) newsgroups (DOT) borland.com...
| Quote: | If I leave the cast out I get the following error:
[C++ Error] unitTAPI.cpp(188): E2034 Cannot convert 'linedevcaps_tag
*'
to 'string'
|
The error is correct in that particular situation, as lpDevCaps is a
pointer to a structure.
| Quote: | lpDevCaps is defined as:
LINEDEVCAPS *lpDevCaps = NULL;
|
That would have been really useful information to know earlier. In
which case, your code should be more like the following instead:
char* pName = (char*) (((LPBYTE) lpDevCaps) +
lpDevCaps->dwLineNameOffset);
string sText(pName, lpDevCaps->dwLineNameSize);
You can't rely on the name being null-terminated. That is why
Microsoft includes the dwLineNameSize member.
Gambit |
|
| Back to top |
|
 |
Roger Guest
|
Posted: Tue Apr 10, 2007 3:25 am Post subject: Re: char to string conversion |
|
|
| Quote: | That would have been really useful information to know earlier. In
which case, your code should be more like the following instead:
char* pName = (char*) (((LPBYTE) lpDevCaps) +
lpDevCaps->dwLineNameOffset);
string sText(pName, lpDevCaps->dwLineNameSize);
You can't rely on the name being null-terminated. That is why
Microsoft includes the dwLineNameSize member.
Gambit
Thanks Remy, I changed my code accordingly. |
|
|
| Back to top |
|
 |
|