A Client in Managed Extensions for C++

Here is how your client looks in Managed Extensions for C++:

Listing 1. Client in Managed Extensions for C++ (ClientVC.cpp)

#using <mscorlib.dll>
using namespace System;

#using "..\Bin\CompCS.dll"
#using "..\Bin\CompVC.dll"
#using "..\Bin\CompVB.dll"

// The method, main, is the application's entry point.
void main() {

   // Iterate through the component's strings,
   // and write them to the console.
   CompCS::StringComponent* myCSStringComp = 
      new CompCS::StringComponent();
   Console::WriteLine  
      (L"Strings from C# StringComponent");
   for (int index = 0; index < myCSStringComp->Count; 
      index++) {
      Console::WriteLine(myCSStringComp-> 
         GetString(index));
   }

   // Iterate through the component's strings,
   // and write them to the console.
   CompVC::StringComponent* myVCStringComp = 
      new CompVC::StringComponent();
   Console::WriteLine 
     (L"\nStrings from Visual C++ StringComponent");
   for (int index = 0; index < myVCStringComp->Count; 
      index++) {
      Console::WriteLine(myVCStringComp-> 
   GetString(index));
   }

   // Iterate through the component's strings,
   // and write them to the console.
   CompVB::StringComponent* myVBStringComp = 
      new CompVB::StringComponent();
   Console::WriteLine(L"\nStrings from Visual Basic 
      StringComponent");
   for (int index = 0; index < myVBStringComp->Count; 
      index++) {
      Console::WriteLine(myVBStringComp-> 
   GetString(index));
   }
}

The first thing to note is the importing of the three components, each of which is now located in the appropriate ..\Bin subdirectory:

#using "..\Bin\CompCS.dll"
#using "..\Bin\CompVC.dll"
#using "..\Bin\CompVB.dll"

The sections of the client code that call the three string components are identical except for the specification of which library to use. The first statement in each of the three sections declares a new, local variable of type StringComponent (which is defined in the component), initializes the variable, and calls its constructor:

CompCS::StringComponent* myCSStringComp = 
new CompCS::StringComponent();

After writing a string to the console to indicate that this part of the program has been entered, the client iterates through the members of the appropriate string component using the value of the Count property:

for (int index = 0; index < myCSStringComp->Count; 
    index++) {
    Console::WriteLine(myCSStringComp-> 
 GetString(index));
}

That is all that is required, and everything is repeated for the other two language components.

**Note   **If you had used the indexer approach rather than the separate GetString method, the myCSStringComp[index] calling code would have been more natural.

Building the new Managed Extensions for C++ client is straightforward:

cl.exe /clr /Zi /c ClientVC.cpp
link.exe /debug /nod:libcpmt.lib 
    kernel32.lib mscoree.lib 
    /out:..\bin\ClientVC.exe ClientVC.obj

As with the previous Managed Extensions for C++ examples, you need the /clr switch to tell the compiler to create common language runtime managed code. Running the resulting program yields the following:

C:\...\CompTest\Bin>clientvc
Strings from C# StringComponent
C# String 0
C# String 1
C# String 2
C# String 3

Strings from Visual C++ StringComponent
Visual C++ String 0
Visual C++ String 1
Visual C++ String 2
Visual C++ String 3

Strings from Visual Basic StringComponent
Visual Basic String 0
Visual Basic String 1
Visual Basic String 2
Visual Basic String 3

See Also

A Client in Visual C# | A Client in Visual Basic | A Windows Client Using Windows Forms | A Client Using ASP.NET | Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces