LPCTSTR operations/ convertion

This is a discussion on LPCTSTR operations/ convertion within the Windows Programming forums, part of the Platform Specific Boards category; Simple problem: how do I do this?... Input: LPCTSTR L"Directory/someFileA" LPSTR L"someFileB" output: LPCTSTR L"Directory/someFileB" Long version: Although my program ...

  1. #1
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27

    LPCTSTR operations/ convertion

    Simple problem:

    how do I do this?...

    Input:
    LPCTSTR L"Directory/someFileA"
    LPSTR L"someFileB"

    output:
    LPCTSTR L"Directory/someFileB"

    Long version:

    Although my program uses directx, I think this is more of a windows question.

    I'm making a mesh class to hold mesh, texture, and material data from a .x file.

    One of the arguments for the directx function that loads the file, D3DXLoadMeshFromX, is a LPCTSTR representing the full path file to read from. (like L"C:/myModel.x")

    In the process of loading the mesh, I also end up with a string representing the texture file name, in the form of a LPSTR, which was gotten from the .x file.

    The problem is that the function that loads textures requires a LPCTSTR that represents the full path of the texture file. Passing the texture name as it is is legal (LPSTR is converted to LPCTSTR no problem), but it can't load the texture because it needs the full directory, not just the name. (It works if I manually alter the .x file so that the texture file name contains the full path, but I don't want to do this for every model I want to use)

    So, essentially I have to lop off the .x file name, leaving the directory it is contained in, and tack on the texture file name, then send the result as a LPCTSTR to the directx function, D3DXCreateTextureFromFileA.

    As far as I know, there are no functions that can easily manipulate LPCTSTR like find_last_of(char) of substr(int, int). I've tried converting to and from std::string and std::wstring but it doesn't work.

    One more thing: this process must be done in 2 parts: 1: get the directory, and 2: add the texture file name, since I'll be using multiple texture file names, (gotten in a loop), but they are all in one directory.

    So, any ideas?

    How would I convert from LPCTSTR to std::string and back?

    Thank you!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Some things to note:

    TCHAR resolves to either a "char" or a "wchar_t" depending on if UNICODE is defined or not (in VS project properties, "Use Unicode Character Set" will enable UNICODE).

    LPCTSTR - the "T" in the middle stands for TCHAR, so this is a "const TCHAR*" - which in turn is either "const char*" or "const wchar_t*".

    LPSTR = char*
    LPCSTR = const char*

    LPWSTR = wchar_t*
    LPCWSTR = const wchar_t*

    LPTSTR = LPSTR or LPWSTR
    LPCTSTR = LPCSTR or LPCWSTR

    Of the two API's mentioned, there are two versions of each:
    D3DXLoadMeshFromXA or D3DXLoadMeshFromXW
    D3DXCreateTextureFromFileA or D3DXCreateTextureFromFileW

    If you don't call the "A" or "W" version explicit, then the UNICODE setting will determine which version is called.

    So if you just want to deal in char strings, then just use char string and call the "A" functions explicitly.

    Or just use wchar_t strings and call the "W" functions explicitly.

    Or just use TCHAR strings and call leave off the "A" or "W" from the function call. I don't recommend using TCHAR's myself, but you have to understand what they are since the Win32 API uses them.

    gg

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Vick jr View Post
    Simple problem:

    How would I convert from LPCTSTR to std::string and back?

    Thank you!
    TCHAR to std::string:
    Code:
    TCHAR* pszStr = _T("...");
    std::string sConv = CT2A(pszStr);
    You can use CX2Y, where X is the current state of the string, and Y is the encoding it should be transformed into.

    Just beware of statements such as:
    Code:
    char* szConv = CT2A(pszStr);
    Alternatively you can use the CStringA/CStringW classes to initialize from a TCHAR* and use there implicit cast operators to pass to API's.
    Last edited by valaris; 05-21-2010 at 12:38 AM.

  4. #4
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by Codeplug View Post
    Some things to note:

    TCHAR resolves to either a "char" or a "wchar_t" depending on if UNICODE is defined or not (in VS project properties, "Use Unicode Character Set" will enable UNICODE).

    LPCTSTR - the "T" in the middle stands for TCHAR, so this is a "const TCHAR*" - which in turn is either "const char*" or "const wchar_t*".

    LPSTR = char*
    LPCSTR = const char*

    LPWSTR = wchar_t*
    LPCWSTR = const wchar_t*

    LPTSTR = LPSTR or LPWSTR
    LPCTSTR = LPCSTR or LPCWSTR

    Of the two API's mentioned, there are two versions of each:
    D3DXLoadMeshFromXA or D3DXLoadMeshFromXW
    D3DXCreateTextureFromFileA or D3DXCreateTextureFromFileW

    If you don't call the "A" or "W" version explicit, then the UNICODE setting will determine which version is called.

    So if you just want to deal in char strings, then just use char string and call the "A" functions explicitly.

    Or just use wchar_t strings and call the "W" functions explicitly.

    Or just use TCHAR strings and call leave off the "A" or "W" from the function call. I don't recommend using TCHAR's myself, but you have to understand what they are since the Win32 API uses them.

    gg
    I want to use plain old std::string with regular single byte ascii chars.

    should this work?

    Code:
    string file="C:/models/mymodel.x";
    
    D3DXLoadMeshFromXA(file.c_str());//other args not shown
    
    //do some string manipulation to get the full path texture file name
    LPSTR pTextureFilename="mymodel.bmp";//this is actually gotten from a directx function
    string directory=file.substr(0,file.find_last_of('/'))+"/";
    string strTexture=directory+pTextureFilename;
    
    D3DXCreateTextureFromFileA(strTexture.c_str());//other args not shown
    The above compiled fine and seemed to work. I put breakpoints where it loads the mesh and file and all the strings were correct and the correct data was loaded into the mesh, texture, and materials.

    However, I then get a completely new error. (which i'll make a new post for in a minute. Has to do with _BLOCK_TYPE_IS_VALID ASSERTE failure because a destructor is being called on an uninitialized object during array initialization.)

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Vick jr View Post
    LPSTR pTextureFilename="mymodel.bmp";
    should probably be a LPCSTR.

  6. #6
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by valaris View Post
    should probably be a LPCSTR.
    D3DXMATERIAL Structure (Windows)

    nope. It returns a LPSTR.
    Frogblast the ventcore!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Quote Originally Posted by Vick jr View Post
    D3DXMATERIAL Structure (Windows)

    nope. It returns a LPSTR.
    No, that's just the problem.
    A string literal is const char*, aka LPCSTR.
    However, the structure expects a char*, aka LPSTR.
    So obviously this is a problem because you cannot write to a string literal since it's const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 05:30 AM
  2. Vector Operations in 3D Graphics
    By The Dog in forum Game Programming
    Replies: 27
    Last Post: 09-13-2005, 06:11 PM
  3. Autorun - How?
    By nouneim in forum C++ Programming
    Replies: 30
    Last Post: 06-23-2004, 07:15 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21