Need to find java version number using c++ program - visual-c++

I'm new to C++ programming. So, which libraries or functions should I use in retrieving this info from the registry? Just give me a brief idea about the steps involved in retrieving the java version number from registry. I'm using VC++.

If java paths are properly set, you could just run java -version from code:
Using the code described here How to execute a command and get output of command within C++ using POSIX? :
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(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;
}
using like:
int main(void) {
std::cout << exec("java -version");
return 0;
}

Related

C++ portability from Windows to Linux

I have been successfully using the following code in C++ on Windows (via CodeBlocks) and have recently attempted to use the same code on Linux (Ubuntu 18.04) also via CodeBlocks. The code appears to compile fine but fails on execution.
The purpose of the code is to import a comma delimited text file of numbers into an array.
In both Windows and Linux I am using the GNU GCC Compiler.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <stdlib.h>
using namespace std;
typedef vector <double> record_t;
typedef vector <record_t> data_t;
istream& operator >> ( istream& ins, record_t& record)
{
record.clear();
string line;
getline( ins, line );
stringstream ss( line );
string field;
while (getline( ss, field, ',' ))
{
stringstream fs( field );
double f = 0.0;
fs >> f;
record.push_back( f );
}
return ins;
}
//-----------------------------------------------------------------------------
istream& operator >> ( istream& ins, data_t& data )
{
data.clear();
record_t record;
while (ins >> record)
{
data.push_back( record );
}
return ins;
}
//-----------------------------------------------------------------------------
int main()
{
data_t data;
ifstream infile( "Import File.txt" );
infile >> data;
if (!infile.eof())
{
cout << "Unsuccessful Import!\n";
return 1;
}
infile.close();
cout << "Your file contains " << data.size()-1 << " records.\n";
return 0;
}
I've checked that the necessary header files exist on Linux and that appears to be the case.
If I comment out the EOF check the console returns the message that
Process returned 49 (0x31)
A snippet of the import file which fails under Linux is:
1138,1139,1137.25,1138.5
1138.25,1138.75,1138.25,1138.5
1138.75,1139,1138.5,1138.75
1138.75,1138.75,1138.25,1138.25
1138.25,1138.25,1137.5,1137.5
1137.5,1138.75,1137.5,1138.5
1138.75,1143.75,1138.75,1143
1143.25,1145.75,1143.25,1144.5
1144.5,1144.75,1143,1143.25
1143.5,1144.5,1143.25,1144.25
Grateful for any help in finding a solution.
That return 4321; in main reports an unsuccessful return code to the OS. Only 0 return code (aka EXIT_SUCCESS) is considered successful.
Change it to return 0 or completely remove that return statement (in C++ main has implicit return 0).

How to link properly Dcmtk with Qt for Linux?

My goal is open Dicom files and convert thes into cv::Mat to process them with Opencv.
I have compiled dcmtk 3.6.3 on ubuntu 18.4.1 and tried to link it with Qt 5.11.1 with Qt Creator 4.6.2 but failed to do so.
# pro file
QT += core
QT -= gui
TARGET = DcmtkTesting
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
DCMTK_PREFIX = "/home/ismail/dcmtk363"
DCMTK_LIBS_PREFIX=$$DCMTK_PREFIX"/lib"
DCMTK_INCLUDE=$$DCMTK_PREFIX"/include"
INCLUDEPATH+=$$DCMTK_INCLUDE
LIBS += -L$$DCMTK_LIBS_PREFIX
SOURCES += main.cpp
and for the main:
#include
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include <dcmtk/dcmimgle/dcmimage.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DicomImage *image = new DicomImage("test.dcm");
if (image != NULL)
{
if (image->getStatus() == EIS_Normal)
{
if (image->isMonochrome())
{
image->setMinMaxWindow();
Uint8 *pixelData = (Uint8 *)(image->getOutputData(8 /* bits*/));
if (pixelData != NULL)
{
/* do something useful with the pixel data */
}
}
} else
cout << "Error: cannot load DICOM image (" << DicomImage::getString(image->getStatus()) << ")" << endl;
}
delete image;
return a.exec();
}
and I got this errors:
The error indicates that the linker could not find the symbols (methods) provided by the library. In your .pro file, you pointed the linker to a directory where your library is located, but you forgot to specify which library should be linked.
So you have to modify the line LIBS +=... accordingly, e.g.:
LIBS += -L$$DCMTK_LIBS_PREFIX -ldcmtk
Since I don't know the actual name of the library, I use dcmtk in my example. You may have to adopt it to fit your build environment. Just make sure that you have the -l (lower case L), immediately followed by the library name.

I can't figure out how to link msi.lib to my Visual Studio C++ project

I'm trying to write a simple application that will enumerate all the ProductCodes installed on my machine.
I've started a new project in Visual Studio 2013, but whenever I build I get the error:
"LNK2019: unresolved external symbol _MsiEnumProductsExA#32 referenced in function _main"
I've been trying to figure out how to add msi.lib to my project include path, but I can't seem to figure it out.
Here's my code:
#define _WIN32_MSI 300
#include <Windows.h>
#include <iostream>
#include <string>
#include <Msi.h>
using namespace std;
int main() {
// Get a list of all installed MSIs
DWORD index = 0;
TCHAR currentProductCode[40] = {0};
unsigned int result = ERROR_SUCCESS;
// Open an MSI handle
while (ERROR_SUCCESS == result) {
result = MsiEnumProductsEx(NULL, "s-1-1-0",
MSIINSTALLCONTEXT_USERMANAGED | MSIINSTALLCONTEXT_USERUNMANAGED | MSIINSTALLCONTEXT_MACHINE,
index, currentProductCode, NULL, NULL, NULL);
if (result == ERROR_SUCCESS) {
cout << "current ProductCode: " << currentProductCode;
}
index++;
}
return 0;
}
I've been trying to update the project's Property Pages by adding the path to the msi.lib to the "Library Directories" property, but that doesn't seem to work:
This is like Visual Studio 101, what am I missing?!
Goto Configuration Properties>Linker>Input
Add msi.lib in Additional Dependencies Thats it! Make sure you are using same calling conversion, which used to built the lib. i.e either stdcall or cdecl.

Trouble with garbage chars in visual c++ file reading

I am trying to read a text file using the following code:
void function readfile(char *inputfile) {
istream is;
int filesize = 0;
is.open(inputfile);
if (!is.is_open()) {
return;
}
is.seekg(0, ios::end);
filesize = (int)is.tellg();
is.seekg(0, ios::beg);
char *buf = new char[filesize];
is.read(buf, filesize);
is.close();
cout << buf << endl;
delete[] buf;
return;
}
While in g++ (mac / macports) it works correctly (getting all contents into a dynamic allocated char* array), in Visual Studio C++ 2010, I get constant errors of this type: Debug assertion failed: (unsigned)(c+1) <= 256, file isctype.c.
The problem is that it opens the file but can't find a termination delimeter so when it reaches the eof it starts reading somewhere else (garbage characters). Using the cout << buf; I can see that the file is being read correctly in mac but in visual c++ it types more garbage chars. What is the problem here?
Make your buffer one larger and add the terminating nul yourself.
Let C++ standard library do the work for you:
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::string buf(std::istreambuf_iterator<char>(is), {});
std::cout << buf << std::endl;
}
See, it's now also
exception safe
handles embedded NUL characters correctly
Note, of course you can use vector instead of string if you prefer (just change that one word)
Full demo: see it live on Coliru
#include <fstream>
#include <iostream>
#include <iterator>
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::string buf(std::istreambuf_iterator<char>(is), {});
std::cout << buf << std::endl;
}
int main()
{
readfile("main.cpp");
}
Update For C++11 challenged compilers (and showing how to use a vector):
Also Live on Coliru
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
void readfile(const char *inputfile) {
std::ifstream is(inputfile);
std::istreambuf_iterator<char> f(is), l;
std::vector<char> buf(f, l);
std::cout.write(buf.data(), buf.size());
}
int main()
{
readfile("main.cpp");
}

how to transfer contents of char[] to a String^ in C++/CLI

I've commandLine arguments in a C++/CLI program denoted by char* argv[].
I want to transfer all the contents getting concatenated to a String^ class.
Code:
String ^masterString = "Commands=>";
for(int i=0; argv[i] != nullptr; ++i)
masterString += String(argv[i]);
However, I find the above not working in the last statement where I use += operator.
What's the wrong usage here? Error here is No operators match the operands.
Any other better ways to store contents into String^ from char*?
Look into MSDN, mostly into this:
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
int main() {
const char* message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
btw: I didn't check this. Just googled it. But, I think, this would work, cause it posted in MSDN.

Resources