Today I have found this problem :
error LNK2019: unresolved external symbol impgethostname@8 referenced in function _wmain
and
fatal error LNK1120: 1 unresolved externals
code fragment is :
|
|
What’s wrong?
If you don’t add #include <WinSock2.h>, the compiler will report : error C3861: ‘gethostname’: identifier not found . But after you adding this header, it will report above error again. First, you should add the header <afxext.h> into stdafx.h, then compile the code again. Code will be compiled without any error. Then run this code fragment, and the result is not my expected. a is -1, and the buffer is filled with messy code. If you use WSAGetLastError() function to see it error code, the error code will be WSANOTINITIALISED. Yes, I have not initialized. So we should call WSAStartup() first and then gethostname. And WSAStartup() will check DLL version. And after having gotten hostname, don’t forget to call WSACleanup(). Here is the whole code:
|
|