// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
// C++
#include <iostream>
using namespace std;

// Windows
#include <windows.h>
#include <initguid.h>
#include <tchar.h>

// our stuff
#import "SDN.tlb" no_namespace named_guids

void ReturnCheck(LPTSTR func, HRESULT res)
{
    if(res != S_OK)
    {
        TCHAR buffer[1000];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, res, 0, buffer, sizeof(buffer), 0);
        _tprintf("%s: %s\n", func, buffer);
        exit(1);
    }
}

int main()
{
    HRESULT res;
    CoInitialize(NULL);
    try
    {
        ISPtr spS;
        res = spS.CreateInstance(CLSID_S);
        ReturnCheck(_T("CreateInstance"), res);
        long a = 123;
        long b = 456;
        long c = spS->Add(a, b);
        wcout << c << endl;
        BSTR s = SysAllocString(L"ABC");
        BSTR s2 = spS->Dup(s);
        wcout << s2 << endl;
        SysFreeString(s);
        SysFreeString(s2);
        BSTR temp = SysAllocString(L"ABC");
        spS->PutIv(123);
        spS->PutSv(temp);
        SysFreeString(temp);
        spS->M();
        long iv = spS->GetIv();
        BSTR sv = spS->GetSv();
        wcout << iv << " " << sv << endl;
        SysFreeString(sv);
        spS = NULL;
    }
    catch (_com_error er)
    {
        _tprintf("%s\n", er.ErrorMessage());
    }
    //
    CoUninitialize();
    return 0;
}

