Defined environment variables in .bat aren't assigned if we launch an administator-required application












0















The following problem appears only on Windows 10 and not on Windows 7.



I have an app (C++, Visual studio 2017), that requires administrator rights (flag /level='requireAdministrator' on the Linker). Here, I try to get environment variable RESOURCES:



Sample



  int main() {
const char* buf;
buf = getenv("RESOURCES");
if (buf)
cout << buf << endl;
else
cout << 0 << endl;

system("pause");

return 0;
}


On Windows 7, I used the following batch file without running it as administrator:



@echo off

set WORKING_DIR=%~dp0
set APP=%WORKING_DIR%ConsoleApplicationTest.exe
set RESOURCES=%WORKING_DIR%resources

start "" "%APP%"


As a result - on Windows 10 RESOURCES variable in the c++ code is empty, on the Windows 7 - correct path to the resources. Is the only solution on Windows 10 to launch the batch file as admin always?










share|improve this question

























  • Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

    – CatCat
    Nov 21 '18 at 8:07













  • @CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

    – N. Zolotavin
    Nov 21 '18 at 9:03











  • Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

    – Gerhard Barnard
    Nov 21 '18 at 9:22













  • @CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

    – Mofi
    Nov 21 '18 at 9:36








  • 1





    @N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

    – Mofi
    Nov 21 '18 at 9:54
















0















The following problem appears only on Windows 10 and not on Windows 7.



I have an app (C++, Visual studio 2017), that requires administrator rights (flag /level='requireAdministrator' on the Linker). Here, I try to get environment variable RESOURCES:



Sample



  int main() {
const char* buf;
buf = getenv("RESOURCES");
if (buf)
cout << buf << endl;
else
cout << 0 << endl;

system("pause");

return 0;
}


On Windows 7, I used the following batch file without running it as administrator:



@echo off

set WORKING_DIR=%~dp0
set APP=%WORKING_DIR%ConsoleApplicationTest.exe
set RESOURCES=%WORKING_DIR%resources

start "" "%APP%"


As a result - on Windows 10 RESOURCES variable in the c++ code is empty, on the Windows 7 - correct path to the resources. Is the only solution on Windows 10 to launch the batch file as admin always?










share|improve this question

























  • Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

    – CatCat
    Nov 21 '18 at 8:07













  • @CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

    – N. Zolotavin
    Nov 21 '18 at 9:03











  • Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

    – Gerhard Barnard
    Nov 21 '18 at 9:22













  • @CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

    – Mofi
    Nov 21 '18 at 9:36








  • 1





    @N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

    – Mofi
    Nov 21 '18 at 9:54














0












0








0








The following problem appears only on Windows 10 and not on Windows 7.



I have an app (C++, Visual studio 2017), that requires administrator rights (flag /level='requireAdministrator' on the Linker). Here, I try to get environment variable RESOURCES:



Sample



  int main() {
const char* buf;
buf = getenv("RESOURCES");
if (buf)
cout << buf << endl;
else
cout << 0 << endl;

system("pause");

return 0;
}


On Windows 7, I used the following batch file without running it as administrator:



@echo off

set WORKING_DIR=%~dp0
set APP=%WORKING_DIR%ConsoleApplicationTest.exe
set RESOURCES=%WORKING_DIR%resources

start "" "%APP%"


As a result - on Windows 10 RESOURCES variable in the c++ code is empty, on the Windows 7 - correct path to the resources. Is the only solution on Windows 10 to launch the batch file as admin always?










share|improve this question
















The following problem appears only on Windows 10 and not on Windows 7.



I have an app (C++, Visual studio 2017), that requires administrator rights (flag /level='requireAdministrator' on the Linker). Here, I try to get environment variable RESOURCES:



Sample



  int main() {
const char* buf;
buf = getenv("RESOURCES");
if (buf)
cout << buf << endl;
else
cout << 0 << endl;

system("pause");

return 0;
}


On Windows 7, I used the following batch file without running it as administrator:



@echo off

set WORKING_DIR=%~dp0
set APP=%WORKING_DIR%ConsoleApplicationTest.exe
set RESOURCES=%WORKING_DIR%resources

start "" "%APP%"


As a result - on Windows 10 RESOURCES variable in the c++ code is empty, on the Windows 7 - correct path to the resources. Is the only solution on Windows 10 to launch the batch file as admin always?







c++ batch-file windows-10 environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:09







N. Zolotavin

















asked Nov 21 '18 at 8:00









N. ZolotavinN. Zolotavin

266




266













  • Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

    – CatCat
    Nov 21 '18 at 8:07













  • @CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

    – N. Zolotavin
    Nov 21 '18 at 9:03











  • Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

    – Gerhard Barnard
    Nov 21 '18 at 9:22













  • @CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

    – Mofi
    Nov 21 '18 at 9:36








  • 1





    @N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

    – Mofi
    Nov 21 '18 at 9:54



















  • Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

    – CatCat
    Nov 21 '18 at 8:07













  • @CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

    – N. Zolotavin
    Nov 21 '18 at 9:03











  • Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

    – Gerhard Barnard
    Nov 21 '18 at 9:22













  • @CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

    – Mofi
    Nov 21 '18 at 9:36








  • 1





    @N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

    – Mofi
    Nov 21 '18 at 9:54

















Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

– CatCat
Nov 21 '18 at 8:07







Here we go again. Everyday. DON'T USE START. What is wrong with set resources=%~dp0resources then %~dp0consoleapplicationtest.exe If they contain spaces enclose with quotes.

– CatCat
Nov 21 '18 at 8:07















@CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

– N. Zolotavin
Nov 21 '18 at 9:03





@CatCat, Thanks for addition, but can you explain, why don't use start? I can't google it. Related to the topic - it doesn't solve a problem.

– N. Zolotavin
Nov 21 '18 at 9:03













Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

– Gerhard Barnard
Nov 21 '18 at 9:22







Well, you specify very specifically that you require Administrator rights to run it, then you ask if you require administrator rights. Are you not answering your own question??

– Gerhard Barnard
Nov 21 '18 at 9:22















@CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

– Mofi
Nov 21 '18 at 9:36







@CatCat Not good are %~dp0resources and %~dp0consoleapplicationtest.exe because of drive and path string referenced with %~dp0 always ends with a backlash. So concatenating this dynamic path string with an additional and a file or folder name results in having finally two backslashes in series in full qualified file/folder name. The Windows kernel functions for file/folder accesses automatically correct this small error, but it is nevertheless better using %~dp0resources and %~dp0consoleapplicationtest.exe although not so good readable, but being in real 100% right.

– Mofi
Nov 21 '18 at 9:36






1




1





@N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

– Mofi
Nov 21 '18 at 9:54





@N.Zolotavin Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? It explains in detail and with an example how Windows manages environment variables. Why does your application depend on environment variables set before by another application. Why do you not pass the resources path as command line argument to your application? Of course that would require changing int main() to int main(int argc, *argv) and making use of argument count and argument values.

– Mofi
Nov 21 '18 at 9:54












1 Answer
1






active

oldest

votes


















1














Your problem is that if the cmd instance running the batch file is not elevated, but your executable file requires elevation then the Application Information Service/UAC processes will handle the new process creation and will not transfer the cmd environment block (more here and here).



So, without changing the program code you only have two options: execute the cmd instance as administrator or set the variable as a system environment variable (yes, that requires an elevated process)






share|improve this answer
























  • The links are very helpful. Thank you.

    – N. Zolotavin
    Nov 21 '18 at 11:47











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407549%2fdefined-environment-variables-in-bat-arent-assigned-if-we-launch-an-administat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your problem is that if the cmd instance running the batch file is not elevated, but your executable file requires elevation then the Application Information Service/UAC processes will handle the new process creation and will not transfer the cmd environment block (more here and here).



So, without changing the program code you only have two options: execute the cmd instance as administrator or set the variable as a system environment variable (yes, that requires an elevated process)






share|improve this answer
























  • The links are very helpful. Thank you.

    – N. Zolotavin
    Nov 21 '18 at 11:47
















1














Your problem is that if the cmd instance running the batch file is not elevated, but your executable file requires elevation then the Application Information Service/UAC processes will handle the new process creation and will not transfer the cmd environment block (more here and here).



So, without changing the program code you only have two options: execute the cmd instance as administrator or set the variable as a system environment variable (yes, that requires an elevated process)






share|improve this answer
























  • The links are very helpful. Thank you.

    – N. Zolotavin
    Nov 21 '18 at 11:47














1












1








1







Your problem is that if the cmd instance running the batch file is not elevated, but your executable file requires elevation then the Application Information Service/UAC processes will handle the new process creation and will not transfer the cmd environment block (more here and here).



So, without changing the program code you only have two options: execute the cmd instance as administrator or set the variable as a system environment variable (yes, that requires an elevated process)






share|improve this answer













Your problem is that if the cmd instance running the batch file is not elevated, but your executable file requires elevation then the Application Information Service/UAC processes will handle the new process creation and will not transfer the cmd environment block (more here and here).



So, without changing the program code you only have two options: execute the cmd instance as administrator or set the variable as a system environment variable (yes, that requires an elevated process)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 11:35









MC NDMC ND

58.7k54882




58.7k54882













  • The links are very helpful. Thank you.

    – N. Zolotavin
    Nov 21 '18 at 11:47



















  • The links are very helpful. Thank you.

    – N. Zolotavin
    Nov 21 '18 at 11:47

















The links are very helpful. Thank you.

– N. Zolotavin
Nov 21 '18 at 11:47





The links are very helpful. Thank you.

– N. Zolotavin
Nov 21 '18 at 11:47


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407549%2fdefined-environment-variables-in-bat-arent-assigned-if-we-launch-an-administat%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?