CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2003
    Posts
    11

    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

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    Post the code where argv[1] gets its value. I suspect its the wrong data type.

  3. #3
    Join Date
    May 2002
    Posts
    1,435
    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.

  4. #4
    Join Date
    Mar 2003
    Posts
    11
    char *argv[] is the command line parameters.

    int main(int argc, char *argv[])
    {

    }

  5. #5
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Post your code snippet...reversefind?? CString? why the cast?

  6. #6
    Join Date
    Mar 2003
    Posts
    11
    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);

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    LPCTSTR is a macro of const char*.

    const char* s = argv[1];

    should work fine.

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    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.

  9. #9
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    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?

  10. #10
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  11. #11
    Join Date
    Mar 2003
    Posts
    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

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  13. #13
    Join Date
    Mar 2003
    Posts
    11
    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

  14. #14
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  15. #15
    Join Date
    Mar 2003
    Posts
    11
    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
  •  





Click Here to Expand Forum to Full Width

Featured