-
March 5th, 2003, 03:11 PM
#1
Casting to LPCTSTR
Hello,
I am taking a command line argument from argv and casting it to a LPCTSTR. When I do this and attempt to do a reversefind on the LPCTSTR it finds nothing even though I'm positive that the character I'm looking for is in the string. Could this be caused by the way I'm casting? I do the following:
LPCTSTR fname = (LPCTSTR) argv[1];
Thanks,
Tim
-
March 5th, 2003, 03:37 PM
#2
Post the code where argv[1] gets its value. I suspect its the wrong data type.
-
March 5th, 2003, 03:40 PM
#3
It could cause a problem because technically strings can never be cast legitimately. We don't know if argv is char or wchar_t, and we don't know if 'T' is char or wchar_t so it's hard to say. If they are both the same then casting is superfluous. If they are different, then casting will not work (even though it will compile). If argv is TCHAR then you don't need the cast. If argv is explicitly char or wchar_t, then you should be using strrchr or wcsrchr explicitly rather than a fucntion that requires LPCTSTR.
If your function is main(int arg, char **argv) then maybe you could try changing it to _tmain(int arg, TCHAR **argv).
Last edited by 0xC0000005; March 5th, 2003 at 03:43 PM.
-
March 5th, 2003, 03:47 PM
#4
char *argv[] is the command line parameters.
int main(int argc, char *argv[])
{
}
-
March 5th, 2003, 04:00 PM
#5
Post your code snippet...reversefind?? CString? why the cast?
-
March 5th, 2003, 04:10 PM
#6
I meant the method CString method ReverseFind.
Here is the problem, I have a legacy method that I can not change and it requires an LPCTSTR. My string that I need to pass to this method comes from the command line. I stepped through the legacy method and it assigns the LPCTSTR to a CString and uses the ReverseFind method to find a character.
The method was failing so I stepped through and notice the reverse find wasn't finding the character it was supposed to even though I watched the variable the character was in the string.
int main(int argc, char *argv[])
{
UINT nID = 32786;
LPCTSTR fname = (LPCTSTR) argv[1];
......code....
modelEd->HandlePopupMenuItems(fname, nID);
..............code............
return 0;
}
The legacy method prototype:
void HandlePopupMenuItems (LPCTSTR filename, UINT nID);
-
March 5th, 2003, 04:56 PM
#7
LPCTSTR is a macro of const char*.
const char* s = argv[1];
should work fine.
-
March 5th, 2003, 04:58 PM
#8
Some other issue is going on.
PS: and I hope it's not "foo" vs "FOO"....
Last edited by Mick; March 5th, 2003 at 05:01 PM.
-
March 5th, 2003, 05:35 PM
#9
Micorsoft supplies source for the CString class. Have you tried stepping into the ReverseFind code?
One more throut. Could you be trying to use the unicode library version of CString to find something in a non-unicode string?
-
March 6th, 2003, 04:00 AM
#10
I think it is probably not necessary to step into the ReverseFind code but I would either put a breakpoint on the ReverseFind or put a TRACE just before it to see what the CString looks like just before the ReverseFind. Then if the string is what is expected then stepping into the MFC source code would be worthwhile. I suspect the string that ReverseFind is getting is not what is expected.
-
March 6th, 2003, 08:46 AM
#11
Thanks, but I actually already tried putting the Trace before the ReverseFind to verify that the string has the character I was looking for. It did not find the character though.
Within the HandlePopupMenuItems there is a call to another method that I neglected to include in my last post. It calls a method in another class that requires a CString. So I have listed snippets of the code starting at main.
First, my main class:
int main(int argc, char *argv[])
{
UINT nID = 32786;
LPCTSTR fname = (LPCTSTR) argv[1];
......code....
modelEd->HandlePopupMenuItems(fname, nID);
..............code............
return 0;
}
Method in legacy Class:
void HandlePopupMenuItems (LPCTSTR filename, UINT nID)
{
CString fname = filename;
CFileUtil::ChangeExtension (fname, EQUATIONFILE_EXTENSION);
}
Method in another legacy class:
void CFileUtil::ChangeExtension (CString& filename, LPCTSTR extension)
{
int pos = filename.ReverseFind ('.');
}
Since pos is -1 I figured I might be casting to the LPCTSTR incorrectly.
Tim
-
March 6th, 2003, 11:33 AM
#12
Can you show us what "filename" has in it in ChangeExtension before the ReverseFind? Please copy to the clipboard and paste from the clipboard instead of copying it by typing it in here. You can sanitize it by deleting the drive and root directory or whatever you need to do to sanitize it but if you can show us the data as close to what it actually is then that will be very helpfull for helping you.
-
March 6th, 2003, 11:59 AM
#13
Instead of this step:
LPCTSTR fname = (LPCTSTR) argv[1];
I did this:
CString fname(argv[1]);
modelEd->HandlePopupMenuItems(fname, nID);
This is working. I was trying to cast to an LPCTSTR since that is the type the method required, but from reading over the messages I tried using the CString constructor. Does this seem correct?
Thanks,
Tim
-
March 6th, 2003, 12:10 PM
#14
Since that works, use it. It is okay to do it that way. However it is a little bit extra processing but that is not worth spending your time fixing that.
What you had should also have worked so it is very strange it did not.
-
March 6th, 2003, 12:49 PM
#15
Yeah, I thought so as well. Thank you all for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|