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 7075807
In Process

The Archive Base Latest Questions

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

Today I was able to write a simple C++ program that granted a user

  • 0

Today I was able to write a simple C++ program that granted a user the “Log on as a service” privilege. Part of this involved converting between a LPCWSTR and an LSA_UNICODE_STRING. The code to do that is here:

LSA_UNICODE_STRING StringToLsaUnicodeString(LPCWSTR string) {
    LSA_UNICODE_STRING lsaString;
    DWORD dwLen = 0;

    dwLen = wcslen(string);
    lsaString.Buffer = (LPWSTR) string;
    lsaString.Length = (USHORT)((dwLen) * sizeof(WCHAR));
    lsaString.MaximumLength = (USHORT)((dwLen + 1) * sizeof(WCHAR));
    return lsaString;
}

When I had some small errors in this function, my call to LsaLookupNames2() failed with a code 87(hex 0x57) “The parameter is incorrect.” I am trying to make this call in a C++ app that uses std::wstring and it is failing. My current function there is as follows:

#if defined(_UNICODE)
    LSA_UNICODE_STRING toLsaUnicodeString (std::wstring str) {
        LSA_UNICODE_STRING lsaWStr;
        DWORD len = 0;

        LPWSTR cstr = (LPWSTR)str.c_str();
        len = wcslen(cstr);
        lsaWStr.Buffer = cstr;
        lsaWStr.Length = (USHORT)((len) * sizeof(WCHAR));
        lsaWStr.MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR));
        return lsaWStr;
    } 
#endif

What am I doing wrong?

  • 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-28T06:13:19+00:00Added an answer on May 28, 2026 at 6:13 am

    You’re likely encountering a lifetime issue with the wchar_t* returned from str.c_str(). str.c_str() will return a pointer to an underlying string whose lifetime is governed by str. Since str is passed by-value, it will be destroyed at the end of the toLsaUnicodeString function, resulting in the returned LSA_UNICODE_STRING pointing at memory that has been deallocated. In order to avoid this, you’ll need to make a copy of the underlying string in the toLsaUnicodeString function, and associate the copy with the returned LSA_UNICODE_STRING, something like:

    LSA_UNICODE_STRING toLsaUnicodeString (const std::wstring& str) {
        LSA_UNICODE_STRING lsaWStr;
        DWORD len = 0;
    
        len = str.length(); 
        LPWSTR cstr = new WCHAR[len + 1];
        memcpy(cstr, str.c_str(), (len + 1) * sizeof(WCHAR));
        lsaWStr.Buffer = cstr;
        lsaWStr.Length = (USHORT)((len) * sizeof(WCHAR));
        lsaWStr.MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR));
        return lsaWStr;
    }
    

    Since the memory is now allocated on the heap, you are responsible for making sure it is deallocated. You can use a function like the following to take care of this.

    void freeLsaUnicodeString(LSA_UNICODE_STRING& str) {
        delete [] str.Buffer;
        str.Buffer = 0;
        str.Length = 0;
        str.MaximumLength = 0;
    }
    

    Even better would be to use RAII to manage the memory and guarantee that it is released when the variable is no longer in use. See Mr_C64’s answer for details on this approach.

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

Sidebar

Related Questions

Today I discovered that my fresh installation of Apache HTTP Server is able to
Today, I ran into this weird problem with a user using Mac OS X.
I was writing a bit of simple UI stuff today and am only able
I have a very simple VB.net Windows Service written using VS.net 2008. The program
Today somebody told me that interface implementation in C# is just Can-Do relationship, not
So i just started trying to develop a simple webpart today for a sharepoint
This relates to another question I asked earlier today. I built SVN 1.6.2 from
Clarification: this is not about user agent calls to pages, but Classic ASP calling
I would like to write a plug-in that will allow a custom written CRM
I realized today that I don't know how to escape characters in comments for

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.