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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:24:36+00:00 2026-05-23T11:24:36+00:00

I am trying to call function CreateProcessAsUser. Passing constant string is fine. Trying to

  • 0

I am trying to call function CreateProcessAsUser. Passing constant string is fine. Trying to pick up an Environment variable using char* getenv(const char name) is causing me a problem.

If I use the following, notepad.exe will run.

CreateProcessAsUser(hTokenDup, _T("c:\\windows\\notepad.exe"), 
                    _T("c:\\windows\\notepad.exe"), NULL, NULL, FALSE,
                    dwCreationFlag, pEnvironment, NULL, &si, &pi);

However, if I use the following nothing runs.

CreateProcessAsUser(hTokenDup, _T("MyAppName"), 
                    (LPTSTR)getenv("MYENVVAR"), NULL, NULL, FALSE,
                    dwCreationFlag, pEnvironment, NULL, &si, &pi);

Have I specified the getenv and (LPTSTR) correctly?

I have tried using user and system environment vars containing c:\\windows\\notepad.exe and c:\windows\notepad.exe.

Thanks!

  • 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-23T11:24:37+00:00Added an answer on May 23, 2026 at 11:24 am

    The third parameter, lpCommandLine is LPTSTR which means it must be writeable memory. You need to copy the command line into a writeable string before calling CreateProcessAsUser.

    The documentation for getenv states:

    It is not safe to modify the value of the environment variable using the returned pointer.

    You therefore cannot pass this as the lpCommandLine parameter of CreateProcessAsUser.

    Your first call to CreateProcessAsUser appears to be wrong too since you also are not passing writeable memory for lpCommandLine.

    Of course it’s most likely that your immediate problem is that you are mixing ANSI and Unicode. If your app is Unicode then you need to call _wgetenv, or _tgetenv if you really do want to target both ANSI and Unicode from the same source. But make sure you copy it into a writeable buffer before passing it on.

    Finally, as Adam commented, every time you write a cast, there is a strong possibility that you are making a mistake.

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

Sidebar

Related Questions

In my javascript function I am trying to call my global variable that is
I'm trying to call a function in JavaScript via Java. This works fine when
I'm trying to call a function within a string return statement. Both are in
I'm trying to call a function which writes a very long string of html
I'm trying to call a function within an object literal that I created, using
I am trying to call a function in a class based on the string
I'm trying to call a function, then take the returned variable and display it
I'm trying to call a function only if an HTML element is empty, using
I am trying to call a function that takes char** as a parameter. Its
Trying to call this function in C without any real luck: int sendcommand(char str[256],

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.