#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.
TCHARis defined as eitherwchar_torchar, depending on whetherUNICODEhas 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 whetherUNICODEis defined:Most legacy Win32 APIs that deal with character data are separated into
AandWversions like this. Newer APIs introduced in recent years tend to be Unicode-only.In your case,
UNICODEis 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 thatTCHARactually maps to, eg:Which is effectively doing the following when
UNICODEis defined:And doing this when
UNICODEis not defined:However, modern coding practices should not rely on
TCHARAPIs 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.