Skip to main content
GameDev.net gamedev.net
🔒 Locked

Concatenating two LPCWSTR

Started by Chett2001 Sep 10, 2007 at 10:29 AM 4 replies 13.6k views
Original Post
Chett2001
Chett2001
Hey all, This is another general/DirectX question, heres what i want to do in semi-pseudocode:

IDirect3DTexture9* tex = 0;
LPCWSTR texFilename = CA2W(mtrls.pTextureFilename);
LPCWSTR path = TEXT("MyFolder\\")
LPCWSTR finalPath = 0;
finalPath = path + TexFilename; 

                            D3DXCreateTextureFromFile(
					Device,
					finalPath,
					&tex);
I basically want to concatenate two LPCWSTR's so i can get to the texture resource in MyFolder but obviously i cant do this because they are two pointers, whats the best way to achieve this, im using a unicode build also. Thanks, Chett
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
jpetrie
jpetrie
Use std::wstring for both strings, then you can simply use operator+ to concatenate. When you need to pass the LPCWSTR to API methods, use the c_str() method of std::wstring.
Colin Jeanne
Colin Jeanne
There is also the StringCchCat() function from strsafe.h
Anon Mike
Anon Mike
Old-school 'C' style would be to call wcslen on both strings, malloc a new buffer big enough to hold the concatentation (don't forget the NULL terminator and to handle the difference between byte and character lengths), wcscpy the first string to the new buffer, wcscat the second string (you also can use the StringCch stuff if you really want to), do whatever, then try to remember to free the buffer afterwords.

Or you can do C++ style and use wstring instead as jpetrie suggested and everything works with basically no effort.

Don't use the TEXT macro if you are using LPCWSTR. Just use L"MyFolder\\". You only use TEXT if you are also using LPCTSTR. IMHO there isn't really much point in using the 'T' stuff anymore. Just using Unicode and be done with it.
-Mike
Chett2001
Chett2001
Cool thanks, i also need to do float to LPCWSTR. Is there an easy way to do this also?
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
Zahlman
Zahlman
Quote:
Original post by Chett2001
Cool thanks, i also need to do float to LPCWSTR. Is there an easy way to do this also?


Again, convert to std::wstring first, via a std::wstringstream.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.