|
And at some point, you're going to demand a job too, right?
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
You use the resource editor in Visual Studio, which will generate the code for you.
|
|
|
|
|
DELETED SOLVED
modified 8-Jul-22 15:49pm.
|
|
|
|
|
Member 14968771 wrote: no "post (this) at QT forum " -
just do not answer if you do not want to help.
Comments like that are probably what got you kicked off the other sites. If you want help then try a different approach.
|
|
|
|
|
Member 14968771 wrote: DELETED SOLVED Deleted, really?
So no one in the future might learn?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
In my program I'm using system("") command to add a variable into User variables using reg add . In the future I'll want to update the variable value to something else, but if the variable exists it will prompt the user to input Y/N to override it. Is there a way to automatically give the input instead of the user? I was thinking that maybe I could use system("y") , but it still waits for the input from the user, and then gives the error that y is not a command.
I can't use Windows.h to modify registry, so I want to find a solution to this. Maybe I'll need system("") for something else in the future that will have the same problem with having to input something.
Solution:
Victor Nijegorodov wrote: According to the reg add | Microsoft Docs you can add option /f that will cause the adding the registry entry without prompting for confirmation.
modified 7-Jul-22 7:00am.
|
|
|
|
|
I do not understand how system("") can add a variable to anything. I think we need more details.
|
|
|
|
|
He said he's using system("reg add") to do it. I've never used either one, but I assume it's a way for a program to add something to the registry. I think he's now looking for a way for a program to enter "y" on the user's behalf, perhaps to confirm a system command invoked by system() . But I don't know of a way to fake console input. Do you?
|
|
|
|
|
I thought system("") looked a bit silly. I do wish people would just copy and paste the actual code they are using to make their questions clear.
|
|
|
|
|
system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValue\"");
Result: "The operation completed successfully."
And the variable is added with the given value.
But then when wanting to update it:
system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValueUpdated\"");
Result: "Value TestVariable exists, overwrite(Yes/No)?"
If Y is given as input, then "The operation completed successfully.", and the value is updated. But I want to skip the step where the user has to give the Y input, and send it automatically.
|
|
|
|
|
At first I was thinking, well if I can't modify it, what if I try to first delete it and then add it with the new value, but I get in the same situation, and that is I have to give it an input because when you want to delete a variable, you have to input Y/N.
system("reg delete \"HKEY_CURRENT_USER\\Environment\" /v TestVariable");
Result: "Delete the registry value TestVariable (Yes/No)?"
|
|
|
|
|
According to the reg add | Microsoft Docs you can add option /f that will cause the adding the registry entry without prompting for confirmation.
|
|
|
|
|
Yeah, that works. I have no idea how I managed to skip that one line that I needed...
Thanks!
modified 7-Jul-22 7:00am.
|
|
|
|
|
Why are you not using the registry API directly rather than going through (antiquated) system() calls?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I've been tasked with updating a function which currently reads in a configuration file from disk and populates a structure:
static int LoadFromFile(FILE *Stream, ConfigStructure *cs)
{
int tempInt;
...
if ( fscanf( Stream, "Version: %d\n",&tempInt) != 1 )
{
printf("Unable to read version number\n");
return 0;
}
cs->Version = tempInt;
...
}
to one which allows us to bypass writing the configuration to disk and instead pass it directly in memory, roughly equivalent to this:
static int LoadFromString(char *Stream, ConfigStructure *cs)
A couple of things to note:
• The current LoadFromFile function is incredibly dense and complex, reading dozens of versions of the config file in a backward-compatible manner, which makes duplication of the overall logic quite a pain.
• The functions that generate the config file and those that read it originate in totally different parts of the old system and therefore don't share any data structures so I can't pass those directly. I could potentially write a wrapper, but again, it would need to handle any structure passed in a backward-compatible manner.
• I'm tempted to just pass the file as is in as a string (as in the prototype above) and convert all the fscanf's to sscanf's but then I have to handle incrementing the pointer along (and potentially dealing with buffer overrun errors) manually.
• This has to remain in C, so no C++ functionality like streams can help here
Am I missing a superior choice? Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Any pointers, ideas or other assistance is enormously valuable.
|
|
|
|
|
|
Your LoadFromString method could write the string to a temporary file, open the file for input and pass the FILE* to LoadFromFile . That way you would not require any changes to the existing code.
|
|
|
|
|
sahil Ranka wrote: Is there a good method for making a FILE * that simply focuses to an area in memory rather than on a disk? Is mmap() of any value here?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I need to do the following request within a C++ app (response is json):
curl --header "x-api-key:ABCD" -s https:
1. Can I replace curl with another protocol ? (This is not mandatory, could be as it is now)
2. Is there a header only c++ lib that can complete this request ? Of course, I know there is REST SDK library, but it is too much trouble for that simple request.
Thank you.
modified 4-Jul-22 10:07am.
|
|
|
|
|
curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.
#include <cstdio>
int main()
{
FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while( {
}
fclose(pipe);
}
Keep Calm and Carry On
|
|
|
|
|
Hmm ... I tried:
FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (true)
{
pipe->_Placeholder;
break;
}
fclose(pipe);
but I cannot retrieved any data from pipe ...
|
|
|
|
|
using a C style FILE * you have to use C stdio functions. So for example you could do
#include <stdio>
#include <cstdlib>
FILE *pipe = popen( ... );
char *buff = NULL;
size_t blen = 0;
while( getline(&buff, &blen, pipe) > 0) {
}
free(buff);
I generally prefer getline() to fgets() when using cstdio because it allocates and grows the input buffer as needed.
If you wish to stick with a more C++ style interface, then maybe look into Boost.Process
I think you've mentioned QT as part of your framework, so there's QProcess as well.
Keep Calm and Carry On
|
|
|
|
|
The C++ app is MFC app ...
|
|
|
|
|
For some reason, I was assuming you were using linux. If you're on windows, there's no getline in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function.
It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up Windows HTTP Services - Win32 apps | Microsoft Docs , but that appears to be a Win32 API, so may not be applicable to your situation. There's also GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.
Keep Calm and Carry On
|
|
|
|
|