| Winsock Home | Part 1 | Part 2 | Tenouk.com |


 

 

 

 

 

 

C, Winsok2 and Visual Studio 2008 (VC++) Professional Edition:

The How-to Build Guide Part 1

 

 

 

 

 

 

 

This module tries to show how to compile the Winsock program examples available at Tenouk.com using the Visual Studio with the CLR. This Visual IDE should be the newer version such as VS 2005 and 2008 (Express or full version). In this case we are going to use C++ from Visual Studio 2008, Professional Edition. The version is shown in the following screenshot. The OS used is Win XP Pro SP2.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation

 

In compiling the Winsock program examples using newer version IDE, we are not just learning how to customize the project environment to suit our programming needs. As can be learned in the following sections, we are also will get familiar with the IDE environments.

Okay, let start. Launch the VS 2008 > as usual, click File menu > click New menu > select Project sub menu.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 2

 

In the following project templates page, select the Win32 Console Application. Select Visual C++ folder on the left pane (or Win32 under the Visual C++ folder) for the Project Types: > select the Win32 Console Application project template.

Put the project and solution name in the Name: and Solution Name: fields respectively (in this case we use Winsock2sample for both fields). Change the Location: if required. Click the OK button.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 3

 

In the first page of the Win32 Application Wizard page, click the Next button. This just to refine the Win32 project more.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 4

 

On the second page, make sure the following settings are selected: Console Application radio button for the Application type: and an Empty Project for the Additional options: check box. At this stage, we try to exclude all the unnecessary items for our project environment. Then, click the Finish button.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 5

 

Now, we have been provided with an empty Win32 console mode application and we have 'disabled' the un-needed features. There are three folders provided for our project: Header Files, Resource Files and Source Files, same as the old plain C programming environment. Next, let add a source file, which suppose to be 'put' under the Source Files folder.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 6

 

Click the Project menu > select Add New Item sub menu.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 6

 

In the Add New Item page, select Visual C++ folder in the Categories: and select C++ File(.cpp) for the Templates:. Put the source file name (in this case we use Winsock2samplesrc as the source file name). The Location: should be defaulted to our project folder (in most cases, no need to change it). Click the Add button.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 7

 

The source file has been added properly under the Source Files folder. This empty file is shown on the right, else you need to double-click the source file under the Source Files folder (in this case, double-click the Winsock2samplesrc.cpp). Next, we are ready to do the coding or adding the codes.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 8

 

We just copy and paste the code example taken from http://www.tenouk.com/Winsock/Winsock2example.html page. The code is shown below.

 

// Run on Windows XP Pro machine, version 2002, SP 2

// C++ from Visual Studio 2008 Pro Edition

#include <stdio.h>

#include <winsock2.h>

 

int main()

{

      WORD wVersionRequested;

    WSADATA wsaData;

    int wsaerr;

 

    // Using MAKEWORD macro, Winsock version request 2.2

      wVersionRequested = MAKEWORD(2, 2);

      wsaerr = WSAStartup(wVersionRequested, &wsaData);

     

      if (wsaerr != 0)

      {

            /* Tell the user that we could not find a usable */

            /* WinSock DLL.*/

            printf("The Winsock dll not found!\n");

            return 0;

      }

      else

      {

            printf("The Winsock dll found!\n");

            printf("The status: %s.\n", wsaData.szSystemStatus);

      }

 

      /* Confirm that the WinSock DLL supports 2.2.*/

      /* Note that if the DLL supports versions greater    */

      /* than 2.2 in addition to 2.2, it will still return */

      /* 2.2 in wVersion since that is the version we      */

      /* requested.                                        */

     

      if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 )

      {

            /* Tell the user that we could not find a usable */

            /* WinSock DLL.*/

            printf("The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));

            WSACleanup();

            return 0;

      }

      else

      {

            printf("The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));

            printf("The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));

      }

 

    /* More task... */

      return 0;

}

 

Next, let try building (compile and link) this solution. Click the Build menu > click the Build Solution sub menu.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 9

 

Well, from the Output page, there are errors as shown in the following screenshot.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 10

 

The details of the 'rubbish', has been re-produced in the following text. The failure is at the link time with two symbols could not be resolved as highlighted.

 

1>------ Build started: Project: Winsock2sample, Configuration: Debug Win32 ------

1>Compiling...

1>Winsock2samplesrc.cpp

1>Compiling manifest to resources...

1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0

1>Copyright (C) Microsoft Corporation.  All rights reserved.

1>Linking...

1>Winsock2samplesrc.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main

1>Winsock2samplesrc.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main

1>C:\networkdotnetproject\Winsock2sample\Debug\Winsock2sample.exe : fatal error LNK1120: 2 unresolved externals

1>Build log was saved at "file://c:\networkdotnetproject\Winsock2sample\Winsock2sample\Debug\BuildLog.htm"

1>Winsock2sample - 3 error(s), 0 warning(s)

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

 

Let invoke the project’s property page. Click the Project menu > select what_ever_your_project_name Properties sub menu.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 11

 

We have to make sure that several settings are suited to our project type. First of all, make sure the compilation is for C code. Select the Configuration Properties folder > expand the C/C++ subfolder > select the Advanced node. Make sure Compile as C Code (/TC) option was selected under the Compile As setting. You can skip this step if the setting is OK.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 12

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 13

 

Next, rebuild the solution.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 14

 

Well, same error generated.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 15

 

The machine used to build this Win32 console mode application Winsock program, the Winsok2.h header files are found in various paths as shown below. Of course, yours should be different. In this machine we have installed several versions of the Visual Studio IDE which include the Express and full versions. What we want to confirm is that the winsock2.h header file is there.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 16

 

And also for the Winsock2 library (WS2_32.lib) as shown in the following Figure. Remember that yours should be different. However the file names must be the same (Winsock2.h and WS2_32.lib). In this case, (multiple similar file) the concerned thing that we need to consider is the version of those files. As usual, the best thing is to use the latest one.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 17

 

Notice that, in the previous Figures for the Winsock2.h header and WS2_32.lib files, both are not available under the %Microsoft Visual Studio 9.0%, the folder for Visual xxxx 2008. To verify this, you can try searching those file in the folder shown in the following Figure. If those files are available there, then it is easier for us to use those files directly (and the previous build errors should not occurred in the first place).

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 18

 

If those two files are available for Visual xxxx 2008, they should be found in the following subfolders. We found none. Ermmmm….we need to tell our VS 2008/ VC++ where to find the header and/or the library files implicitly or explicitly.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 19

 

Before that, let verify our 'assumption' regarding the 'cannot find' winsock2 header and/or the library file. Re-open the project property page. Expand the Configuration Properties folder > expand the Linkers folder > select the General node. On the right pane, for the Show Progress: setting, set to the Display All the Progress Messages (/VERBOSE).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 20

 

Next, under the Configuration Properties folder > C/C++ folder > Advanced node, set Yes (/showincludes) for the Show Includes. Save the new settings and close the page by clicking the OK button. Both steps will tell the C++ compiler and linker to 'spit out' all the messages for what is 'she' doing during those processes (compiling and linking).

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 21

 

If you build your program using the VC++ command line tools, the following Figure shows the options or switches that should be used for the same purpose.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 22

 

Next, rebuild the solution.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 23

 

Verify all the messages generated in the Output window. If the Output window is not docked in the IDE GUI, invoke it from the View > Output menu.

 

The Visual studio 2008/visual C++ .NET and winsock2 C code building (compile and link) issue and how-to documentation 24

 

Continue on the 2nd page...

 

 

 

 


| Winsock Home | Part 1 | Part 2 | Tenouk.com |