Creating Visual Studio Error List Messages - c++

Is there any way to create a custom message in the Visual Studio error list? I'm looking at VS 2010 and 2012.
For generating errors, it's a simple #error text.
For generating warnings it's #pragma warning ("text").
For printing a message to the Output window, it's #pragma message("text")
There's little or no documentation on how to create a information message in the Error List. Anyone know if it is possible?
I'd prefer if no VS addons would be used, but if there is a way with those, that would be fine too.

Possibly this may help: Formatting the Output of a Custom Build Step or Build Event
or https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks?view=vs-2022
Formatting the Output of a Custom Build Step or Build Event
If the output of a custom build step or build event is formatted
correctly, users get the following benefits:
Warnings and errors are counted in the Output window.
Output appears in the Task List window.
Clicking on the output in the Output window displays the appropriate topic.
F1 operations are enabled in the Task List window or Output window.
The format of the output should be:
{filename (line# [, column#]) | toolname} :
[any text] {error | warning} code####: localizable string

And this macro is a possible implementation of what Rost linked to :
#define COMBINE_NAMES_HELPER_6(a,b,c,d,e,f) a##b##c##d##e##f
#define COMBINE_NAMES_6(a,b,c,d,e,f) COMBINE_NAMES_HELPER_6(a,b,c,d,e,f)
#define FILE_LINE_FUNC_STR(msg) ( COMBINE_NAMES_6(__FILE__, "(",STRING( __LINE__), "): ", __FUNCTION__, " "msg) )
#define COMPILER_MSG(msg) message FILE_LINE_FUNC_STR(msg)

Related

Add line to comment in Visual Studio C++

I'm looking for something rather specific.
Let's say I define something like a variable, and add a comment to it.
#define ARDUINO_READ_Q0 //Read pins 22-29
Now, any time I start typing ARDUINO_READ_Q0, it pops up in Visual Studio's list of available values, and when you hover over it, it comes up with a tooltip saying "Read pins 22-29"
Is there any way to add a line to this tooltip? I thought of something like this:
#define ARDUINO_READ_Q0 /* Read pins 22-29
Return 1B */
However, I would prefer for it to remain on a single line when I define it.
To give you an idea of what I'm trying to do, imagine something looking like this:
#define ARDUINO_READ_Q0 // Read pins 22-29 /n Return 1B
This way I can say "next line" without making the mess of having two lines per #define.
It's sort of a formatting preference. I'm about to have a lot of #defines.
If you are using Visual Studio 2013 there are special XML tags that you can use to do this.
/// <summary>Read pins 22-29</summary>
#define ARDUINO_READ_Q0
This will display "Read pins 22-29" in the Intellisense tooltip when you hover the ARDUINO_READ_Q0.
You can find more documentation details on MSDN:
http://msdn.microsoft.com/en-us/library/ms177227.aspx
http://msdn.microsoft.com/en-us/library/ms177226.aspx
Note: This may work on older version of Visual Studio but I only have 2013 have on my laptop at the moment.

Resource.rc Windres Syntax Error (Mingw)

I got a project from MS VS, and at the moment I'm migrating it to compile using gcc for Windows.
The C code is completely ported, but I'm having a problem using windres to compile the projet resources.
I'm having a syntax error, reported by windres, at those single lines:
CONTROL "Tab1",IDC_FILETAB,"SysTabControl32",TCS_BOTTOM,0,1,336,194
CONTROL "Tab1",IDC_KEYS,"SysTabControl32",TCS_BOTTOM,27,111,73,6
All others use of Control, with similar syntax, works as expected...
According with http://cygwin.com/cygwin-ug-net/windres.html , the follow syntax is used for CONTROL:
CONTROL ["name",] id, class, style, x,y,w,h [,exstyle] [data]
CONTROL ["name",] id, class, style, x,y,w,h, exstyle, helpid [data]
At top of resource.rc I'm including afxres.h,winuser.h and windows.h .
Can any one give me a help? I don't have a clue about what to do....
BTW, if I comment those lines, all ends with no errors, but the executable cannot works properly.
Thanks
Edit: After more search on the internet.. I found that windres already had many problems with syntax accepted on windows resource compiler, mainly because some classes aren't visible for windres. So if any one know an alternative classes/id, or where it are defined to include, I can workaround it.
You might have copied the code happens all the time just open the code in a textviewer and change the format to plain text or edit and replace characters like " , ' etc.

Printing output on the Output Window in Visual C++ IDE

How do I print on the output window in Visual C++? The project that I am working on isn't of a console window project type. That's when I build and run it, it doesn't open a console window. Instead, it opens a win32 application, which isn't built by me. I am just adding things to it.
I am pretty new to C++ and because I couldn't print variables out on any console, it makes it very hard for me to debug.
Since the Visual Studio 2010 project doesn't launch console when I build and run it, can I still print outputs such as variables and others on the Output window of the IDE?
Thanks for any help.
You can use OutputDebugString("..."); to print to the Output window of Visual Studio. You have to #include <windows.h> though.
I have written a portable TRACE macro.
On MS-Windows, it is based on OutputDebugString as indicated by other answers.
Here I share my work:
#ifdef ENABLE_TRACE
# ifdef _MSC_VER
# include <windows.h>
# include <sstream>
# define TRACE(x) \
do { std::stringstream s; s << (x); \
OutputDebugString(s.str().c_str()); \
} while(0)
# else
# include <iostream>
# define TRACE(x) std::clog << (x)
# endif // or std::cerr << (x) << std::flush
#else
# define TRACE(x)
#endif
example:
#define ENABLE_TRACE //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"
int main (void)
{
int v1 = 123;
double v2 = 456.789;
TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');
}
Please feel free to give any improvements/suggestions/contributions ;-)
Instead of printing to the Output window in VS as indicated by other answers, I prefer to create a console window in my GUI apps, then use regular printf or cout to write debugging info to it. This has the benefit that you can do it even when you run without the debugger.
See this site for a simple function that sets up a console.
I have used this in the past, although not with a win32 application. You could give it a shot though :)
http://www.cplusplus.com/forum/lounge/17371/
You can use the Windows function OutputDebugString (see here) to send output to debuggers. These outputs are shown in the VS output window. You can also watch these outputs with external applications, e.g. DebugView.
Remember to remove these statements from your production code if you don't want other people to see these debug messages (which would otherwise be possible using tools like DebugView...)

Task Scheduler Error Message: 80041318, Whats it mean?

I have search & searched MSDN to find the definition of the HRESULT error with the value 80041318 but I cannot find it.
I am working in C++ Win32 attempting to schedule a task that will execute when the current user logs in. The function RegisterTaskDefinition() fails & returns this value.
Does anyone know what this error means & if possible does anyone know of the MSDN link to ALL the HRESULT ERRORS? I can find one link that has definitions of 7 errors.
The Microsoft Exchange Server Error Code Look-up tool can be used to look up error codes. Don't let the name fool you—it's not just for Exchange, it's useful for program that generates Win32 error codes or HRESULTs. It searches all of the system header files for potential matches.
To look up error code 80041318, just run it from the command line like so:
C:\> err 80041318
In this case, a quick search yields this MSDN page, which says that error 80041318 is SCHED_E_INVALIDVALUE: "The task XML contains a value which is incorrectly formatted or out of range."
Using the error lookup tool that comes with VC++ (errlook.exe, or "Error Lookup" on the Tools menu in the IDE), the error message for 0x80041318 is "The task XML contains a value which is incorrectly formatted or out of range."
In the case of Task Scheduler 1.0, the error codes used are in WinError.h (search for SCHED_E_). I'm not sure if all the error used by TS 2.0 are there or not, though.
It means that you are passing incorrect argument to pLogonTrigger

Is there a TRACE statement for basic win32 C++?

In MFC C++ (Visual Studio 6) I am used to using the TRACE macro for debugging. Is there an equivalent statement for plain win32?
_RPTn works great, though not quite as convenient. Here is some code that recreates the MFC TRACE statement as a function allowing variable number of arguments. Also adds TraceEx macro which prepends source file and line number so you can click back to the location of the statement.
Update: The original code on CodeGuru wouldn't compile for me in Release mode so I changed the way that TRACE statements are removed for Release mode. Here is my full source that I put into Trace.h. Thanks to Thomas Rizos for the original:
// TRACE macro for win32
#ifndef __TRACE_H__850CE873
#define __TRACE_H__850CE873
#include <crtdbg.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef _DEBUG
#define TRACEMAXSTRING 1024
char szBuffer[TRACEMAXSTRING];
inline void TRACE(const char* format,...)
{
va_list args;
va_start(args,format);
int nBuf;
nBuf = _vsnprintf(szBuffer,
TRACEMAXSTRING,
format,
args);
va_end(args);
_RPT0(_CRT_WARN,szBuffer);
}
#define TRACEF _snprintf(szBuffer,TRACEMAXSTRING,"%s(%d): ", \
&strrchr(__FILE__,'\\')[1],__LINE__); \
_RPT0(_CRT_WARN,szBuffer); \
TRACE
#else
// Remove for release mode
#define TRACE ((void)0)
#define TRACEF ((void)0)
#endif
#endif // __TRACE_H__850CE873
From the msdn docs, Macros for Reporting:
You can use the _RPTn, and _RPTFn macros, defined in CRTDBG.H, to replace the use of printf statements for debugging. These macros automatically disappear in your release build when _DEBUG is not defined, so there is no need to enclose them in #ifdefs.
There is also OutputDebugString. However that will not be removed when compiling release.
Trace macros that provide messages with source code link, run-time callstack information, and function prototype information with parameter values:
Extended Trace: Trace macros for Win32
I just use something like this (from memory, not tested at all...)
#define TRACE(msg) {\
std::ostringstream ss; \
ss << msg << "\n"; \
OutputDebugString(msg.str()); \
}
And then I can write things like :-
TRACE("MyClass::MyFunction returned " << value << " with data=" << some.data);
You can wrap that in some #ifdefs to remove it in release builds easily enough.
I found that using the _RPT() macro will also work with a C source file in Visual Studio 2005. This article Debugging with Visual Studio 2005/2008: Logging and Tracing provides an overview of TRACE, _RPT, and other logging type macros.
I generate a line for a log file called the ASSRTLOG which contains logs and when writing the log to the file, I also do the following source code line:
_RPT1(_CRT_WARN, "ASSRTLOG: %s", szLog1);
This line puts the same log that is going into the log file into the output window of the Visual Studio 2005 IDE.
You might be interested in the mechanics behind the approach we are using for logging. We have a function PifLogAbort() which accepts a series of arguments that are then used to generate a log. These arguments include the name of the file where the log is being generated along with the line number. The macro looks like this:
#define NHPOS_ASSERT_TEXT(x, txt) if (!(x)) { PifLogAbort( (UCHAR *) #x , (UCHAR *) __FILE__ , (UCHAR *) txt , __LINE__ );}
and the function prototype for PifLogAbort() look like this:
PifLogNoAbort(UCHAR *lpCondition, UCHAR *lpFilename, UCHAR *lpFunctionname, ULONG ulLineNo)
and to use the macro we will insert a line like this:
NHPOS_ASSERT_TEXT(sBRetCode >= 0, "CliEtkTimeIn(): EtkTimeIn() returned error");
What this macro will do is that if the return code is less than 0 (the assertion fails), a log will be generated with the provided text. The log includes the condition that generated the log along with file name and line number.
The function PifLogAbort() generates logs with a specified length and treats the output file as a circular buffer. The logs have a time and date stamp as well.
In those cases where we want to generate the descriptive text dynamically at run time, perhaps to provide the actual error code value, we use the sprintf() function with a buffer as in the following code sequence:
if (sErrorSave != STUB_BM_DOWN) {
char xBuff[128];
sprintf(xBuff, "CstSendBMasterFH: CstComReadStatus() - 0x%x, sError = %d", usCstComReadStatus, CliMsg.sError);
NHPOS_ASSERT_TEXT((sErrorSave == STUB_BM_DOWN), xBuff);
}
If we want the logs to not be generated, all we need to do is to go to the single header file where the macro is defined and define it to be nothing then recompile. However we have found that these logs can be very valuable when investigating field issues and are especially useful during integration testing.
Windows Events are a potential replacement for TRACE macros, depending on your particular scenario. The code gets compiled into both Debug and Release configurations. Event tracing can then be dynamically enabled and disabled, displayed in real-time, or dumped on a client's machine for later diagnosis. The traces can be correlated with trace information gathered from other parts of the OS as well.
If you merely need to dump information whenever code reaches certain checkpoints, together with variable content, stack traces, or caller names, Visual Studio's Tracepoints are a non-intrusive option to do so.