#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    64
    Rep Power
    11

    String to LPCSTR


    Is there a way to convert a String to an LPCSTR?
    I use this code:

    Code:
    CopyFile((LPCSTR)textBox1->Text,"c:\ttest.dat",true);
    and get this error:
    error C2440: 'Typcasting': 'System::String __gc *' cannot be converted to 'LPCSTR'
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    There should be a function in the text object to convert to a char * (which is basically what the LPCSTR is (Long Pointer to C STRing, if I recall correctly)), just search your documentation.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    I think its:

    LPCSTR szString = "Orange";
    String *sString = new String(szString);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    My bad, the other way around its:

    LPCTSTR szString = NULL;
    szString = (LPCTSTR)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer());

    CopyFile(szString, "c:\ttest.dat", true);

    System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr((void*)szString)));
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    64
    Rep Power
    11

    Thumbs up


    And again I say:"Thank you very much Nem!!!!!!!!"

IMN logo majestic logo threadwatch logo seochat tools logo