QProcess gpg2 error in windows - gnupg

I need to encrypt some text with gpg using Qt. QCA is no choice.
My function is working in Linux but in Windows I get exitCode 2.
Executing the gpg2.exe with the same arguments on the commandline is working like expected.
QString PGPProcess::encrypt(QString toAddress, QString message)
{
this->start(QStringList() << "--trust-model always" << "-a" << "-e" << "-r " + toAddress);
this->waitForStarted(PROCESS_WAIT);
this->write(message.toUtf8());
this->waitForBytesWritten(PROCESS_WAIT);
this->closeWriteChannel();
this->waitForFinished(PROCESS_WAIT);
if(this->exitCode()) {
qDebug() << this->exitCode();
return message;
} else {
return this->readAllStandardOutput();
}
}
I think it hast to do with closeWriteChannel(). Commandline has to send CRTL-D (Linux) that gpg is encrypting the written text.
In Windows CTRL-D does not do anything, there I have to press CTRL-C.
But calling terminate() on the QProcess does not work too.

Ok simply changing the arguments QStringList to the following does the trick ...
this->start(QStringList() << "--trust-model" << "always" << "-a" << "-e" << "-r" << toAddress);

Related

Windows RegSetValueEx Funtion Error with REG_SZ C++

HKEY hkey;
LPCSTR data = "HelloWrold\0";
if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sample", 0,KEY_ALL_ACCESS, &hkey))
{
cout << "Registry Open SUccess" << endl;
if (ERROR_SUCCESS == RegSetValueEx(hkey, L"NAME", 0, REG_SZ, (LPBYTE)(data), strlen(data)+1))
cout << "Value Set Success" << endl;
else
cout << "Value Set Failed" << endl;
}
else
cout << "Registry Open Failed " << GetLastError() << endl;
when i use this code the code works fine, but in registry I'm not getting Helloworld but getting some chinese characters, any suggestion what to do?
Why does misdetected Unicode text tend to show up as Chinese characters?
If you take an ASCII string and cast it to Unicode, the results are usually nonsense Chinese.
Your project setting are probably defining UNICODE and your call to RegSetValueEx ends up calling RegSetValueExW but you are passing it a narrow C-style string.
Change it to RegSetValueExA(hkey, "NAME", 0, REG_SZ, (LPBYTE)(data), strlen(data)+1) or change data to be a Unicode string (WCHAR/LPWSTR).
You can catch this sort of error in C++ by changing (LPBYTE)(data) to (LPBYTE)const_cast<LPTSTR>(data).

FLTK windows in Mac OS X share the same system menu

When some application in OS X has multiple windows (many open documents, each in own window), it seems that all them share the same system menu, at least in FLTK. Is there a way to find the most recent selected window to send an event to it from the menu?
Here is my setup (Mac OS X 10.6.2, FLTK 1.3.3): have Shell class with system menu. Each time new document is opened, new Shell is created:
#ifdef __APPLE__
void Shell::macOpen(const char *fileName)
{
// If there are empty shell, open the model in it
if (s_empty != 0)
{
...
s_empty = 0;
}
// Otherwise, create new shell with the model
else
{
char *args[1];
args[0] = (char *) fileName;
new Shell(1, args, s_conf, s_dct, fileName, 1);
}
}
#endif
Then I keep track on the most recently selected Shell saving it into static Shell *Shell::s_current:
int Shell::handle(int event)
{
...
case FL_FOCUS:
#ifdef __APPLE__
// We just selected some shell, it is current.
s_current = this;
cout << "Select shell with address: [" << s_current << "]" << endl;
#endif
return 1;
...
}
This piece seems to work, as I can see the traces each time I select some Shell:
Select shell with address: [0x8206db0]
Select shell with address: [0x82375f0]
Select shell with address: [0x5d20650]
Select shell with address: [0x82375f0]
Now, given:
Shell *Shell::currentShell(Fl_Widget *w)
{
cout << "Widget address: [" << w << "]" << endl;
Shell *result = 0;
if (w != 0)
{
result = (Shell *) w->window();
cout << "Widget wingow address: [" << result << "]" << endl;
}
#ifdef __APPLE__
else
{
result = s_current;
cout << "Last selected shell address: [" << result << "]" << endl;
}
#endif
return result;
}
I have some callback:
void Shell::shortcutCB(Fl_Widget *w, void *data)
{
cout << "Shortcut" << endl;
Shell *ref = currentShell(w);
if (ref != 0)
{
...
}
}
When this callback is executed from the menu and more Shells are open, I get the following error:
Bus error
With no trace from either Shell::shortcutCB or Shell::currentShell. When the only Shell is open, all works perfectly. When more Shells are open and I close all but one, the error arises again. There is no problems when the same callback is called from some widget within Shell or is delivered from keyboard shortcut.
Solved the problem with the following 3 steps:
Declaring manu bar in OS X also static (here was the fail):
#ifdef __APPLE__
static Fl_Sys_Menu_Bar *s_menubar;
#else
Fl_Sys_Menu_Bar *m_menubar;
#endif
Save current Shell::s_current not only on FL_FOCUS event, but on any event handled by Shell, that is, each time it returns 1:
int Shell::handle(int event)
{
int result = 0;
switch (event)
{
// Set result = 1 when handling the event
...
}
#ifdef __APPLE__
if (result == 1)
{
// We just selected some shell, it is current.
s_current = this;
cout << "Select shell with address: [" << s_current << "]" << endl;
}
#endif
return result;
}
Use Shell::s_current for menu callbacks on OS X regardless of generating widget of the call:
Shell *Shell::currentShell(Fl_Widget *w)
{
Shell *result = 0;
#ifdef __APPLE__
result = s_current;
cout << "Last selected shell address: [" << result << "]" << endl;
#else
cout << "Widget address: [" << w << "]" << endl;
if (w != 0)
{
result = (Shell *) w->window();
cout << "Widget wingow address: [" << result << "]" << endl;
}
#endif
return result;
}

Running program from debug folder doesn't read input from file while on compile with VS2010 in runs properly

So i have a program which reads the input out of a specific .txt file. The code is:
void Image::get_image_dimensions(char *fname)
{
// determine the number of entries in image
ifstream fin(fname);
fin >> num_rows ;
fin >> num_columns ;
cout << "...reading from file " << fname << endl;
cout << "File has " << num_columns << " rows and "<< num_columns << " columns" << endl;
fin.close();
}
the method is called inside the main.
After I compile the program with VS2010 and run the code everything works properly. But after if I go to the Debug folder of my program and run my program from there it doesn't read the input anymore, and crashes...
What might be the problem?
The current directory is different in the two cases. The best solution is to provide the full path to the file, not just the file name.

Multiple applications write to one console - mixed/messed output

I have the following system architecture (cannot be changed - legacy code): One main application invokes one or more other applications and these applications interact over a IP protocol.
All applications write to one console window. Unfortunately the console output can get messed up (one character from app 1, next char from app 2, next character from app 4 etc.).
All applications write to console via one Logger.dll (provides static logging functions) using cout/cerr.
Is there a way how I can prevent mixed logging messages in this setup?
Thanks in advance.
EDIT code added:
void Logger::Log(const std::string & componentName, const std::string & Text, LogLevel logLevel, bool logToConsole, bool beep)
{
std::ostringstream stream;
switch (logLevel)
{
case LOG_INFO:
if (logToConsole)
{
stream << componentName << ": INFO " << Text;
mx_console.lock(); // this is a static boost::mutex
std::cout << stream.str() << std::endl;
std::cout.flush();
mx_console.unlock();
}
break;
case LOG_STATUS:
stream << componentName << ": STATUS " << Text;
mx_console.lock();
std::cout << stream.str() << std::endl;
std::cout.flush();
mx_console.unlock();
break;
case LOG_WARNING:
stream << componentName << ": WARNING " << Text;
mx_console.lock();
std::cout << stream.str() << std::endl;
std::cout.flush();
mx_console.unlock();
break;
default:;
}
if (beep)
Beep( 500, 50 );
}
Since you have separate logging functionality you can at minimum use some kind of locking (global mutex, etc.) to avoid interspersing messages from different applications too much. To make it more readable and grepable, add some identifying information, like process name or PID. Wrapping your Logger.dll around existing logging library sounds like an option as well.
Alternatively, you could have logging functions just forward messages to your main application and let that to sort out the synchronization and interspersing.
Syslog might be a solution for you as it is intended to handle logs from various places. Syslog is developed for unix, but this answer shows versions for windows.
You can change your logger to log to syslog instead of the console.
I replaced now all the
std::cout << stream.str();
statements with
std::string str = stream.str();
printf(str.c_str());
and now the output isn't messed up character-wise anymore.
But I don't have a good explanation for this behavior, does anybody know why?

Get a process executable name from process ID

I am currently trying to get the names of a list of programs whose pid I have.
The program is run as administrator, but GetModuleFileNameEx fails with error code 5.
I open the program with OpenProcess(PROCESS_TERMINATE,PROCESS_QUERY_INFORMATION) and I have the SE_DEBUG_PRIVILEGE enabled.
The process handle passed to GetModuleFileNameEx() requires PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights.
This worked for me:
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
6088);
if (0 == h)
{
std::cerr << "OpenProcess() failed: " << GetLastError() << "\n";
}
else
{
char exe_path[2048] = {};
if (GetModuleFileNameEx(h, 0, exe_path, sizeof(exe_path) - 1))
{
std::cout << exe_path << "\n";
}
else
{
std::cerr << "GetModuleFileNameEx() failed: " <<
GetLastError() << "\n";
}
CloseHandle(h);
}
However, as others have pointed out (and is also stated in documentation for GetModuleFileNameEx()) there are safer ways to acquire this information:
GetProcessImageFileName()
QueryFullProcessImageName()
According to this thread that error is returned when there's not enough information to return the filename.

Resources