Chris Est
Chris Est

Reputation: 17

LPTSTR Losing Scope, work around not working (C++)

EDIT: Solution was to replace LPTSTR to wstring. When Windows API required LPTSTR the wstring is casted with .c_str()

My problem is, a variable (LPTSTR) is going out of scope and I can't fix it. I'll explain...

In my code I am trying to save the name of an Image in a Class. The Class I am using saves function limits, flags, and other variables that need to be saved, such as the original file directory.

I am using Windows, thus when I want to load an Image the Image Name needs to be in a LPTSTR (Unicode is enabled). I've had my head aches about the Ascii to Unicode conversions already.

For most of my code I have had no issue with this class, until I try to save this Image Name. The first time the image name is created and saved, the name is created only one function deep from main, and this works! The second time I am making a new image name with the same function but the function call is 3 levels deep from main. When the 2nd level returns to the first level, my image name goes out of scope (the string gets over written).

I am trying to make a work around but after 3 days, nothing is working.

Here is my code for the setter of the class. It is brutal as it is trying to be a work around.

// From the Limit.h
void Set_BMPName(LPTSTR S); 
LPTSTR BMPName;

// From the Limit.cpp
void Limit::Set_BMPName(LPTSTR S)
{// This function should set BMPName to S.  
    static  LPTSTR* StaticString = new LPTSTR(S);
    // I used static to stop the variable from going out of scope.
    if (*StaticString != S)
    { // Because StaticString is static, it wont get updated if the name is changed.
        delete StaticString;
        // So delete it.
        static  LPTSTR* StaticString = new LPTSTR(S);
        // And remake it..
    }   
    BMPName = *StaticString;
    // Then set BMPName to this Static variable.
    return; 
};

The first time I call Set_BMPName() name it works. The second time the setter is called the If loop gets entered, but when the new StaticString is made, the values were not assigned. Could I possibly be using the "new" operator wrongly?

Let me know if you need to see any more code. Thank you.

Upvotes: 1

Views: 131

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

See, this is the problem with Microsoft giving pointer types names that do not look like pointers.

Long pointer to TCHAR string (source)

Your LPTSTR doesn't actually contain a string; like a char* (or wchar_t*), it merely points to one. You can play with the lifetime of the pointer all you like but that's not going to do anything about the actual data. Certainly, dynamically allocating the pointer (while an inventive attempt) won't help.

Why not use proper C++ technology like std::string (or std::wstring) instead? Then you will be done in three minutes, not three days. :-)

If you really are stuck with LPTSTRs, then use the Windows API function dedicated to copying ones data to another: StringCchCopy.

Upvotes: 2

Related Questions