Solved

byte to const char * conversion error

Posted on 2009-07-15
3
2,010 Views
Last Modified: 2013-12-14

I have the function below.  It fails to compile with the following error:
u:\myDll\WinNetTools.c(151): error C2664: 'strlen' : cannot convert parameter 1 from 'BYTE [20]' to 'const char *'

The error is referring to this line:
   int iPos = strlen(psClientAddr->Address) == 4 ? 0 : 2;
   char* pszIP = inet_ntoa(*((struct in_addr *)(&psClientAddr->Address[iPos])));


Can anyone tell me what I'm doing wrong?
int GetRemoteIP(const DWORD dwASessionID, char* pszIPBuff)

{

	int rc = FALSE;

	LPTSTR pszBuffer = NULL;

	DWORD dwBytes = 0;
 

	if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwASessionID, WTSClientAddress, &pszBuffer, &dwBytes)) {
 

		WTS_CLIENT_ADDRESS* psClientAddr = (WTS_CLIENT_ADDRESS *)pszBuffer;

		if (psClientAddr->AddressFamily == AF_INET) {
 

			/* if the return addr contains a port, ignore the port */

			int iPos = strlen(psClientAddr->Address) == 4 ? 0 : 2;

			char* pszIP = inet_ntoa(*((struct in_addr *)(&psClientAddr->Address[iPos])));

			if (pszIP) {

				strncpy(pszIPBuff, pszIP, MAX_IPADDR_LENGTH);

				pszIPBuff[MAX_IPADDR_LENGTH] = 0;

				rc = TRUE;

			}

		}

		WTSFreeMemory(pszBuffer);

	}

	pszIPBuff[MAX_IPADDR_LENGTH] = '\0';

	return rc;

}

Open in new window

0
Comment
Question by:dhenderson12
3 Comments
 
LVL 14

Accepted Solution

by:
flob9 earned 250 total points
ID: 24865031
This should work :

int iPos = strlen((char*)psClientAddr->Address) == 4 ? 0 : 2;
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 24868512
To add to above information:

The psClientAddr->Address seems to be a 'unsigned char[20]' type. The compiler refuses to convert that to a 'const char*' cause unsigned char or BYTE type normally doesn't describe a zero-terminated string as it was with const char*.

Casting the unsigned char array to char* may help out of this (what simply tells the compiler not to care for the type difference)though I have doubts.  Why is the Address member an unsigned char array if it is a zero-terminated string?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 60
MS Visual Studio 2015 - Install Error 5 82
Visual Studio 2015 locks debug executable 9 128
Angular JS Route 3 45
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now