FIX: LNK2001 on AfxSocketInit When Using __stdcall Default

Last reviewed: September 18, 1997
Article ID: Q135336
2.10 2.20 WINDOWS NT kbprg kbbuglist kbfixlist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, versions 2.1, 2.2

SYMPTOMS

An MFC sockets application that uses a default calling convention of __stdcall receives the following error:

<module name>.obj : error LNK2001: unresolved external symbol

   "?AfxSocketInit@@YGHPAUWSAData@@@Z
   (int  _stdcall AfxSocketInit(struct WSAData *))"

CAUSE

The AfxSocketInit function is declared in AFXSOCK.H as follows:

   BOOL AfxSocketInit(WSADATA* lpwsaData = NULL);

Note that it has no modifiers on the name of the function. Because of this, the function calling conventions used by the compiler for this function will be whatever the command-line setting is.

Because MFC is built with the default __cdecl calling convention, the AfxSocketInit function has its name decorated according to this convention. If your application is built with the __stdcall calling convention, then the function will be decorated according to this convention. Your code is effectively trying to call a function by a different name than the one that exists in the MFC library (due to the different name decoration), so the linker cannot resolve your reference.

RESOLUTION

Because MFC is built with the default calling convention of __cdecl, your application needs to have the AfxSocketInit declaration specifying this. Modify the declaration of AfxSocketInit in the header file AFXSOCK.H. This file is installed by default in the directory: \MSVC20\MFC\INCLUDE

Change the declaration of AfxSocketInit from:

   BOOL AfxSocketInit(WSADATA* lpwsaData = NULL);

To:

   BOOL __cdecl AfxSocketInit(WSADATA* lpwsaData = NULL);

After making the change to this declaration, rebuild the .CPP file in your project that contains the call to AfxSocketInit. Note that you won't need to rebuild MFC because the MFC libraries have been built with this function being marked as __cdecl.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++, 32- bit edition, version 4.0.

REFERENCES

For more information on how to track down LNK2001 errors, please use the Help file.


Additional reference words: 2.10 2.20 3.10 3.20
KBCategory: kbprg kbbuglist kbfixlist
KBSubcategory: MfcSockets
Keywords : MfcSockets kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 2.10 2.20
Platform : NT WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.