VC++ Colors in console - visual-c++

I'am not a VC++ Dev but I'am searching to EDIT a source code. I want simply add new line on my Console with Different color (Green, Red ...)
void main()
{
static char * DES_KEY = "!_a^Rc*|#][Ych$~'(M _!d4aUo^%${T!~}h*&X%";
XStrDESUtil desUtil(DES_KEY);
printf("Password: %s\n", desUtil.Decrypt("1a6a2dfd3e44b8a0b02a2b66c801821e").c_str());
system("PAUSE");
}
I've searched but all what I found is
printf ("\033[34;01mBonjour\033[00m\n");
and those characters ... but it doesn't work for some reasons :(

Check out this header-only library that works for both, ANSI and Windows consoles:
https://github.com/tapio/rlutil
rlutil::setColor(rlutil::GREEN);
rlutil::setColor(rlutil::RED);
should suffice

In Windows, you can use the Windows Console Functions for this, such as SetConsoleTextAttribute.
I have created a small free C++ template library (one header only) to wrap many of that functionality. Available at http://cppconlib.codeplex.com/.

Related

How to print in a Windows Universal C++ project

I feel so dumb asking this question but honestly I can't understand why System namespace can't be used! What am I doing wrong? Is there any other way to print a single line in the output?
(I am using Visual Studio 2015)
I can't understand why System namespace can't be used
Windows Universal app is totally different with traditional desktop app, please check Windows Runtime APIs and Win32 and COM API which lists all Win32 and COM APIs supported for use in UWP apps.
Is there any other way to print a single line in the output? (I am using Visual Studio 2015)
If you need to print message to Output window, use OutputDebugString function in UWP C++/CX project, adding #include to access it, for example:
void CPPUWPApp1::MainPage::LogMessage(Object^ parameter)
{
auto paraString = parameter->ToString();
auto formattedText = std::wstring(paraString->Data()).append(L"\r\n");
OutputDebugString(formattedText.c_str());
}
Usage:
LogMessage("Hello World!");
You could do this directly:
OutputDebugString(L"Hello World");
Notice the L in front of the string, to convert it directly to LPCWSTR.

getting stared with HDF5 on Visual Studio 2013 with Visual C++ 2013

I am having a hard time getting an HDF5 example working with Visual Studio 2013 (C++).
The example is at: http://www.hdfgroup.org/ftp/HDF5/examples/misc-examples/stratt.cpp and I've posted the code below for completeness.
My first question is: Will the latest HDF5 (version 1.8.13) work with Visual C++ 2013? The docs only mention 2012 that I can see, but generally I've had no problems using 2013 where 2012 is mentioned.
I tried the example program as both a 32 bit and a 64 bit app. Ultimately, I'm interested in 64 bit. In the project settings for 32 bit, under VC++ settings, I added to the include directories: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include
To the library directories, I added: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib
To the Linker->Input, I added: hdf5.lib;hdf5_cpp.lib
When I ran, I (not unexpectedly) got the message, "The program can't start because hdf5.dll is missing from your computer..."
So to the debug directory, I added, hdf5.dll and hdf5_cpp.dll from the directory:
C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\bin
I then get the runtime error:
The application was unable to start correctly (0xc000007b). Click OK to close the application. Any ideas?
Incidentally, when I tried the x64 bit version (using the 64 bit setttings, directories and files), I got slightly different errors. The program runs to the end, but no attribute is written to the console, no file is produced, and I get the dreaded error at the end (after hitting f10 on the last line):
Unhandled exception at 0x000007FEF05E512D (msvcp120d.dll) in HDF5AttributeExample2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
In both cases (32 and 64 bit) my gut tells me that I have some sort of configuration problem (wrong lib, wrong setup, etc.) I'd really appreciate any help or suggestions folks can offer.
If there is another Visual C++ HDF5 example, by all means please tell me!
Thanks,
Dave
#include <string>
#include <iostream>
#include "H5Cpp.h"
using std::cout;
using std::endl;
using namespace H5;
const H5std_string FILE_NAME("string_test.h5");
const H5std_string DS_NAME("Data Set 1");
const H5std_string ATTR_NAME("String Attribute");
int main(void) {
// Create the named file
H5File file = H5File(FILE_NAME, H5F_ACC_TRUNC);
// Create new dataspace for the dataset
const int rank = 3;
const int dim1 = 2;
const int dim2 = 2;
const int dim3 = 2;
hsize_t dims[rank] = { dim1, dim2, dim3 };
DataSpace dataspace = DataSpace(rank, dims);
// Create new datatype for the dataset
FloatType datatype(PredType::NATIVE_FLOAT);
// Create the dataset
DataSet dataset = file.createDataSet(DS_NAME, datatype, dataspace);
// Set up write buffer 'matrix'
int q, r, s;
float value;
float matrix[dim1][dim2][dim3];
for (q = 0; q < dim1; q++)
for (r = 0; r < dim2; r++)
for (s = 0; s < dim3; s++)
{
value = 1.111 + (q * r * s);
matrix[q][r][s] = value;
}
// Write data to the dataset
dataset.write(matrix, datatype);
// Create new dataspace for attribute
DataSpace attr_dataspace = DataSpace(H5S_SCALAR);
// Create new string datatype for attribute
StrType strdatatype(PredType::C_S1, 256); // of length 256 characters
// Set up write buffer for attribute
const H5std_string strwritebuf("This attribute is of type StrType");
// Create attribute and write to it
Attribute myatt_in = dataset.createAttribute(ATTR_NAME, strdatatype, attr_dataspace);
myatt_in.write(strdatatype, strwritebuf);
// Set up read buffer for attribute
H5std_string strreadbuf("");
// Open attribute and read its contents
Attribute myatt_out = dataset.openAttribute(ATTR_NAME);
myatt_out.read(strdatatype, strreadbuf);
// Display attribute contents
cout << "Attribute contents: " << strreadbuf << endl;
return 0;
}
The runtime error that you are getting arises from the fact that you are using libraries that were compiled with VS2012.
From the HDF Group website on VisualStudio and CMake:
First, make sure you use the same version of Visual Studio that was used to create the pre-built binaries. This is required to avoid runtime errors.
I suggest that you try by yourself to build the source code on VS2013 using CMake.
Ok, after having tried myself to compile it for the VS2013 Ultimate I managed to get it working with both 32 bit and 64 bit support.
Download the source code and unzip it. Let's assume the directory name is "source".
Download CMake and open the GUI, choose the source location and choose a "build" location.
Click Configure and select the version from visual studio you are using.
The configuration options will appear in red on CMake. Choose whatever you need.
Click on generate.
Go to the "build" folder and open the generated MSVS solution. Build the solution.
In the same build folder run the command
"cpack -C {Debug | Release} ---config C:\...\"build"\CPackConfig.cmake. This will create an installer for the files.
Run this installer.
Create an empty project in MSVS. Go to Project Configuration Properties. Then select C/C++ -> General -> Additional Include Directories and insert C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include. Change this path accordingly. If you are using a x64 version, you will need to change the platform on top of this window to x64. If it doesn't exist, click on Configuration Manager and from the Active Solution platform drop down menu choose New. Add the x64 option.
Now go to Linker - > General -> Additional Library Directories: Add C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib. Change this also accordingly. Note that if you built the binaries for x64 they will be installed at C:\Program Files\HDF_Group\HDF5\1.8.13\.
Got to Linker -> Input -> Additional Dependencies. Add libhdf5_D.lib and libhdf5_cpp_D.lib if you built it as debug, otherwise (I believe) use hdf5.lib and hdf5_cpp.lib.
Good Luck!
Doesn't look like too many people are interested in HDF5 with Visual C++ judging by the number of views. Perhaps that should be a warning to me! In any case, for posterity I offer the following answers to my original question.
First of all, one needs to be very careful that the path variable is correct in environments settings AND that it is set up correctly for 32 bit or 64 bit depending on which you want.
Instead of:
C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\bin
I have:
C:\Program Files (x86)\HDF Group\HDF5\1.8.13\bin (notice missing underscore)
Additionally, one has to pay attention as to where you want 32 bit (use Program File (x86)) or 64 bit (use Program Files). If anyone knows of an easier way to run 32 bit and 64 bit programs on the same machine that use HDf5, please let me know.
The other major problem was with the example code itself. In it's original form, it did not work. After much trial and error, I tracked the problem down to the use of H5std_string. When I replaced these with char [], everything worked. As an example, instead of using:
const H5std_string FILE_NAME("string_test.h5");
I used:
char fileName[128] = "string_test.h5";
and used H5File constructor as so:
H5File file = H5File(fileName, H5F_ACC_TRUNC);
It's annoying that I didn't get a compile time error. The constructor claims to take std::string (which is what H5std_string is) in the constructor for H5File. Quite possibly, there is some Visual Studio setting somewhere that makes things the original code work. I'm not knowledgeable enough about Visual C++ to know.
Anyhow good luck all.
Dave

"unknown type name 'String'" when creating custom Arduino Libraries

I have a few logging functions that I commonly use for different Arduino programs. Since I use them so much, I decided to try to make a custom library for them. Unfortunately, the compiler crashes at the header file with the error:
unknown type name 'String'
I'm a bit confused as to why this is happening because I am including the standard Arduino libraries (which I believe should contain the String class) at the top of my header. Here's the whole thing:
#ifndef logging_h
#define logging_h
#include "Arduino.h"
void logEvent(String msg);
void debugOut(String msg);
void errOut(String err);
void document(String parameter, float value);
#endif
I reinstalled the Arduino IDE (1.0.5) so I think I should have the most recent standard library. If anyone has some suggestions I would really appreciate it.
(This answer is based on our discussion in comments.)
The problem was that the source file for your library was named *.c. That caused the compiler to treat it as C code instead of C++, which means it couldn't handle classes/objects (such as String).
Naming the file *.cpp instead lets the compiler treat it correctly as C++ code.
I had same issue yesterday. The code you included in your question should be your .h file, isn't? My question is: is your library written in C or in C++?
I assume you use C code.
You can't import code from in a user C library with the Arduino IDE. The reason is that use C++ code, and it can't be called from your C library.
Solution: rewrite your library in C+, it's not too difficult.
You can find a lot of help on google on how to write library in C++. You can also check my example at https://github.com/romain-viollette/AverageFilter/
best regards,

Setting VIPS library for visual studio 2012

I am trying libvips for visual studio 2012, starting with a simple example at
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/vipsmanual/vipsmanualse1.html#x6-60001.1.1
#include <iostream>
#include <vips/vips>
int
main (int argc, char ⋆⋆argv)
{
if (argc != 3)
{
std::cerr << "usage: " << argv[0] << " infile outfile\n";
return (1);
}
try
{
vips::VImage fred (argv[1]);
fred.invert ().write (argv[2]);
}
catch (vips::VError e)
{
e.perror (argv[0]);
}
return (0);
}
What I did was:
Download and extract libvips at http://www.vips.ecs.soton.ac.uk/supported/7.34/win32/
Add to VC++ Directories->Include directories as vips-dev-7.34.1\include (vips-dev-7.34.1 is the extracted folder)
Add to VC++ Directories->Library directories as vips-dev-7.34.1\lib
Add a system path entry as vips-dev-7.34.1\bin
Basically because there are not much guide on using libvips with visual studio, so I applied the procedure that I used for OpenCV. The guide only say "All you need to do is include . This will get all of the include you need". Aparrently there are much more than that.
Upon building, the first error is "Unable to find header file "glib-object.h". Essentially, vips/vips call glib-objects "include which lies inside a subfolder of include \include\glib-2.0\glib-objects.h. I searched for a way to make VS search for all subfolders within the main include folder, it seems that such "recursive search" is not possible in VS. One has to point exactly to the folder containing header file and I may need to add all of the subfolders manually. So I tried adding vips-dev-7.34.1\include\glib-2.0 to VC++ Directories->Include directories. But then glib-objects.h calls for another glibconfig.h which is nowhere to be found within the include folder and subfolders.
Have someone sucessfully make libvips work with VS? Can you give me some advices if I miss something.
I'm the libvips maintainer. Sorry, it's very difficult to use the pre-built libvips binaries with VS, for various reasons (see below). I think your options are to use mingw instead, to cross-compile from linux (this is what I do), or to rebuild libvips yourself from source using VS (perhaps a week's work for an experienced dev?). There are some notes on the vips website about this issue.
The libvips.dll on the website has been cross-compiled from linux using mingw. It's set up for a linux-style build system with pkg-config, so you will have a lot of compiler flags to figure out in VS, and it's built against msvcrt.dll, the Windows C runtime, rather than msvcrtXX.dll, the VS runtime, so you will have endless annoying compatibility problems unless you also build against the Windows runtime.
Unfortunately VS no longer supports building against the Windows runtime. They have an internal tool which does support this mode, but it's not publicly available. I read somewhere you can coax the DDK compiler into doing this, but it's also not supported.
CoApp is an interesting project (partly supported by Microsoft) that is attempting to make building software on Windows less painful, but it's still in beta. You could maybe ask if they have a libvips packaged up for VS, or are considering making one.

How to Generate a text file in WinCE 6.0 application using MFC?

I am trying to create a text file through my code. The file is created but its extension is different(not .txt). Then on searching i came to know that this can be because of MFC runtimes.
I searched for the MFC Runtimes in my C:\program Files\Windows CE tools\SDK but i am not able to find the MFC folder there. What should i do? from where should i include MFC Runtimes?
here's the text file generation code which i am using:
void CFormRight::OnBnClickedButtonTextfile()
{
CFile File;
char cFileAddr[100] = {"My Device\\Label.txt"};
File.Open((LPCTSTR)cFileAddr, CFile::modeCreate | CFile::typeBinary |CFile::modeWrite | CFile::shareDenyNone);
File.Write("Hello World", 15);
File.Close();
}
If you're just looking for the DLLs, try looking on your development machine here:
%PROGRAM_FILES%\Microsoft Visual Studio 9.0\VC\ce\dll
That gets you the actual libraries. Including them in the OS image can be done a variety of ways. Typically you'd add them to your platform or project BIB file.
Even with all of that, though, I don't think it's going to solve your problem of a file extension. If you're creating a file and it shows up, just without an extension, it has nothing to do with MFC being there or not, it has to do with either your code, or the way you're determining there is no extension (is "hide file extensions" turned on in Explorer?). To solve that problem, we'd need to see code.
** Edit **
Windows CE is heavily biased toward Unicode. Nearly all Win32 APIs only have the Unicode variant exposed, therefore your code should also lean toward Unicode, meaning string you pass around that will end up at API calls should be Unicode.
Second, you should not ignore the compiler when it complains. The cast you have in there I bet was due to a compiler complaint, and it's just incorrect. If your original code was this (note the lack of cast on the first parameter):
File.Open(cFileAddr, CFile::modeCreate | CFile::typeBinary |
CFile::modeWrite | CFile::shareDenyNone);
Then you would get a compiler error:
error C2664: 'CFile::Open' : cannot convert parameter 1 from 'char [100]' to 'LPCTSTR'
That's because under Windows CE, Open is looking for a wide (Unicode) string. You have an ANSI string. You cannot convert from ANSI to Unicode through a simple direct cast like you did. Yes, the compiler will quit complaining, but it gives bad behavior. You can cast an int to a char[] too, but that doesn't mean it will work for the API.
So change your code to use wide strings and all will work:
CFile File;
CFileException ex;
wchar_t cFileAddr[100] = TEXT("My Device\\Label.txt");
if(!File.Open(cFileAddr, CFile::modeCreate | CFile::typeBinary |
CFile::modeWrite | CFile::shareDenyNone))
{
wchar_t error[1024];
ex.GetErrorMessage(error, 1024);
cout << "Error opening file: ";
cout << error;
return;
}
File.Write("ID Technologies", 15);
File.Close();
Note the use of wchar_t, the initialization using the TEXT macro, and the lack of the cast in the Open call.
EDIT 2
I added error handling in the code above, but really you should learn to read the documentation and how to debug. This is a pretty basic usage scenario, and knowing how to look for errors is critical.

Resources