Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:18:09+00:00 2026-05-28T03:18:09+00:00

I am trying to open an in memory stream for use with the xmllite

  • 0

I am trying to open an in memory stream for use with the xmllite library. Writing to one works ok, but reading from one is giving me a hard time. Below is the code that I am using. Basically I create a default xml string (LPWSTR) and write it to a memory stream using CreateStreamOnHGlobal. I then seek to the beginning and read it back to make sure its in there (it is). Then I seek back again and assign it to the input of the reader. It never gets past the line:

while (S_OK == (hr = pReader->Read(&nodeType)))

I get an XmlNodeType_None and an HRESULT value of -1072894427. I believe it is having trouble reading the stream, but I dont know for sure. The same code works fine if I use a filestream instead and writing to the xml from the memory stream works as well

HRESULT hr = S_OK;  CComPtr<IStream> pStream = NULL;
IXmlReader *pReader = NULL;
XmlNodeType nodeType;

LPWSTR pwszXMLString = 
    L"<?xml version\"1.0\" encoding=\"UTF-8\" ?>\r\n"
    L"<paramlist name=\"LP\">\r\n"
        L"<value></value>\r\n"
        L"<value></value>\r\n"
    L"</paramlist>\r\n"
    L"<param name=\"AutoConnect\">false</param>\r\n"
    L"<param name=\"ConnectWhenLit\">false</param>\r\n"
    L"<param name=\"SessionMaxBytes\">200000</param>\r\n"
    L"<param name=\"SessionTimeoutSecs\">300</param>\r\n"
    L"<param name=\"PacketDelayMs\">0</param>\r\n"
    L"<param name=\"PacketSizeBytes\">4096</param>\r\n"
    L"<param name=\"LowSSLSecurity\">true</param>\r\n";

DWORD dwWritten = 0;
hr = CreateStreamOnHGlobal(NULL, FALSE, &pStream);
hr = pStream->Write(pwszXMLString, wcslen(pwszXMLString) * sizeof(WCHAR), &dwWritten);

// print out the contents of the memory stream just to make sure we have it
LARGE_INTEGER pos;
pos.QuadPart = 0;
pStream->Seek(pos, STREAM_SEEK_SET, NULL);
STATSTG ssStreamData = {0};
pStream->Stat(&ssStreamData, STATFLAG_NONAME);
SIZE_T cbSize = ssStreamData.cbSize.LowPart;
LPWSTR pwszContent = (WCHAR*) new BYTE[cbSize + sizeof(WCHAR)];
if (pwszContent == NULL)
    return E_OUTOFMEMORY;

pStream->Seek(pos, STREAM_SEEK_SET, NULL);
SIZE_T cbRead;
pStream->Read(pwszContent, cbSize, &cbRead);
pwszContent[cbSize/sizeof(WCHAR)] = L'\0';

CZString czContent;
czContent.LoadWideString(pwszContent, cbSize);
wprintf(L"%S", czContent.GetString().c_str());
pStream->Seek(pos, STREAM_SEEK_SET, NULL);

if (hr == S_OK)
{
    typedef HRESULT (WINAPI *CreateXmlReaderFunc)(const IID & riid, void** ppvObject, IMalloc * pMalloc);
    CreateXmlReaderFunc _CreateXmlReaderFunc = (CreateXmlReaderFunc)GetProcAddress(m_hXMLLite, "CreateXmlReader");

    if (FAILED(hr = _CreateXmlReaderFunc(__uuidof(IXmlReader), (void**) &pReader, NULL)))
    {
        MessageBox(NULL, CStringHelper::Format(L"Error: GetProcAddress() failed to find 'CreateXmlReader' %d\n", GetLastError()).c_str(), L"Error", MB_OK);
        return -1;
    }

    pReader->SetInput(pStream);
}

while (S_OK == (hr = pReader->Read(&nodeType))) 
{
    switch (nodeType) 
    {
                    // parse xml here
    }
}
return 0;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:18:09+00:00Added an answer on May 28, 2026 at 3:18 am

    Your xml string is not correct. There must be “=” between “version” and “1.0”. Secondly, the string uses UTF-16, while the header states it is UTF-8. Change UTF-8 to UTF-16 in header or remove encoding attribute.
    Either

    LPWSTR pwszXMLString = 
    L"<?xml version=\"1.0\" encoding=\"UTF-16\" ?>\r\n"
    

    or

    LPWSTR pwszXMLString = 
    L"<?xml version=\"1.0\"?>\r\n"
    

    works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to open one window from another using makeKeyAndOrderFront. The new window
I'm trying to understand open addressing in hash tables but there is one question
I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.
I am trying to use the amazing DocX library on codeplex to create a
I am trying to stream/pipe a file to the user's browser through HTTP from
In order to improve performance reading from a file, I'm trying to read the
I have developed one windows service through which I am trying to open an
I am trying open a new window using url.Action. And the new Window url
im trying to open a file using StreamReader and ive to set a Encoding,
I trying to open C: directly with FileStream without success: new FileStream(C:, FileMode.Open, FileAccess.Read,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.