COM And Clipboard

So I found myself making a god awful application today, but in the end I am pretty happy with how it turned out.

This is a program to get the up time on a remote system that will be stared by an ancient inventory management system which is happy to call other applications but provides Zero ability for those other applications to communicate with it. So then how to we get data that it is incapable of gathering on it's own into this system you ask? Well, we hijack to clipboard of course! C&C is welcome as always. Thanks for your time!

MAJOR_EDIT: Code has been revised. See below.
Last edited on
So I didn't get much in the way of a response the first time, but the guys over in the IRC gave me some feed back so I did a revision to include some cleanup and error checking. So feel free to ask questions or to comment on any part of the following code. I'm looking for any errors or oversights especially now since adding the cleanup. I have this linking against the following MS libraries:
1
2
3
4
5
6
7
8
msvcrt.lib
msvcprt.lib
Ole32.lib
Wbemuuid.lib
User32.lib
Iphlpapi.lib
Ws2_32.lib
OleAut32.lib

And here is my revised code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <iostream>
#include <string>
#include <cwchar>
#include <cctype>

#define _WIN32_WINNT 0x0600
#define WIN32_LEAN_AND_MEAN ///This Must Be Done So That The 'Winsock.h' Header Is Not Included With 'Windows.h'

#include <windows.h>
#include <winbase.h>
#include <WinCon.h>
#include <Winuser.h>

#include <Objbase.h>
#include <Wbemidl.h>
#include <Wbemcli.h>

#include <Ipexport.h>
#include <Icmpapi.h>
#include <Ws2tcpip.h>

class DATES
{
private:


public:

    DATES(VARIANT& Var)
    {
        std::wstring BaseString;
        std::wstring YearString;
        std::wstring MonthString;
        std::wstring DayString;
        std::wstring HourString;
        std::wstring MinuteString;

        BaseString = Var.bstrVal;

        YearString = BaseString.substr(0, 4);
        Year = wcstol(YearString.c_str(), NULL, 10);

        MonthString = BaseString.substr(4, 2);
        Month = wcstol(MonthString.c_str(), NULL, 10);

        DayString = BaseString.substr(6, 2);
        Day = wcstol(DayString.c_str(), NULL, 10);

        HourString = BaseString.substr(8, 2);
        Hour = wcstol(HourString.c_str(), NULL, 10);

        MinuteString = BaseString.substr(10, 2);
        Minute = wcstol(MinuteString.c_str(), NULL, 10);
    }

    std::wstring DateDifference(DATES& Dates)
    {
        WCHAR DateDifString[MAX_PATH];

        long lYear = Year - Dates.Year;

        long lMonth = Month - Dates.Month;

        long lDay = Day - Dates.Day;

        long lHour = Hour - Dates.Hour;

        long lMinute = Minute - Dates.Minute;


        if(lMinute < 0)
        {
           lMinute = lMinute + 60;
           lHour = lHour - 1;
        }

        if(lHour < 0)
        {
           lHour = lHour + 24;
           lDay = lDay - 1;
        }

        if(lDay < 0)
        {
           lDay = lDay + 30;
           lMonth = lMonth - 1;
        }

        if(lMonth < 0)
        {
           lMonth = lMonth + 12;
        }


        swprintf_s(DateDifString, MAX_PATH, L"Yrs: %i Mts: %i Dys: %i Hrs: %i Min: %i", lYear, lMonth, lDay, lHour, lMinute);

        std::wstring Difference = DateDifString;
        return Difference;
    }

    long Year;
    long Month;
    long Day;

    long Hour;
    long Minute;
};


int main(int NumArgs, char* Argvc[])
{
    WSADATA WsaData;
    WSAStartup(MAKEWORD(2,2), &WsaData);

    addrinfo* Hints = new addrinfo;
        Hints->ai_flags = AI_ADDRCONFIG;
        Hints->ai_family = AF_UNSPEC;
        Hints->ai_addrlen = 0;
        Hints->ai_canonname = NULL;
        Hints->ai_next = NULL;
        Hints->ai_addr = NULL;
        Hints->ai_socktype = SOCK_STREAM;

    addrinfo* IP_Address = NULL;

    HANDLE ICMP = IcmpCreateFile();

    WCHAR strComputer[MAX_COMPUTERNAME_LENGTH];
    WCHAR ComConnection[MAX_PATH];

    IWbemLocator* pLocator = NULL;
    IWbemServices* pWMI = NULL;

    ULONG ClassEnumReturned = 0;
    IEnumWbemClassObject* ClassEnum = NULL;
    IWbemClassObject*     ClassInstance = NULL;

    VARIANT LastBootTime;
    VARIANT LocalDateTime;

    BOOL Clipboard = OpenClipboard(GetConsoleWindow());
    EmptyClipboard();

    char Err[MAX_PATH];
    char SendBuffer[MAX_PATH];

    char* cStrData = NULL;;

    const int rBufferSize = 100;
    ICMP_ECHO_REPLY rBuffer[rBufferSize];
        ZeroMemory(rBuffer, sizeof(rBuffer));

    std::string Data = "NO_DATA";
    size_t DataSize = Data.size() + 1;

    HGLOBAL gHandle = NULL;
    LPVOID hMemory = NULL;

    std::string HostName;
    std::string dotAddress;

    std::wstring wStrData;

    TCHAR* IP_String = NULL;
    sockaddr_in* AddrIn = NULL;
    IPAddr IP;

    size_t cStrDataSize = 0;

    try
    {
        if(!Clipboard)
        {
            throw "NO CLIPBOARD";
        }

        if(NumArgs >= 2)
        {
            HostName = Argvc[1];

            if(HostName.length() >= MAX_COMPUTERNAME_LENGTH)
            {
                throw "Host Name Too Long";
            }
            else
            {
                mbstowcs_s(NULL, strComputer, MAX_COMPUTERNAME_LENGTH, Argvc[1], MAX_COMPUTERNAME_LENGTH);
            }
        }
        else
        {
            throw "Too Few Arguments";
        }

        swprintf_s(ComConnection, MAX_PATH, L"\\\\%s\\ROOT\\CIMV2", strComputer);

        CoInitializeEx(NULL, COINIT_MULTITHREADED);
        CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);

        CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*) &pLocator);

        if(getaddrinfo(HostName.c_str(), NULL, Hints, &IP_Address) != 0)
        {
            sprintf(Err, "COULD NOT RESOLVE ADDRESS %i", GetLastError());

            throw Err;
        }

        IP_String = new TCHAR[IP_Address->ai_addrlen];
        AddrIn = reinterpret_cast<sockaddr_in*>(IP_Address->ai_addr);

        dotAddress = inet_ntoa(AddrIn->sin_addr);
        IP = inet_addr(dotAddress.c_str());

        if(IcmpSendEcho(ICMP, IP, SendBuffer, MAX_PATH, NULL, rBuffer, sizeof(rBuffer), 150) == 0)
        {
            if(IcmpSendEcho(ICMP, IP, SendBuffer, MAX_PATH, NULL, rBuffer, sizeof(rBuffer), 150) == 0)
            {
                throw "NO PING";
            }
        }

        if(pLocator->ConnectServer(BSTR(ComConnection), NULL, NULL, 0, NULL, 0, 0, &pWMI) != WBEM_NO_ERROR)
        {
            sprintf(Err, "COM ERROR %i", GetLastError());

            throw Err;
        }

        CoSetProxyBlanket(pWMI, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
        pWMI->ExecQuery(BSTR(L"WQL"), BSTR(L"Select * From Win32_OperatingSystem"), WBEM_FLAG_BIDIRECTIONAL, NULL, &ClassEnum);

        ClassEnum->Next(WBEM_INFINITE, 1, &ClassInstance, &ClassEnumReturned);

        ClassInstance->Get(BSTR(L"LastBootUpTime"), 0, &LastBootTime, NULL, NULL);
        ClassInstance->Get(BSTR(L"LocalDateTime"), 0, &LocalDateTime, NULL, NULL);

        DATES LastBoot(LastBootTime);
        DATES LocalDate(LocalDateTime);

        wStrData = LocalDate.DateDifference(LastBoot);

        cStrDataSize = wStrData.size() + 1;
        cStrData = new char[cStrDataSize];

        wcstombs_s(NULL, cStrData, cStrDataSize, wStrData.c_str(), cStrDataSize);

        Data = cStrData;

        delete cStrData;
        delete IP_String;

        VariantClear(&LastBootTime);
        VariantClear(&LocalDateTime);

        ClassEnum->Release();
        ClassInstance->Release();

        pWMI->Release();
        pLocator->Release();
    }
    catch(const char* Msg)
    {
        Data = Msg;
    }

    DataSize = Data.size() + 1;

    gHandle = GlobalAlloc(GMEM_ZEROINIT, DataSize);
    hMemory = GlobalLock(gHandle);

    memcpy(hMemory, Data.data(), DataSize);

    SetClipboardData(CF_TEXT, gHandle);
    CloseClipboard();

    GlobalUnlock(gHandle);
    GlobalFree(gHandle);

    delete Hints;

    WSACleanup();

    CoUninitialize();

    return 0;
}
Last edited on
What was the point of this application? To get the uptime of the system?
We use this decrepit old system in my office to manage where our PC's are and what software licensing they have applied to them. This "Database" does not allow external applications to interact with it but it does provide the ability to launch other apps and also the ability to paste from the clipboard into itself. This was something I wrote so that we could get the up time for a set of remote systems, one at a time, and feed that data into the clipboard so that this DB could then paste it into one of it's fields. It's a horrible workaround conceptually but I'm happy with the way it turned out on this end.
Last edited on
Topic archived. No new replies allowed.