I'm trying to print out a variable to a message box from Language C
This is my current code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreveInstance, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
char temp[128];
sprintf(temp, "The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
my attempt was assigning the string which included a variable and then I putting that assigned string into the MessageBox but whenever I compiled this It will give me a warning saying
error C2220: warning treated as error - no 'object' file generated
warning C4133: 'function': incompatible types - from 'char [128]' to 'LPCWSTR'
warning C4100: 'nCmdShow': unreferenced formal parameter
warning C4100: 'lpCmdLine': unreferenced formal parameter
warning C4100: 'hPreveInstance': unreferenced formal parameter
warning C4100: 'hInstance': unreferenced formal parameter
would there be any solution to this?
I am currently using Visual Studio 2017
MessageBox is actually a macro - there are two versions: MessageBoxA which takes chars and MessageBoxW which takes wide chars. Depending on the default character set, it will take either the A or W version. By default, it takes the W version.
If you go into the project properties, under general, near the bottom of the dialog, there is an entry for character set. By default, it is set to unicode (the W version). Just change this to MBCS (Multi byte character set) and your program should build after you've removed the L from the MessageBox title
Alternatively leave it as Unicode and change the code to the following. Note that you don't need winmain if it is not using the GUI. You can use MessageBox in a console application
int main()
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
wchar temp[128];
wsprintf(temp, L"The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
There is a third solution using TCHAR but I'll have to look it up before I post it.
Edit the third solution
If you look in stdafx.h, it has probably already included tchar.h. These are character agnostic definitions. You can use a MessageBox with a C++ Win32 console application.
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
TCHAR temp[128];
_stprintf(temp, _T("The die shows: %d"), dice);
MessageBox(NULL, temp, _T("Dice"), MB_YESNO);
return 0;
}
Related
I should preface with the fact that I'm relatively new to VS, however I am not new to C.
The problem I'm encountering is that nothing shows up on stdout when printed. Neither printf/_s, nor fprintf/_s(stdout, ...) produce any output. Interestingly enough fprinf(file, ...) does in fact produce output to the given file. Is there a chance this has to do with printf deprecation (I have tried the preproc. _CRT_SECURE_NO_DEPRECATE)?
Below is my full program:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
FILE * pFile = fopen("outputTest.txt", "w");
fprintf(pFile, "At top of main1.\n"); //works
printf("At top of main2.\n"); //doesn't work
printf_s("At top of main3.\n"); //doesn't work
fprintf_s(stdout, "At top of main4.\n"); //doesn't work
fflush(stdout);
fclose(pFile);
return FALSE;
}
I'm using Visual Studio 2017, and the program is a Win32 (App?). Also I've ruled out the possibility that Linker->System->Subsystem is the problem.
Any ideas are appreciated.
EDIT: I'm not sure if it matters but the "Solution Platforms" dropdown at the top of VS says Win32, unlike when you create a new "Windows Desktop App." where it says x86.
I am still a rookie with C, and even newer to wide chars in C.
The below code should show
4 points to Smurfs
but it shows
4 points to Smurfs
In gdb I see this:
(gdb) p buffer
$1 = L" 4 points to Smurfs",
But when I copy paste from the console, the spaces are magically gone:
(gdb) p buffer
$1 = L"4 points to Smurfs",
Also, buffer[0] contains this according to gdb:
65279 L' '
Apparently the character in question  is the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). I retyped the code making sure I did not enter this. I don't know where this comes from. I also opened the code in notepad per https://stackoverflow.com/a/9691839/7602 and there is no extra chars there.
I wouldn't care if ncurses would stop showing this as a space.
Code (heavily cut down):
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <wchar.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#define NCURSES_WIDECHAR 1
#include <ncursesw/ncurses.h>
#include "types.h"
#include "defines.h"
#include "externs.h"
WINDOW * term;
/*row column color n arguments */
void rccn(int row, int col, const wchar_t *fmt, ...)
{
wchar_t buffer[80];
int size;
va_list args;
va_start(args, fmt);
size = vswprintf(buffer, 80, fmt, args);
va_end( args );
if(size >= 80){
mvaddwstr(row, col, L"Possible hacker detected!");
}else{
mvaddwstr(row, col, buffer);
}
}
int main(void)
{
int ch;
setlocale(LC_ALL,"");
term = initscr();
rccn(1,1,L"%i points to %ls",4,L"Smurfs");
ch = getch();
return EXIT_SUCCESS;
}
The problem goes 'away' with
rccn(1,1,L"%i points to %ls",4,L"Smurfs"+1);
As if the wide encoding of the constant adds that char in front..
Found it..
I had followed a tutorial where it was advised to add this compiler flag:
-fwide-exec-charset=utf-32
My code was not running on Cygwin at all, and I read that Windows is utf-16 centered, so I removed that compiler flag and it started working on Cygwin.
Then out of curiosity I removed the compiler flag on Raspbian, and it is now working as expected there as well, no more byte order marks.
I have seen a few questions on how to print these characters but none of the methods appear to be working. I suspect it is because I making a Win32 console application based on some of the comments I read.
Here is an example of what I have tried in my code currently. It only prints question mark boxes, or if I change it around I get question marks or random symbols.
I have tried defining these at the top.
#define SPADE '\x06'
#define CLUB '\x05'
#define HEART '\x03'
#define DIAMOND '\x04'
inside function, these are some of the things I've tried. I have left S,D,H,C in case I can't figure it out.
printf("%lc", SPADE);
//printf("♠");
//printf("S");
printf("%lc", HEART);
//printf("♥");
//printf("H");
printf("%lc", DIAMOND);
//printf("♦");
//printf("D");
printf("%lc", CLUB);
//printf("♣");
//printf("C");
UTF-16 wchar_t and wide characters functions are needed in Windows.
#include <windows.h>
int main()
{
DWORD n;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
const wchar_t *buf = L"♠♥♦♣\n";
WriteConsoleW(hout, buf, wcslen(buf), &n, 0);
return 0;
}
The following code will compile with Visual Studio:
#include <stdio.h>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"♠♥♦♣\n");
return 0;
}
After setting the mode to UTF-16, you have to call _setmode(_fileno(stdout), _O_TEXT) if you wish to use printf again.
Scenario :
A C application created in Netbeans IDE with below two files:
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
newmain.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
function_1();
function_1(1);
function_1(1,2);
return (EXIT_SUCCESS);
}
When learning the need of the header file in a C program, I tried the above application (as it is). It got compiled and gave the output as below
Entered Value is = 4200800 & 102
Entered Value is = 1 & 102
Entered Value is = 1 & 2
Question 1 : (I realize, in starting stage, to understand the process of the linker program is tough, hence i ask this question.) Is my assumption correct, that when linking, "the linker will check for the function name and not the arguments" in a condition the header file not used?
Regarding the header file usage, I came across this link and there it said as, we can include the C file itself using the #include. So, I used the below line in the file newmain.c
#include "some_function.c"
As expected it shown the below error
error: too few arguments to function 'function_1()'
error: too few arguments to function 'function_1(1)'
And also, I got the below (unexpected) error:
some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here
Question 2: What error I did when including the 'c' file itself, as it gives the above said (unexpected) error?
You are probably using a pre-C99 dialect of C, which has "implicit function declarations". This means that functions without declarations are taken to have this kind of signature:
int function_1();
i.e. returning an int and accepting any number of arguments of any type. When you pass an argument list that is incompatible with your function definition, you invoke undefined behaviour at runtime.
Concerning the multiple definition errors, think of it. Each translation unit you include some_function.c will have its own definition of the function. It is as if you had written that definition in every other .c file. C doesn't allow multiple definitions in a program/library.
I (the questioner) post this answer for a quick understanding for some C programming starters. This answer inspired from the answers by #juanchopanza & #Sam Protsenko which are in this post.
Question 1 :Implicit function declarations in C
Question 2:
When using the below line
#include "some_function.c"
The result application will change like below after the preprocessor activity
some_function.c
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
newmain.c
#include <stdio.h>
#include <stdlib.h>
/*"#include "some_function.c"" replace begin by preprocessor*/
#include <stdio.h>
int function_1(int a, int b){
printf("Entered Value is = %d & %d\n",a,b);
return 0;
}
/*"#include "some_function.c"" replace end by preprocessor*/
int main(int argc, char** argv) {
//function_2(); //Error //function name not allowed
//function_1(); //Error
//function_1(1); //Error
function_1(1,2);
return (EXIT_SUCCESS);
}
Note : In the above two file, the function function_1 is there in two places
So now the below error is meaningful
some_function.c:8: multiple definition of `function_1'
some_function.c:8: first defined here
I am trying to make a simple Message Box in C in Visual Studio 2012, but I am getting
the following error messages
argument of type const char* is incompatible with parameter of type "LPCWSTR"
err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup
Here is the source code
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,"Hello","Title",0);
return(0);
}
Please Help
Thanks and Regards
To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.
Set Project -> Properties -> General -> Character Set option to Use Multi-Byte Character Set
I found it here https://stackoverflow.com/a/33001454/5646315
To make your code compile in both modes, enclose the strings in _T() and use the TCHAR equivalents
#include <tchar.h>
#include <windows.h>
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPrevinstance, LPTSTR lpszCmdLine, int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return 0;
}
I recently ran in to this issue and did some research and thought I would document some of what I found here.
To start, when calling MessageBox(...), you are really just calling a macro (for backwards compatibility reasons) that is calling either MessageBoxA(...) for ANSI encoding or MessageBoxW(...) for Unicode encoding.
So if you are going to pass in an ANSI string with the default compiler setup in Visual Studio, you can call MessageBoxA(...) instead:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBoxA(0,"Hello","Title",0);
return(0);
}
Full documentation for MessageBox(...) is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
And to expand on what #cup said in their answer, you could use the _T() macro and continue to use MessageBox():
#include<tchar.h>
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return(0);
}
The _T() macro is making the string "character set neutral". You could use this to setup all strings as Unicode by defining the symbol _UNICODE before you build (documentation).
Hope this information will help you and anyone else encountering this issue.
Yes whatever it was it was a wrong tutorial, you need to make it a long byte integer.
Try this:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,L"Hello",L"Title",0);
return(0);
}