check mariadb connection with c++ (without dependencies) [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
I got myself a programm where a user types in their mariadb credentials (username/password/port).
I now want the programm to check if this connection is working or if something is wrong.
Right now I am running processes with CreateProcess but since statements like mysql -u root -pwrongpassword will still run through without any errors, this doesn't work.
I want such a statement, or just a generic connection check, to return false when those credentials turn out as wrong.
Important here is that it has to work without any existing software on the target system (except mariadb if necessary for your solution).
What would be a solid practice for that task?

Reinventing the wheel (as suggested by Sam Varshavchik) is not a good idea: It's not just opening a socket, writing and reading data. Depending on the authentication options you have to support SSL/TLS and the various authentication methods (native password, ed25519, sha256_caching_password, sha256_password, gssapi/kerberos) which is quite complex.
Since you mentioned that MariaDB is installed on your target system, you can use the mariadb client library (MariaDB Connector/C), which is also used by mysql command line client. It is installed together with the server package.
#include <mysql.h>
int check_connection(const char *user, const char *password, int port)
{
int rc= 0;
MYSQL *mysql= mysql_init(NULL);
if (mysql_real_connect(mysql, "localhost", user, password, NULL, port, NULL, 0))
rc= 1;
mysql_close(mysql);
return rc;
}
Now you have to link your application against libmariadb.lib (dynamic linking) or mariadbclient.lib (static linking).

Related

Is it possible to create "quick time events" in 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 4 years ago.
Improve this question
I have some knowledge about C++, but I stumbled upon an problem. I want the user to enter some text, but when it takes longer than X seconds, the program will go on. I guess, it is not possible, but maybe you know something about it.
It will be in the command line, without GUI.
I am not certain how the programm will look like, but it will be something like a CMD-RPG. I wanted to use Quick Time Events to make it a little bit more exciting.
I cant comment so I will just leave this here
Input with a timeout in C++
Since I cannot comment, I will simply leave this as an answer.
One possible way to solve this problem is to have 2 threads:
Input capture thread: since receiving input is a thread-blocking action, you should have a thread that simply polls for user input, placing that input into a thread-safe buffer
Quick-time function on main thread: a function that will be responsible for executing the quick-time event. Something like the following could be done (pseudo code):
clear input buffer //the buffer provided by the input capture thread
bool success = false;
while(current time < ending time)
{
if(input buffer isn't empty)
{
get contents of input buffer and send contents to cout
if (user has finished input correctly)
{
success = true;
break;
}
clear buffer
}
}
return success;
For this to work, you would need to turn off echo on the command prompt (see this page for more info)
Although Diogo's reference is excellent (and informative), I believe this answer is more applicable than since the OP has indicated that this program will be ran on Windows.

object is not being saved even after fstream.clear() in 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 5 years ago.
Improve this question
I am using C++ to write a music memorize game for my school project, here the Player objects are not being saved in the PLAYER_DATA.DAT file even i tried clear() function
here is the peace of code (as the whole code is 600+ lines)
Player p_dat,plyr;
plyr.getData();
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
while(P_file.read((char*)&p_dat, sizeof(p_dat)))
{
if(nameEqual(plyr,p_dat))
{
P_file.clear();
gotoxy(1,10);
delline();
textcolor(RED);
cout<<"\t\t EXIXTING PLAYER PROFILE FOUND!\n";
int ch = playPanel("It's me", "Change Name");
if(ch == 0)
{
P_file.seekp(P_file.tellg() - sizeof(Player));
GameStarted = 1;
if(c == 1)
Campaign(p_dat,P_file);
else
Endless(p_dat,P_file);
return;
}
else
{
startGame(c);
return;
}
}
}
P_file.clear();
P_file.seekp(0,ios::end);
P_file.write((char*)&plyr,sizeof(plyr));
Just to make it short, The last line of the code is not doing anything the file already exists and of size 0kb
however,
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary)
P_file.write((char*)&plyr,sizeof(plyr));
is saving the file. Please help me.
EDIT 1.1
finally found this line is problematic
P_file.seekp(0,ios::end);
Its working for code, i.e correctly saving objects
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
P_file.seekp(0,ios::end);
while removing
P_file.seekp(0,ios::end);
making the code look like,
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
after the while loop, is not saving the file
again does not save the object
this line is making problem, are there any alternatives or solutions?
Compile with all warnings and debug info (g++ -Wall -Wextra -g with GCC) then use the debugger (e.g. gdb).
Read carefully documentation of C++ IO functions.
Check that your file has been opened correctly; after:
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
add
if (!P_file) { std::cerr << "failed to open PLAYER_DATA.DAT" << std::endl; };
On some systems (e.g. Linux), you might also display strerror(errno).
After some intermediate call to write, consider using flush.
Be sure that your program is started in the correct working directory.
On Linux, you might also use strace(1) to understand the system calls done by your program.
At last, did you consider using some simple database, e.g. with sqlite, or some indexed file, e.g. with gdbm ?

Repeat system() command in 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 5 years ago.
Improve this question
What is the easiest way to repeat a simple system command on multiple CMD's in C++? For instance, how could I repeat this code on multiple terminal windows from my C++ code?
system( ("ping "+ ip +" -t -l 32").c_str() );
From a networking perspective, I don't think that pinging a single system from multiple terminals will be as effective as pinging that system from multiple remote systems.
Anyways, in regards to the easiest way to ping a system from multiple processes... just use the shell directly. Something like:
target=s4
for remotehost in s1 s2 s3; do (ssh -n $remotehost ping $target -t -l 32 & ) ; done
The "remotehost" doesn't have to really be a remote host either. You can just use "localhost" multiple times (instead of multiple names of remote hosts).
Alternatively if you really want to use C++ from a single host:
#include <cstdlib>
#include <string>
int main()
{
const std::string ip = "foo";
for (int i = 0; i < 3; ++i)
{
std::system(("ping " + ip + " -t -l 32 & ").c_str());
}
}
Note the usage of the ampersand (&) character in the input string to the system function. That instructs the shell to run the given task in the background. That way system returns immediately and the ping command basically runs at the same time as the two other instances of the command.
Hope this helps answer your question.

Why file is not created in /etc/ directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

How can i make my program run automatically in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I export my python code by pyinstaller i need my program to startup automatically whith the windows (i don't need to do that by startup folder)
First you need to create a shortcut. This will create shortcut on your desktop
import os, sys
import pythoncom
from win32com.shell import shell, shellcon
shortcut = pythoncom.CoCreateInstance (
shell.CLSID_ShellLink,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)
desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)
You can make toany of windows locations for startup files ( or create it only in startup place) :
Run Once :HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
Run each Start: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
StartUp folder : C:\Documents and Settings\All Users\Start Menu\Programs\Startup
Shared Task Manager : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SharedTaskScheduler
Make it as a service, and make that service automatic started
To see what programs start automatically on your computer, or to add your entries easy, you can use autoruns from SysInternals, http://technet.microsoft.com/en-us/sysinternals/bb963902.aspx
P.S. Python example is from timgolden.me.uk site.