|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
If I have a for loop, the program will allocate memory for an int every frame. When the for loop is done will the memory be released or will it result in garbage that piles up every frame.
int main()
{
for(int anint = 0; anint < 100; anint++)
{
}
}
modified yesterday.
|
|
|
|
|
This memory is allocated on the stack and will be "released" after the loop finishes.
|
|
|
|
|
The program knows I don’t need the integer after the loop is done, thanks.
|
|
|
|
|
|
I suspect maybe that is a bit different especially given the OP.
C, so before C++, would limit the scope to the method. C++ would have been based on the same. So if one has 3 for loops in one method with each in a block for a if statement, the compiler might or might not have limited it to the block.
What you refer to makes that explicit. The compiler thus must reuse the stack space.
The OP though is referring both the scope which is a method and, I believe, to each iteration of the for loop.
|
|
|
|
|
jschell wrote: C, so before C++, would limit the scope to the method. No, C89, C90 through until C98 had method scope.
jschell wrote: So if one has 3 for loops in one method with each in a block for a if statement, the compiler might or might not have limited it to the block.
The ISO standards are listed at the bottom of this page. Could you point out what you are referring to?
I see what you are saying now. You are describing old C89 rules. Are you an embedded C programmer? Those scope rules were changed way back in 1999.
Yeah, modern compilers don't have method scope at all anymore. Some embedded compilers still do C89
modified 2 days ago.
|
|
|
|
|
Randor wrote: Could you point out what you are referring to?
I should have read your original post more carefully....
The following is part of what the OP posted.
"allocate memory for an int every frame."
To me the terms I underlined are significant.
Most compilers that I have ever seen, not just C/C++, use a 'stack frame' to manage the variables within a method.
The allocation, far as I can tell, is how the OP is referring to, because the post specifically uses those terms.
But it is still up to the compiler. As a matter of fact at least at one time compilers at one time made a big deal (advertising) that the method variables were managed as CPU 'register' values and were not put onto the stack frame at all. And that is definitely not in the specifications for C or C++.
I do know, because I looked at the assembly that compilers used to emit (and at times modified it) that compilers at one time did nothing more than allocate variables on the stack frame sequentially. I might even recall reading an article that a developer would need to manage variables more carefully to limit that. (I can perhaps recall the suggestion that all variables should be declared at the top of the method for that very reason.)
Now back to what you posted...
For what you posted the "scope" refers to where the variable is visible from. At the language level.
That does NOT specify how the compiler is to manage the variables.
Do you have a different specification, which would probably need to be after C99, that does state how the stack frame is to be managed? I say C99 since I was familiar with that one and I am rather certain that it says nothing at all about the stack frame.
I also looked through my books for "C++ Programming Language" and "C Programming Language" and found nothing at all about the stack frame. I did not expect to find it.
I did look through the Dragon book where I would expect this to be discussed. It doesn't use that term instead it uses the term 'activation record'. It discusses how the activation record can be managed by a stack.
|
|
|
|
|
The C99 standard is here: C99 Standard
Scope starts at 6.2.1
But you would probably want to read: 6.2.4 Storage durations of objects
I use to read all these many years ago, I stopped reading the specs after C11
At Microsoft we had a huge internal mailing list where everyone got to watch the compiler team go back and forth over the new language features. The gatekeeper of the STL library had the initials S.T.L. I always thought that was funny.
|
|
|
|
|
Randor wrote: 6.2.4 Storage durations of objects
But 6.2.4 says nothing about how a stack frame is built.
Just as with your other reference it explains what the compiler must enforce but not how it must enforce it.
Following is the only thing that relates to the language
4 An object whose identifier is declared with no linkage and without the storage-class specifier statich asautomatic storage duration.
5 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in anyway.(Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.
The second paragraph is the only one even close to relevant and basically makes the same point as your other reference.
It does not specify how it is built on the stack frame.
Again a compiler writer could make a fully compliant compiler which used new slots on the stack frame for each block. Or it could reuse existing ones. Both implementations are compliant.
The referenced section allows a compiler writer to produce optimized code that reuses the slots. And they cannot be considered non-compliant if someone attempts to use a declared variable in a block outside the block (for example via a pointer.)
Consider exactly that case - using a pointer outside the block.
1. Compiler A uses new slots so the code works.
2. Compiler B reuses slots so the code doesn't work.
The user complains that Compiler B is non-compliant. The creators can tell them explicitly that they are using code that the spec does not support.
But it says nothing about Compiler A. Compiler A is NOT required to attempt to determine that a pointer belongs to a variable that goes out of scope. (Similar to having a method return a pointer to a local variable.) The compiler might choose to warn about that but is not required to do so.
|
|
|
|
|
Thanks for writing all that. The ironic part of this conversation is that the answer to the question at the top of this thread been the same since C99.
|
|
|
|
|
Calin Negru wrote: the program will allocate memory for an int every frame.
No, it does not. An int is a "value type", and value types are usually allocated on the stack, not the heap. Also, in your example, the anint variable is only allocated (or "pushed" onto the stack once, upon execution of the loop initializer. The value in that stack location is changed on every iteration of the loop. Once the loop is complete, that variable is popped off the stack and will no longer exist.
Of course, all of this is a bit generalized and is not accurate in all cases. It is possible to allocate a value type on the heap, generally called "boxing".
|
|
|
|
|
|
The typical way for functions to work is allocating whatever stack space they need once in the prologue. But there's no need for anint to be in memory here. Or to exist at all for that matter, since the loop trivially doesn't do anything and can be skipped. MSVC compiles this function like this, anint isn't anywhere, not in memory, not in a register, just gone.
If it was going to be in memory, then only one instance of it needs to exist, so that's what happens. Or should happen anyway. If a compiler individually allocated separate copies of that variable for each iteration, I would file a bug report.
|
|
|
|
|
|
I'm using the following function for years and used it under any Version of Windows since XP. It creates a systemtask with the start type set to "auto", just like it is expected to do. But under windows 11, the start type of the installed service always defaults to "manual" when being created. Any help solving this is very much appreciated. Am using VS2022 using toolset 1.41_XP (for reasons)
(already added some extra check for win 11... but still the start type defaults to manual in the newly created task)
static int manage_service(int action) {
SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_DESCRIPTION descr = { server_name };
char path[PATH_MAX + 20];
int success = 1;
GetModuleFileName(NULL, path, sizeof(path));
strncat(path, " ", sizeof(path));
strncat(path, service_magic_argument, sizeof(path));
if (IsRunAsAdministrator()) {
if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
GENERIC_WRITE : GENERIC_READ)) == NULL) {
success = 0;
show_error();
}
else if (action == ID_INSTALL_SERVICE) {
hService = CreateService(hSCM, service_name, service_name,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, NULL, NULL, NULL, NULL, NULL);
if (hService) {
ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion = 11;
if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
}
else {
show_error();
}
}
else if (action == ID_REMOVE_SERVICE) {
if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
!DeleteService(hService)) {
show_error();
}
}
else if ((hService = OpenService(hSCM, service_name,
SERVICE_QUERY_STATUS)) == NULL) {
success = 0;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
else {
if (action == ID_INSTALL_SERVICE) {
RunServiceAsAdmin('I', path, service_name);
}
else if (action == ID_REMOVE_SERVICE) {
RunServiceAsAdmin('R', path, service_name);
}
else {
if ((hSCM = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL) {
success = 0;
show_error();
}
if ((hService = OpenService(hSCM, service_name,
SERVICE_QUERY_STATUS)) == NULL) {
success = 0;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
}
return success;
}
|
|
|
|
|
Hmmm,
Your code looks good to me. Although it would be nice if you captured the return values of ChangeServiceConfig .
I would recommend debugging this by checking the Event logs. Look for event ID 7040 in the "Service Control Manager" log source. You might need to enable auditing.
Also, try temporarily adding a Windows Defender exclusion on the service file path if your executable is unsigned/untrusted. I'm wondering if Defender is blocking the change.
|
|
|
|
|
 Just found out somethhing more.... As soon as I invoke the service creation function from within the program, I do get the normal service controll manager asking for elevated rights in order to create the service, what is exactly what happen. But then the service gets created with start type set to "manual".
If I do start the program manually "as Administrator" and then invoke the service creation function, the service gets created correctly with start type "auto". So there probably might be a problem with my elevation of rights!?... will check this. Strange though, that it works fin under any Windows version since XP... just not windows 11...
Here is the code to start with elevated rights:
BOOL IsRunAsAdministrator()
{
BOOL isRunAsAdmin = FALSE;
DWORD dwError = ERROR_SUCCESS;
PSID pAdministratorsGroup = NULL;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdministratorsGroup))
{
goto Cleanup;
}
if (!CheckTokenMembership(NULL, pAdministratorsGroup, &isRunAsAdmin))
{
goto Cleanup;
}
Cleanup:
if (pAdministratorsGroup)
{
FreeSid(pAdministratorsGroup);
pAdministratorsGroup = NULL;
}
return isRunAsAdmin;
}
void RunServiceAsAdmin(char ch, const char *program, const char* name)
{
char param[255];
SHELLEXECUTEINFO sei = { sizeof(sei) };
memset(param, 0 , sizeof(param));
sei.lpVerb = "runas";
sei.lpFile = "sc.exe";
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
if(ch == 'I')
{
sprintf(param, "create \"%s\" binPath= \"%s\" DisplayName=\"%s\"", name, program, name);
}
else
{
sprintf(param, "delete \"%s\"", name);
}
sei.lpParameters = param;
if (!ShellExecuteEx(&sei))
{
show_error();
}
}
modified 4 days ago.
|
|
|
|
|
Well,
You appear to have a function that is checking if you are running as Administrator. Could you show me the content of that function?
|
|
|
|
|
 Here is some more complete code:
BOOL IsRunAsAdministrator()
{
BOOL isRunAsAdmin = FALSE;
DWORD dwError = ERROR_SUCCESS;
PSID pAdministratorsGroup = NULL;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdministratorsGroup))
{
goto Cleanup;
}
if (!CheckTokenMembership(NULL, pAdministratorsGroup, &isRunAsAdmin))
{
goto Cleanup;
}
Cleanup:
if (pAdministratorsGroup)
{
FreeSid(pAdministratorsGroup);
pAdministratorsGroup = NULL;
}
return isRunAsAdmin;
}
void RunServiceAsAdmin(char ch, const char *program, const char* name)
{
char param[255];
SHELLEXECUTEINFO sei = { sizeof(sei) };
memset(param, 0 , sizeof(param));
sei.lpVerb = "runas";
sei.lpFile = "sc.exe";
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
if(ch == 'I')
{
sprintf(param, "create \"%s\" binPath= \"%s\" DisplayName=\"%s\"", name, program, name);
}
else
{
sprintf(param, "delete \"%s\"", name);
}
sei.lpParameters = param;
if (!ShellExecuteEx(&sei))
{
show_error();
}
}
static int manage_service(int action) {
SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_DESCRIPTION descr = { server_name };
char path[PATH_MAX + 20];
int success = 1;
GetModuleFileName(NULL, path, sizeof(path));
strncat(path, " ", sizeof(path));
strncat(path, service_magic_argument, sizeof(path));
if (IsRunAsAdministrator()) {
if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
GENERIC_WRITE : GENERIC_READ)) == NULL) {
success = 0;
show_error();
}
else if (action == ID_INSTALL_SERVICE) {
hService = CreateService(hSCM, service_name, service_name,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, NULL, NULL, NULL, NULL, NULL);
if (hService) {
ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion = 11;
if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}
}
else {
show_error();
}
}
else if (action == ID_REMOVE_SERVICE) {
if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
!DeleteService(hService)) {
show_error();
}
}
else if ((hService = OpenService(hSCM, service_name,
SERVICE_QUERY_STATUS)) == NULL) {
success = 0;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
else {
if (action == ID_INSTALL_SERVICE) {
RunServiceAsAdmin('I', path, service_name);
}
else if (action == ID_REMOVE_SERVICE) {
RunServiceAsAdmin('R', path, service_name);
}
else {
if ((hSCM = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL) {
success = 0;
show_error();
}
if ((hService = OpenService(hSCM, service_name,
SERVICE_QUERY_STATUS)) == NULL) {
success = 0;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
}
return success;
}
static LONG queryServiceStatus(const char* serviceName){
SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwBytesNeeded;
LONG status = 0;
if ((hSCM = OpenSCManager(NULL, NULL, GENERIC_READ)) == NULL) {
show_error();
return 0;
}
hService = OpenService(
hSCM,
serviceName,
SERVICE_QUERY_STATUS);
if (hService == NULL)
{
goto END_QUERY;
}
if (!QueryServiceStatusEx(
hService,
SC_STATUS_PROCESS_INFO,
(LPBYTE) &ssStatus,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded ) )
{
goto END_QUERY;
}
if(ssStatus.dwCurrentState == SERVICE_RUNNING)
{
status = SERVICE_RUNNING;
goto END_QUERY;
}
modified 4 days ago.
|
|
|
|
|
I do see a bug.
SHELLEXECUTEINFO sei = { sizeof(sei) };
You should zero that struct out. Then set the cbSize member. Not sure if this is causing your problem though.
I'm on my TV right now so reviewing on my couch. But don't see any other issues.
|
|
|
|
|
Thanks for the tip!
Am also currently looking into a way to use the ControlService utility via CreateProcess to install the service, instead of calling CreateService directly... But not sure if I can pull that off correctly...
|
|
|
|
|
I just noticed in your original post:
Rick R. 2023 wrote:
Am using VS2022 using toolset 1.41_XP
Do you get the same behavior if you compile for Windows 11?
|
|
|
|
|
Starting to suspect the toolset, too.
Have to fix a bunch of linker problems caused by my old and pretty messy project settings, in order to test with a newer version... might take a while.
|
|
|
|
|