#include <iostream>
#include <WS2tcpip.h>
#include "sharedmemory.h"
#pragma comment (lib, "ws2_32.lib")
using namespace std;
#define MAP_OBJECT_NAME "$pcars2$"
void main(int argc, char* argv[])
{
HANDLE fileHandle = OpenFileMapping(PAGE_READONLY, FALSE, MAP_OBJECT_NAME);
I am relatively new to C++, and I happened across this road block. When I'm trying to place any #define
into an argument like on the OpenFileMapping()
line, I get an error saying:
C++ argument of type is incompatible with parameter of type
My end goal with this program is to send a UDP message that grabs data from shared memory.
Is this a result of me using void
instead of int
? I don't know.
TCHAR
is defined as eitherwchar_t
orchar
, depending on whetherUNICODE
has been defined or not in your project setup, respectively.OpenFileMapping()
is aTCHAR
-based preprocessor macro. It is defined as taking aconst TCHAR*
pointer to a null-terminated string in its 3rd parameter.In reality, what happens is that
OpenFileMapping()
maps to either theOpenFileMappingA()
(ANSI) orOpenFileMappingW()
(Unicode) function, depending on whetherUNICODE
is defined:Most legacy Win32 APIs that deal with character data are separated into
A
andW
versions like this. Newer APIs introduced in recent years tend to be Unicode-only.In your case,
UNICODE
is defined, so you are trying to pass a narrow string literal (const char[]
) where a Unicode string (constwchar_t*
) is expected. That is why you are getting a type mismatch error.When using character/string literals with
TCHAR
-based APIs, use theTEXT()
macro to ensure the literal uses the correct character type thatTCHAR
actually maps to, eg:Which is effectively doing the following when
UNICODE
is defined:And doing this when
UNICODE
is not defined:However, modern coding practices should not rely on
TCHAR
APIs at all. They are meant for backwards compatibility with legacy Win9x/ME code during Microsoft's push to migrate users to adopt Unicode in the early 2000s. You should use the ANSI or Unicode functions directly instead as needed.