armage

Just installed Microsoft Platform SDK R2. I tried a simple code, it won't compile. Before installation, no errors for type "char" when compiled using c++ express.

Followed instructions on "How to: Use Visual C++ Express Edition with the Microsoft Platform SDK"

http://msdn2.microsoft.com/en-us/library/ms235626(VS.80).aspx

Create Project ->Win32 Console Application

Accept defaults in Wizard.

/*The code below won't compile*/

#include "stdafx.h"
#include "WinSock2.h"


int _tmain(int argc, _TCHAR* argv[])
{

WSADATA WSAData;
// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
// Error handling
// Get local host name
char szHostName[128] = "";
if(::gethostname(szHostName, sizeof(szHostName)))
// Error handling -> call 'WSAGetLastError()'
// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;
pHost = ::gethostbyname(szHostName);
if(!pHost)
// Error handling -> call 'WSAGetLastError()'
char aszIPAddresses[10][16]; // maximum of ten IP addresses
for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}
// Cleanup
WSACleanup();

return 0;
}

/*Output*/

------ Build started: Project: cpplocalhost, Configuration: Debug Win32 ------

Compiling...

stdafx.cpp

Compiling...

cpplocalhost.cpp

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(16) : error C2065: 'szHostName' : undeclared identifier

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(16) : error C2070: ''unknown-type'': illegal sizeof operand

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(27) : error C2065: 'SocketAddress' : undeclared identifier

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(27) : error C2228: left of '.sin_addr' must have class/struct/union

type is ''unknown-type''

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(28) : error C2065: 'aszIPAddresses' : undeclared identifier

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(28) : error C2228: left of '.sin_addr' must have class/struct/union

type is ''unknown-type''

Build log was saved at "file://c:\Project\Get Localhost IP\cpplocalhost\cpplocalhost\Debug\BuildLog.htm"

cpplocalhost - 6 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Re: Visual C++ Express Edition platform SDK installed - won't compile

Jonathan Caves - MSFT

The problem here is not with the PSDK. What is this code meant to do:

Code Snippet
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
// Error handling
// Get local host name
char szHostName[128] = "";

I think that you missing some braces, { }, after the if statement. The code you have written it is equivalent to:

Code Snippet
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
// Get local host name
char szHostName[128] = "";
}

Which explains the error messages that you are seeing.







Re: Visual C++ Express Edition platform SDK installed - won't compile

armage

Thank you for the quick response. I followed your suggestions to include braces. That helps to eliminate the type related errors.

I still run into some linking problems. Could you some help here.

/*Source Code*/

// cpplocalhost.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "winsock2.h"

int _tmain(int argc, _TCHAR* argv[])

{

WSADATA WSAData;

// Initialize winsock dll

if(::WSAStartup(MAKEWORD(1, 0), &WSAData))

{// Error handling

}

// Get local host name

char szHostName[128] = "";

if(::gethostname(szHostName, sizeof(szHostName)))

{

// Error handling -> call 'WSAGetLastError()'

}

// Get local IP addresses

struct sockaddr_in SocketAddress;

struct hostent *pHost = 0;

pHost = ::gethostbyname(szHostName);

if(!pHost)

{// Error handling -> call 'WSAGetLastError()'

char aszIPAddresses[10][16]; // maximum of ten IP addresses

for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)

{

memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);

strcpy_s(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));

}

}

// Cleanup

WSACleanup();

return 0;

}

/*Output*/

------ Build started: Project: cpplocalhost, Configuration: Debug Win32 ------

Compiling...

cpplocalhost.cpp

Linking...

cpplocalhost.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _wmain

cpplocalhost.obj : error LNK2019: unresolved external symbol __imp__inet_ntoa@4 referenced in function _wmain

cpplocalhost.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function _wmain

cpplocalhost.obj : error LNK2019: unresolved external symbol __imp__gethostname@8 referenced in function _wmain

cpplocalhost.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _wmain

C:\Project\Get Localhost IP\cpplocalhost\Debug\cpplocalhost.exe : fatal error LNK1120: 5 unresolved externals

Build log was saved at "file://c:\Project\Get Localhost IP\cpplocalhost\cpplocalhost\Debug\BuildLog.htm"

cpplocalhost - 6 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





Re: Visual C++ Express Edition platform SDK installed - won't compile

Jonathan Caves - MSFT

Here's the help page for WSACleanup - if you look near the bottom of the page you'll see that you need to link to Ws2_32.lib. I would go to Project Properties -> Configuration Properties -> Linker -> Input and add this library to the Addtional Dependencies.






Re: Visual C++ Express Edition platform SDK installed - won't compile

armage

Thanks Jonathan, it is working now. Cheers





Re: Visual C++ Express Edition platform SDK installed - won't compile

armage

I am getting this error related to this program. I have not changed anything in the program nor have I changed anything in Platform SDK.

When I searched the directories I could not find Ws2_32.lib. Where is this library located

------ Build started: Project: cpplocalhost, Configuration: Debug Win32 ------

Linking...

LINK : fatal error LNK1104: cannot open file 'Ws2_32.lib'

Build log was saved at "file://c:\Project\Get Localhost IP\cpplocalhost\cpplocalhost\Debug\BuildLog.htm"

cpplocalhost - 1 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





Re: Visual C++ Express Edition platform SDK installed - won't compile

armage

When I tried to compile this program which has not changed. I get the error.

------ Build started: Project: cpplocalhost, Configuration: Debug Win32 ------

Compiling...

stdafx.cpp

Compiling...

cpplocalhost.cpp

c:\project\get localhost ip\cpplocalhost\cpplocalhost\cpplocalhost.cpp(5) : fatal error C1083: Cannot open include file: 'winsock2.h': No such file or directory

Build log was saved at "file://c:\Project\Get Localhost IP\cpplocalhost\cpplocalhost\Debug\BuildLog.htm"

cpplocalhost - 1 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The rest of the settings are the same. No Changes. Strange.