Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I have no C++ knowledge. I have the source code of a project and added these new lines to read IP and PORT values from Server section inside a ini file, and now I can't compile it. Can someone help me with this? Thank you.

C++
if (GetFileAttributes(gamepath.c_str()) != INVALID_FILE_ATTRIBUTES)
    {
        char ip[16];
        int port;

        GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", ip, _countof(ip), gamepath.c_str());
        GetPrivateProfileInt(L"Server", L"PORT", port, gamepath.c_str());
    }



char ip[16]; Error: argument of type "char*" is incompatible with parameter of type "LPWSTR"
Posted

You compile in Unicode so you cant use char. Use a unicode string or characters like wchar_t.
 
Share this answer
 
hi,
the error message explains it :
the function expexts you tp pass an argument of type LPWSTR but you are passing it a char.
so please convert the ip[16] to LPWSTR and it should work.

The function mbstowcs[^] can help you out.

a rough example :

C++
char *mychar;
size_t nSize1 = 1 + strlen( mychar );
LPWSTR wUserName = new WCHAR[nSize1];
mbstowcs( wUserName, mychar, nSize1 );
  // use wUserName but after all the usage do remember delete it
delete []wUserName;


Check out this msdn link[^] for mbstowcs.

hope this helps !!

i dont know your whole code, this is all just hit and trial
C++
 char ip[16];
 size_t nSize1 = 1 + strlen( ip );
 LPWSTR wIPName = new WCHAR[nSize1];
 mbstowcs( wIPName, ip, nSize1 );
 //now wIPNmae has the string in required fromat
 GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", wIPName, _countof(ip),gamepath.c_str());
//the _countof() function is also using 'ip', i hope it does not complain
 
 //when you are done with wIPNmae then delete it
delete []wIPName;
 
Share this answer
 
v3
Comments
chandanadhikari 16-May-15 5:46am    
comment out the line which gives error and try the new
code
froggz19 16-May-15 6:40am    
Ok, tried again, thank you for trying to help me. Now the messagebox shows up saying Bad ip Configured.

if (GetFileAttributes(gamepath.c_str()) != INVALID_FILE_ATTRIBUTES)
{
char ip[16];
size_t nSize1 = 1 + strlen(ip);
LPWSTR wIPName = new WCHAR[nSize1];
mbstowcs(wIPName, ip, nSize1);
//now wIPName has the string in required fromat
//GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", wIPName, _countof(ip), gamepath.c_str());
//the _countof() function is also using 'ip', i hope it does not complain

//when you are done with wIPNmae then delete it

int port;

GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", wIPName, _countof(ip), gamepath.c_str());
delete[]wIPName;
GetPrivateProfileInt(L"Server", L"PORT", port, gamepath.c_str());

if (ip[0] == '\0')
{
MessageBox(nullptr, va(L"Bad ip configured! %s", ip), L"Error", MB_OK | MB_ICONERROR); // Shows this
ExitProcess(0);
}
else
{
Connect(ip, port);
}
}
else
{
MessageBox(nullptr, va(L"Unable to read ini"), L"Error", MB_OK | MB_ICONERROR);
ExitProcess(0);
}
froggz19 16-May-15 7:11am    
I tried to convert using wcstombs (found something on google)

wchar_t ip[16];
char ipstr[16];
wcstombs(ipstr, ip, 16);

and called it

Connect(ipstr, port); but it didn't worked, it seems wchar_t is not being converted to normal char, it gives me a chinese letter or something if I call it with MessageBox(nullptr, LPCWSTR(ipstr), L"Error", MB_OK | MB_ICONERROR);

I tried MessageBox(nullptr, va(ip), L"Error", MB_OK | MB_ICONERROR); and it shows the ip inside the ini...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900