const char* to LPCWSTR in Qt - c++

I have faced a problem in Qt with error: cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
when calling: OutputDebugString( "wtf!" );
In simple C++ project I've always been setting "Character Set" to "Not Set", but this time it doesn't work, the error keeps displaying all the time. I tried other possibilities like "Use multi-byte" but still no effect. What's going on?
Thanks.

I found out that when I use qDebug instead of OutputDebugString it works.

Related

getlength throwing error while using with wchar_t

I am working on a project that is written is VScode and is working properly. But when I am trying to execute the same on gn(generate ninja), this the error I am getting:-
error: no member named 'GetLength' in 'std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>'
int sz = string.GetLength();
I am not sure what is wrong here. Any little hint will be highly appreciated.

C++ multiline CString literal in VS2013 Unicode

A multiline CString literal 'str1', accepted without a wink in VS2012 (with MBCS) is now refused at build time, after upgrading to VS2013 (with Unicode, to alleviate tons of errors from the newly deprecated MBCS, even after installing its addon), with the output message:
error C2308: concatenating mismatched strings
as in the following example (A):
str1 = _T(" HELP - available commands \n\n\n"
"F1 : the present help message \n\n");
The first line is reported 'wide' and the second 'narrow'.
I have then tried (B) to add mono-line CString literals:
str1 = _T(" HELP - available commands \n\n\n")
+ _T("F1 : the present help message \n\n");
but the IDE already complains with
Error: expression must have integral or unscoped enum type
and the builder with
error C2110: '+' : cannot add two pointers
It does indeed work if I build (C) the CString str1 with mono-line literals one by one:
str1 = _T(" HELP - available commands \n\n\n");
str1 += _T("F1 : the present help message \n\n");
but I would like to understand why (A) and (B) do not work here, as expected, and as they were so until now. There are several such problems in this (large) program, but in most other similar instances it does work just fine.
Is it due to changes in VS2013 or (and?) the switch from MBCS to Unicode? Are there special characters I overlooked in these strings? And then, how to fix these problems?
Thanks in advance for your responses.
It should be:
str1 = _T(" HELP - available commands \n\n\n") // no semicolon here
_T("F1 : the present help message \n\n");
The reason it worked before is that _T is a no-op for MBCS but for Unicode expands to a width prefix. In C/C++ concatenation is just by making them adjacent with whitespace, but they need the same prefix, at least for MSVC.

C2664 error c++ Visual Studio

I am trying to modify an old MFC program. After opening the project in Visual Studio 2013 there are many errors of the type below.
In AviPlay.cpp
#include "stdafx.h"
#include "AviPlay.h"
#define OPEN_AVI_VIDEO "open avivideo"
BOOL initAVI()
{
return mciSendString(OPEN_AVI_VIDEO, NULL, 0, NULL) == 0;
}
The error thrown is error C2664: 'MCIERROR mciSendStringW(LPCWSTR,LPWSTR,UINT,HWND)' : cannot convert argument 1 from 'const char [14]' to 'LPCWSTR'
Should setting the compiler option for Strict to off, or some other compiler option, resolve this error? If not, I can modify the many lines of code manually. In that case, what might have changed in the last 15 years that would make code like this OK before but not OK now?
Thank you in advance.
LPCWSTR tells you it is expecting a wchar_t string, not a char string. By default, all Windows APIs now accept wchar_t strings (unicode). You can change it back to char strings in the project properties, General page, Character Set. Setting it to 'Use Multibyte char set' will get it working as it used to.
Also if you are creating a new project and facing this issue , then make the conformance mode to Default or No .

C Programming in OPNET

I'm starting some work into manual programming of nodes in OPNET however I am having a few troubles. I'm getting some information from packets and storing them in variables and want to output this to the simulation console. When I add the line puts(bcast_info) I get the following error.
C:/Users/Andrew/op_models/traffic_source.pr.c(74) : warning C4047:
'function' : 'const char *' differs in levels of indirection from
'Objid' C:/Users/Andrew/op_models/traffic_source.pr.c(74) : warning
C4024: 'puts' : different types for formal and actual parameter 1
Prior to adding the line mentioned above, the simulation worked perfectly and I got the basic text output. This is my code so far.
static void route_pk(void)
{
Packet * pkptr;
Objid bcast_info;
FIN(route_pk());
pkptr = op_pk_get(op_intrpt_strm ());
bcast_info = op_pk_bcast_get (pkptr);
printf ("Hello! \n");
puts("Hello from puts");
puts(bcast_info);
op_pk_send (pkptr, 1);
FOUT;
}
I appreciate that OPNET is a variation on the C language with some of its own methods etc but any help on what the errors actually mean and potential fixes would be much appreciated. Please be aware that I have never worked with C / C++ or this OPNET language before.
Please don't use the print() function. Use the op_prg_odb_print_major() function.
Objid is a special data type in Modeler. You can't print it to screen.

Qt - getting "warning: format not a string literal and no format arguments"

Keep getting warnings on lines like these
qDebug("An error occured while trying to create folder " + workdir.toAscii());
workdir being QString()
warning: format not a string literal and no format arguments
That should probably be:
qDebug("An error occured while trying to create folder %s", workdir.constData());
since qDebug takes const char* as first argument.
When debbuging with qDebug, I find the following syntax much easier :
qDebug() << "An error occured while trying to create folder" << workdir;
To do this, you'll need to include the <QtDebug> header.
More info : Qt docs regarding qDebug().
I managed to get it to work fine without warning like this :
qDebug("An error occurred while trying to create folder %s", qUtf8Printable(workdir));