Thread: C# versus C++
View Single Post
Old 11-02-2011, 09:01 PM   #1
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default C# versus C++

I found this funny and quite eloquent. In this MSDN walkthrough article there are alternative samples in different COM-aware languages. This is one of them in C#:

Code:
using System;
using SideBySideLib;

class Example
{
   static void Main()
   {
      var obj = new SideBySideClassClass();
      Console.WriteLine(obj.Version());
      Console.ReadLine();
   }
}
To do the same in C++ you have to (and thank God you have Visual Studio):

Quote:
Create a new Visual C++ Win32 Console Project called client in a sibling folder relative to the SideBySide project's folder. In the Win32 Application Wizard, on the Application Settings tab, check the Add support for ATL check box.

Edit stdafx.h and add the following line at the top of the file, immediately after the #pragma once:
Code:
#define _WIN32_DCOM
Also in stdafx.h add the following line at the bottom of the file:
Code:
#import "..\deployed\SideBySide.dll" no_namespace
Replace the contents of client.cpp with this code:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

void ErrorDescription(HRESULT hr)
{
    TCHAR* szErrMsg;
    if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
      FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
      (LPTSTR)&szErrMsg, 0, NULL) != 0)
   {
        cout << szErrMsg << endl;
        LocalFree(szErrMsg);
    }
   else
        cout << "Could not find a description for error 0x" 
          << hex << hr << dec << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
   CoInitializeEx(0, COINIT_MULTITHREADED);

   {
      ISideBySideClassPtr ptr;
      HRESULT hr = ptr.CreateInstance(__uuidof(SideBySideClass));
      if (SUCCEEDED(hr))
      {
         cout << ptr->Version() << endl;
      }
      ErrorDescription(hr);

      char c;
      cin >> c;
   }

   CoUninitialize();

   return 0;
}
Wow.
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote