OpenKey Windows 7 [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what have of wrong here ? Work in my notebook, but not in my PC . . .
The two are 64-bits, Windows 7 ultimate.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
HKEY CH;
char File_Name[] = "C:\\Users\\RMS\\Desktop\\M.txt";
if(RegCreateKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0){
printf("Erro - RegCreateKey\n");
system("PAUSE");
return -1;
}
if(RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&CH) != 0) // Abre a CH "Minha CH"
{
printf("Erro - RegOpenKey\n");
system("PAUSE");
return -1;
}
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",40) != 0)
printf("Erro - RegSetValue\n");
RegCloseKey(CH);
printf("\nsucesso !\n");
system("PAUSE");
return 0;
}
I found. . . Was only do this:
if(RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) L"C:\\Users\\RMS\\Desktop\\M.txt",60) != 0)
VERY THANK GUYS !!

Your problem is that the HKLM registry key is only writable by elevated programs, and your program is not running elevated. The reason it works on one machine and not the other is that one has user access control turned down/off while the other doesn't.
If you ran the program from an elevated command prompt it will work.
Additionally, you're using L"" for the strings, but using a RegSetValueEx call with 40, which is 40 bytes, and will actually cut off the M.txt on the text you're setting (if it works at all). Where you initialize the .txt file you should use:
TCHAR File_Name[] = L"C:\\Users\\RMS\\Desktop\\M.txt";
Then for the RegSetValueEx you do:
RegSetValueEx(CH,L"My_Value",0,REG_SZ,(LPBYTE) File_Name, sizeof File_Name + sizeof(TCHAR))
This makes it the number of bytes that corresponds to the filename, plus the final NULL TCHAR.

what error do you get?
try
RegOpenKeyEx
instead of RegOpenKey, since that's for 16 bit windows.

Related

Move file from one location to another location in window c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Simple code for moving file to another location using window 10 visual studio c++ 2015. In window how to move file from one location to another.
I have a text file at location D:\data.txt . I want to change its loaction to C:\total data\data.txt .
#include <cstdio>
int main (void)
{
std::rename ("old_name", "new_name");
return 0;
}
I already use rename fuction for moving using but it not works further details https://bytes.com/topic/c/answers/132322-file-move-programmatically
Just i want to change location of file.
Changes the name of the file or directory specified by oldname to newname.
If oldname and newname specify different paths and this is supported by the system, the file is moved to the new location.
#include <stdio.h>
int main ()
{
int result;
char oldname[] ="D:\\data.txt";
char newname[] ="C:\\datadull\\newname.txt";
result= rename( oldname , newname );
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
return 0;
}

How to change permissions of a file in Linux with C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to change the permissions of a file with C++ in Linux. The user has to enter the permissions using this syntax: "Please enter the permissions: rwx-w-r--" in the terminal.
Thanks for helping.
#include <sys/types.h>
#include <sys/stat.h>
int main() {
chmod("./myfile", S_IRWXU); // enables owner to rwx file
}
See man 2 chmod for more details.
If the question is how to parse a 9 character string of the form "rwx-w-r--", realize that the permissions are encoded in an int as bits. If a bit is on, that permission is on. The following code will take your string, turn it into bits, in the obvious way...no validation etc. is done. It's a proof-of-concept.
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
int parse(char* perms) {
int bits = 0;
for(int i=0; i<9; i++){
if (perms[i] != '-') {
bits |= 1<<(8-i);
}
}
return bits;
}
int main() {
char perms[]="rwx-w-r--";
int exmp = S_IRWXU | S_IWGRP | S_IROTH;
printf("%d %d\n", parse(perms), exmp);
// outputs 468 468
}

How detect spacebar? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I havenĀ“t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???
Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h

How to add a timer in a C++ program [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game in C++ i.e. hangman in which for entering each letter as an input I want to give user a time limit of 20 seconds. In those 20 seconds if input letter is not given then program ends with display "Time is up".
Remember 'while the timer is running' I want to give input letter.
Is that possible?
Please help through code implementation.
If your platform has conio.h available and your compiler supports C++11 (including <chrono>) you can do it like this:
#include <iostream>
#include <chrono>
#include <conio.h>
int main(int argc, char* argv[]){
std::chrono::time_point<std::chrono::system_clock> start;
start = std::chrono::system_clock::now(); /* start timer */
while(true){
__int64 secondsElapsed = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now()-start).count();
if(secondsElapsed >= 20){ /* 20 seconds elapsed -> leave main lopp */
break;
}
if(_kbhit()){ /* keypressed */
char c = _getch(); /* get character */
std::cout << c; /* output character to stdout */
}
}
return 0;
}
After 20 seconds are passed the programm terminates - until that, you can enter characters to the console window (tested on Windows 7 with Visual Studio 2012).
See also the documentation for _kbhit(), _getch() and <chrono>.

Extract HTML source code linux library [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this
Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.