sendmessage callback error in visual studio 2012 - c

sendmessage callback error in visual studio 2012
I am recieving following errors in this C function?
error C2220: warning treated as error - no 'object' file generated
warning C4100: 'wParam' : unreferenced formal parameter
warning C4100: 'hwnd' : unreferenced formal parameter
LRESULT CALLBACK WndProc (HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_COPYDATA:
{
COPYDATASTRUCT* copy_data = (COPYDATASTRUCT*)(lParam);
const char* str = (const char* )(copy_data->lpData);
/* Also fixed the parameter list for "%.*s" */
printf("Message (%u): %.*s\n", copy_data->dwData, (int)copy_data->cbData, str);
}
}
}

The error is that you told the compiler to treat warnings as errors, and you have two warnings about arguments that is not used in the function.
The simplest solution is to actually skip naming those arguments, like so:
LRESULT CALLBACK WndProc (HWND, UINT message, WPARAM, LPARAM lParam)
For a solution working in C, then you have to actually use the arguments, even if you don't do anything with them. This can be done with e.g. using them in an expression and throw away the result, like
(void) hwnd;
(void) wParam;
Or use the macros defined in winnt.h (which should be included for you by default) for this:
UNREFERENCED_PARAMETER(hwnd);
UNREFERENCED_PARAMETER(wParam);
See e.g. this article.

Related

Undefined reference to `StringCbPrintfW' when compiling Win32 app

I'm trying to compile a Win32 app with MinGW, whose code can be found here. The source code compiles into an object just fine, but when I try and link it, this happens:
> gcc -o bin\updown.exe obj\updown.o -s -lcomctl32 -Wl,--subsystem,windows
obj\updown.o:updown.c:(.text+0x3ac): undefined reference to `StringCbPrintfW'
collect2.exe: error: ld returned 1 exit status
The only time this function appears is in the following snippet:
#include <windows.h>
#include <commctrl.h>
#include <strsafe.h>
...
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
LPNMUPDOWN lpnmud;
UINT code;
switch(msg) {
...
case WM_NOTIFY:
...
const int asize = 4;
wchar_t buf[asize];
size_t cbDest = asize * sizeof(wchar_t);
StringCbPrintf(buf, cbDest, L"%d", value);
SetWindowText(hStatic, buf);
}
break;
...
}
...
}
Your project can define STRSAFE_LIB before including strsafe.h, and then link to strsafe.lib. Note that strsafe.h will use a #pragma comment(lib, "strsafe.lib") statement if STRSAFE_LIB is defined, but I don't know if that pragma works in MinGW or not, so you may have to link to the .lib manually.

Warning C4047 : '=': 'int' differs in levels of indirection from 'int *'

I am working on this code:
BOOL CALLBACK bWSStatus(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static int npFlag;
switch (iMsg)
{
case WM_INITDIALOG:
npFlag = (int *)lParam;//Geht in Ordnung, der Wert auf den der Zeiger lParam zeigt ist für npFlag relevant
...
I am getting this warning Warning C4047 : '=': 'int' differs in levels of indirection from 'int *'. Can someone please explain it?
Translation of the comment by Google is
Okay, the value pointed to by the lParam pointer is relevant for npFlag
It seams you forgot to dereference the pointer.
// add * to dereference the pointer
npFlag = *(int *)lParam;

BOOL not recognized as data type (C...?) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to compile an application from source code with Visual Studio 2015 Enterprise. I've run into some problems.
When I try to compile, I get several errors related to BOOL being an undefined type. I've tried including winmindef.h and windef.h to no avail. Here's some of the function declarations in the code that throw errors:
void GetCheatName ( int CheatNo, char * CheatName, int CheatNameLen );
BOOL LoadCheatExt ( char * CheatName, char * CheatExt, int MaxCheatExtLen);
void RefreshCheatManager ( void );
void SaveCheatExt ( char * CheatName, char * CheatExt );
BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem);
BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck);`
When I include the following in the .h file, the problem is still not resolved
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef int BOOL;
If it helps, I am trying to compile PJ64, and may need to change some setting in order to compile, but I'm not sure which.
Any help at all is appreciated!
---UPDATE---
Here are the complete compile errors (I've omitted most of the warnings):
1>cl : Command line warning D9007: '/Gm' requires '/Zi or /ZI'; option ignored
1> Cheat.c
1>Cheat.c(55): error C2059: syntax error: '<parameter-list>'
1>Cheat.c(56): error C2059: syntax error: '{'
1>Cheat.c(233): warning C4267: '=': conversion from 'size_t' to 'WORD', possible loss of data
1>Cheat.c(326): warning C4267: '=': conversion from 'size_t' to 'WORD', possible loss of data
1>Cheat.c(346): warning C4267: '=': conversion from 'size_t' to 'WORD', possible loss of data
1>Cheat.c(982): error C2059: syntax error: '<parameter-list>'
1>Cheat.c(998): error C2059: syntax error: '{'
1>Cheat.c(999): error C2449: found '{' at file scope (missing function header?)
1>Cheat.c(1012): error C2059: syntax error: '}'
The complete code for the project I am trying to compile can be found here.
This is -part- of the code that was compiled and threw the above errors. I've left the vast majority of the code out because it is too lengthy to include here. I've left comments indicating which lines threw errors.
#include <Windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <windef.h>
#include <stdio.h>
#include "main.h"
#include "cheats.h"
#include "cpu.h"
#include "resource.h"
#define UM_CHECKSTATECHANGE (WM_USER + 100)
#define UM_CHANGECODEEXTENSION (WM_USER + 101)
#define IDC_MYTREE 0x500
#define MaxCheats 300
#define SelectCheat 1
#define EditCheat 2
#define NewCheat 3
HWND hManageWindow = NULL;
HWND hSelectCheat, hAddCheat, hCheatTree;
CHEAT_CODES Codes[MaxCheats];
int NoOfCodes;
void GetCheatName ( int CheatNo, char * CheatName, int CheatNameLen );
BOOL LoadCheatExt ( char * CheatName, char * CheatExt, int MaxCheatExtLen);
void RefreshCheatManager ( void );
void SaveCheatExt ( char * CheatName, char * CheatExt );
BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem); //(line 55, error thrown.)
BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck); //(line 56)
LRESULT CALLBACK ManageCheatsProc (HWND, UINT, WPARAM, LPARAM );
void ApplyCheats (void) {
//lots 'o code
}
BOOL CheatActive (char * Name) {
//more code
}
LRESULT CALLBACK CheatsCodeExProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
//Too much code to contain in this post...
}
LRESULT CALLBACK CheatsCodeQuantProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
//code
}
//Because of the inmensity of code, I will skip to the lines that throw errors.
BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem) { //line 982, error thrown.
//code here
}
BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck) //line 999, error thrown
{ //line 1000, error thrown
//Lots o code
} // line 1013, error thrown
If more code is desired, I'll gladly post!
I've checked the code you gave a link to.
BOOL type is properly included. To check that you can compile Cheat.c with /P option.
All the problems are caused by upgrade of the project from Visual Studio 6 to Visual Studio 2015.
If you will investigate the pre-processor ouput you will see the transformation:
BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem); //(line 55, error thrown.)
BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck); //(line 56)
pre-processed to:
BOOL ((((UINT)(SendMessageA((HWND hwndTreeView), (0x1100 + 39), (WPARAM)(HTREEITEM hItem), 0xF000))) >> 12) -1);
BOOL { TVITEMA _ms_TVi; _ms_TVi.mask = 0x0008; _ms_TVi.hItem = (HTREEITEM hItem); _ms_TVi.stateMask = (0xF000); _ms_TVi.state = ((((BOOL fCheck)?2:1) << 12)); SendMessageA((HWND hwndTreeView), (0x1100 + 13), 0, (LPARAM)(TVITEMA *)&_ms_TVi);};
It's not what you expect to see in your function declarations.
Both of these TreeView_GetCheckState and TreeView_SetCheckState are standard macroses defined in CommCtrl.h. Remove your old functions and use these macroses.

How to avoid "multiple definition" error for global constants?

I'm writing a C program using the Windows API. Each major function has its own file, and there is one header for the prototypes and includes and whatnot:
// Headers & global constants
#pragma once
#define _WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WindowsX.h>
#include <Windef.h>
#define szClassName TEXT("EthicsPresentationWnd")
// Prototypes
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK FontProc1(HWND hWnd, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
ATOM RegisterWindow(HINSTANCE hInstance);
The thing I'm irritated about is the #define szClassName line. I dislike using macros and would prefer to have a proper global variable, wchar_t szClassName[], but if I do that then the linker complains about multiply defined variables in each of the modules that include the header.
I thought the #pragma once directive would prevent this, but it didn't.
Is there any solution to this problem?
The solution to this is to have a separate declaration and definition...
Header (*.h; sorry, I don't know WinAPI type names, adapt as necessary):
extern const char szClassName[];
Implementation (*.c or *.cpp)
const char szClassName[] = "hello, world"
You're seeing the problem because a new symbol szClassName is being declared each time one of your *.c or *.cpp files includes the header (even with the include guards!); and that makes the linker confused (see below).
Do note that this will make sizeof(szClassName) not work anymore.
Further explanation:
After preprocessing, the compiler is basically seeing this:
file "a.c": const char someSymbol[] = <some text, don't care what right now>;
file "b.c": const char someSymbol[] = <some text, don't care if it's the same>;
file "c.c": const char someSymbol[] = <some text, ditto>;
When the linker is linking the object files (say, "a.obj", "b.obj" and "c.obj"), it sees the same symbol being defined with a new value (at least as far as the linker is concerned) --- and thus it fails with an error.
Place it in between
#ifndef GLOB_CONST_H
#define GLOB_CONST_H
// All macro definitions
// and type definitions
#endif
Use extern keyword to declare your global variables and put those declarations in this header file. After that you need to place the definition of all the variables in a .c file.
You can declare the variable as static so that each module that includes the .h file gets its own local unique copy that the linker will not complain about, as each copy will have local linkage instead of external linkage. This also eliminates the need for declaring the variable as extern and defining it in a separate .c file.
static const TCHAR szClassName[] = TEXT("EthicsPresentationWnd");
Or;
static const TCHAR *szClassName = TEXT("EthicsPresentationWnd");
Or:
static LPCTSTR szClassName = TEXT("EthicsPresentationWnd");
Use header guards in all your header file and declare a global variable in .c file and declare extern to that global variable in a header file.
#ifndef HEADER_FILE_NAME_H /* if not defined already */
#define HEADER_FILE_NAME_H
extern wchar_t szClassName[];
#endif
In any one of your .c file define the global variable.
wchar_t szClassName[];

getting Strange error in WINAPI during linking

this is the error I am getting during linking:
main.obj : error LNK2001: unresolved external symbol "long __stdcall windowProcess(struct HWND__ *,unsigned int,unsigned int,long)" (?windowProcess##YGJPAUHWND__##IIJ#Z)
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/main.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
main.exe - 3 error(s), 0 warning(s)
here is my source code
#include <windows.h>
//#include <stdlib.h>
//#include <string.h>
#include "stdafx.h"
//1. Declaring windowProcess of type LRESULT
//2. WIN API
//3. WIndow Class
//4. Register Class
//5. Create Window
//6. Show Window
// The main window class name
const char myClassName[] = "myClass";
//1. Moving towards 1st step & showing the ass of Window Process
LRESULT CALLBACK windowProcess(HWND,UINT,WPARAM,LPARAM);
//2. Using win api
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevIntance,
LPSTR lpcmdLine,
int comandNumber){
WNDCLASS wc;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=windowProcess;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName=myClassName;
//3. Registering Wnd Class
if(!RegisterClass(&wc))
{
MessageBox(NULL,"Class didnot get registered","An Simple Window Application",NULL);
}
//hint=hInstance;
//4. registering window class
HWND hWnd=CreateWindow(myClassName,"Title f Window",WS_OVERLAPPEDWINDOW,0,0,500,100,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"window didnt created","An Simple Windw Application",NULL);
}
//5. Now after window has been created lets show this window
ShowWindow(hWnd,comandNumber);
UpdateWindow(hWnd);
return 1;
}//winapi WinMain function ends
The error tells you that the function
LRESULT CALLBACK windowProcess(HWND,UINT,WPARAM,LPARAM);
is only declared and defined nowhere.
Check Project props -> Linker -> System -> SubSystem.

Resources