Re: LPCWSTR to CString conversion problem



I am trying out the following program...

////////////////////////////////////////////////////////////////////////////////////////////////
<config>
<key name="first" value="1250A"/>
<key name="second" value="1250B"/>
</config>

And I want to read a value of a key named "foo":

#include "stdafx.h"
#include <atlbase.h>
#include "xmllite.h"
#include <strsafe.h>

int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
CComPtr<IStream> pFileStream;
CComPtr<IXmlReader> pReader;
XmlNodeType nodeType;
const WCHAR* pName;
const WCHAR* pValue;


//Open XML document
if (FAILED(hr = SHCreateStreamOnFile(L"config.xml",
STGM_READ, &pFileStream)))
{
wprintf(L"Error opening XML document, error %08.8lx", hr);
return -1;
}

if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader),
(void**)&pReader, NULL)))
{
wprintf(L"Error creating XmlReader, error %08.8lx", hr);
return -1;
}

if (FAILED(hr = pReader->SetInput(pFileStream)))
{
wprintf(L"Error setting input for XmlReader, error %08.8lx", hr);
return -1;
}

while (S_OK == (hr = pReader->Read(&nodeType)))
{
switch (nodeType)
{
case XmlNodeType_Element:
if (FAILED(hr = pReader->GetQualifiedName(&pName, NULL)))

{
wprintf(L"Error reading element name, error %08.8lx", hr);
return -1;
}
if (wcscmp(pName, L"key") == 0)
{
if (SUCCEEDED(hr =
pReader->MoveToAttributeByName(L"name", NULL)))

{
if (FAILED(hr = pReader->GetValue(&pValue, NULL)))

{
wprintf(L"Error reading attribute value, error %08.8lx", hr);
return -1;
}
if (wcscmp(pValue, L"foo") == 0)
{
//That's an element we are looking for
if (FAILED(hr =
pReader->MoveToAttributeByName(L"value", NULL)))

{
wprintf(L"Error reading attribute \"value\", error %08.8lx",
hr);
return -1;
}
if (FAILED(hr = pReader->GetValue(&pValue, NULL)))

{
wprintf(L"Error reading attribute value, error %08.8lx", hr);
return -1;
}
wprintf(L"Key \"foo\"'s value is \"%s\" ", pValue);
}
}
}
break;
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////

I am using the above program with some modifications.

if (FAILED(hr = pReader->GetValue(&pValue, NULL)))

here pValue has the value "1280.50A." and i assign it to a cstring NewV.
There are no errors.

Then i use the following piece f code

for(i=0;i<NewV.GetLength();i++)
{
if(isdigit(NewV.GetAt(i)) == FALSE)
{
hasAlpha = TRUE;
}
}

But when i is 0, then the if loop gets executed where it should have not
entered. I hope you got where the problem is. Using iswdigit doesnt help
either. My project is in unicode.

Thanks,
Rohit.

"SvenC" wrote:

Hi,

sorry i intended to write getat(0). My cstring variable NewVersion which
contains the return value of GetValue() function has the value
"1280.50.". I get the first character of the string and pass it to
isdigit(). But it returns false. I guess there is some problem with the
LPCWSTR to CString conversion. When i manually initialize the NewVersion
as NewVersion = "1280.50.", then the first character wen passed to
isdigit() returns true.

Please show your code.
Do you compile as Unicode or Ansi? If Unicode use iswdigit to test a single
character.

--
SvenC

"Igor Tandetnik" wrote:

"Rohit Kumar" <RohitKumar@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:95D7082C-4540-41A2-B485-E866F903C030@xxxxxxxxxxxxx
I am using xmllite methods for reading from an xml file inside my
interface method. There is a function IXmlReader::GetValue which
returns the value of the xml element. The return value is an LPCWSTR.
When i try assigning it to a CString and use the first element of
that cstring using getat(1) then it returns a garbage value.

GetAt(1) returns the second character of the string, not the first. Are
you sure the string is long enough? What exactly do you mean by "garbage
value"?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Searching for byte sequence
    ... you have to decide if you are ever going to support Unicode. ... You can use CString, but in that case, you should add ... for a string? ... would read a header and search it, then if nothing is found, skip the 10K ...
    (microsoft.public.vc.mfc)
  • Re: Conversion of byte array to cstring
    ... "feature" is that there is a CString constructor that will accept an 8-bit ... string and create an initialized CString, even if the CString is actually ... This ctor converts from ANSI to Unicode, so, even if you are ... // Point to next WCHAR in source array ...
    (microsoft.public.vc.mfc)
  • Re: Retrieving text from CEdit box as char *?
    ... Windows CE uses Unicode so strings coming from the OS such as window text ... If you really need a string of char, you need to convert the CString to ... Alternatively, if you don't actually need the string in ASCII format, just ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Invalid pointer
    ... because it is more flexible than CString. ... safely convert to string? ... I beg to differ with Brian on this point. ... always UNICODE. ...
    (microsoft.public.vc.language)
  • Re: Using wide CRT functions in non-Unicode builds
    ... >>I have a file containing Unicode text. ... so it returns NULL on finding the first ASCII character ... >>wcschrsees as the string terminator). ... unless you happen to be lucky enough to be searching for the first character ...
    (microsoft.public.vc.mfc)