Prob assigning a LPWSTR to a CStringW - Programmer

This is a discussion on Prob assigning a LPWSTR to a CStringW - Programmer ; Hi, I have a LPWSTR in my code and I want a CStringW from it. I'm trying to use "=" to assign the value, but it seems to be garbling the string because of slashes in the string. Before the ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Prob assigning a LPWSTR to a CStringW

  1. Prob assigning a LPWSTR to a CStringW

    Hi,

    I have a LPWSTR in my code and I want a CStringW from it. I'm trying to
    use "=" to assign the value, but it seems to be garbling the string
    because of slashes in the string.

    Before the code executes, I can print out the LPWSTR, and it has the
    correct value. Then after the assignment, I print both variables and
    the values are a mix of what I want and garbage.

    It looks something like this:

    LPWSTR wideCharString;
    CStringW wideCString;

    .... //wideCharString gets set to "C:\Program
    Files\Webscore\some.exe"
    //I printout the string here to verify its value and it looks good

    wideCString = wideCharString;

    //both values now print as "C:\Program FilesŸ>bscor?"


    It looks like the assignment statement is interpreting the \W as an
    escape character or something. Any ideas?

    Thanks so much in advance.


  2. Re: Prob assigning a LPWSTR to a CStringW

    >I have a LPWSTR in my code and I want a CStringW from it. I'm trying to
    >use "=" to assign the value, but it seems to be garbling the string
    >because of slashes in the string.


    Can you show a complete example, nothing you have shown would cause
    such a problem to the best of my knowledge.

    Dave

  3. Re: Prob assigning a LPWSTR to a CStringW

    You're right, this problem is probably not just in the = operator. I am
    getting the string from a remote Windows registry, and that may be part
    of the problem.

    Here is the relevant code. Thanks again.


    // ----------------------- this is in a seperate file
    -------------------

    int RemoteRegistry::GetStringValue(LPCWSTR value_name, LPWSTR &value)
    {
    wchar_t path[1024];
    DWORD path_size = 1024;

    LONG RetVal = RegQueryValueExW(RemoteKey, value_name, NULL, NULL,
    (LPBYTE)path, &path_size);

    if ( RetVal != ERROR_SUCCESS )
    {
    value = TEXT("");
    return 0;
    }
    value = path;
    return 1;
    }



    int RemoteRegistry::OpenKey(LPCWSTR Key)
    {
    LONG RetVal = RegOpenKeyExW(RemoteReg, Key, 0, KEY_ALL_ACCESS,
    &RemoteKey);

    if ( RetVal != ERROR_SUCCESS )
    {
    RemoteKey = NULL;
    return 0;
    }
    return 1;
    }



    //----------------------- here is where I see the problem
    --------------------------------

    LPWSTR char_tPath;

    if ( !Reg.OpenKey(TEXT
    ("SYSTEM\\CurrentControlSet\\Services\\WsIntegration")) )
    {
    OutputDebugString(TEXT("Error opening key value.\n"));
    return 0;
    }

    if ( ! reg.GetStringValue(TEXT("ImagePath"), char_tPath) )
    {
    reg.CloseKey();
    OutputDebugString(TEXT("Error getting key value.\n"));
    return 0;
    }

    wchar_t temp[1024];
    swprintf(temp, TEXT("charPath in getWciPath: %s, size: %d\n"),
    char_tPath, wcslen(char_tPath));
    OutputDebugString(temp);
    swprintf(temp, TEXT("WciPath in getWciPath: %s, size: %d\n"), wciPath,
    wciPath.GetLength());
    OutputDebugString(temp);

    wciPath = char_tPath;


    swprintf(temp, TEXT("charPath in getWciPath: %s, size: %d\n"),
    char_tPath, wcslen(char_tPath));
    OutputDebugString(temp);
    swprintf(temp, TEXT("WciPath in getWciPath: %s, size: %d\n"), wciPath,
    wciPath.GetLength());
    OutputDebugString(temp);

    reg.CloseKey();

    return 1;


    // ---------------------------------------- this is the debug printout
    -------------------------------
    charPath in getWciPath: C:\Program
    Files\Websense\bin\wscitrixintegration.exe, size: 53
    WciPath in getWciPath: , size: 0
    charPath in getWciPath: C:\Program FilesŸebsens?, size: 25
    WciPath in getWciPath: C:\Program FilesŸebsens?, size: 53


  4. Re: Prob assigning a LPWSTR to a CStringW

    A little more info:

    I don't have any problems if I initialize the CStringW object to some
    constant string which is bigger than the one that I assign to it later.
    For example:

    wciPath =
    TEXT("11111111111111111111111111111111111111111111111111 1111111111111111111111");

    However, it's impossible to know how big all such strings will be in
    advance, so this hack doesn't really solve the problem. But hopefully
    it sheds more light.


  5. Re: Prob assigning a LPWSTR to a CStringW

    Problem sloved!

    For any who may find this post useful for some reason, the resolution
    has to do with passing back a pointer to stack memory, which we all
    know is a terrible thing to do. In the function that gets a string from
    the registry and passes it back through a reference, I was using a char
    array declared locally, instead of allocating heap memory, or using a
    buffer which is passed in.


+ Reply to Thread