Class: Winevt::EventLog::Channel

Inherits:
Object
  • Object
show all
Defined in:
ext/winevt/winevt_channel.c

Instance Method Summary collapse

Constructor Details

#initializeObject



34
35
36
37
38
# File 'ext/winevt/winevt_channel.c', line 34

static VALUE
rb_winevt_channel_initialize(VALUE klass)
{
  return Qnil;
}

Instance Method Details

#eachObject



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
# File 'ext/winevt/winevt_channel.c', line 40

static VALUE
rb_winevt_channel_each(VALUE self)
{
  EVT_HANDLE hChannels;
  struct WinevtChannel *winevtChannel;
  char *errBuf;
  char * result;
  LPWSTR buffer = NULL;
  LPWSTR temp = NULL;
  DWORD bufferSize = 0;
  DWORD bufferUsed = 0;
  DWORD status = ERROR_SUCCESS;

  RETURN_ENUMERATOR(self, 0, 0);

  TypedData_Get_Struct(self, struct WinevtChannel, &rb_winevt_channel_type, winevtChannel);

  hChannels = EvtOpenChannelEnum(NULL, 0);

  if (hChannels) {
    winevtChannel->channels = hChannels;
  } else {
    sprintf(errBuf, "Failed to enumerate channels with %s\n", GetLastError());
    rb_raise(rb_eRuntimeError, errBuf);
  }

  while (1) {
    if (!EvtNextChannelPath(winevtChannel->channels, bufferSize, buffer, &bufferUsed)) {
      status = GetLastError();

      if (ERROR_NO_MORE_ITEMS == status) {
        break;
      } else if (ERROR_INSUFFICIENT_BUFFER == status) {
        bufferSize = bufferUsed;
        temp = (LPWSTR)realloc(buffer, bufferSize * sizeof(WCHAR));
        if (temp) {
          buffer = temp;
          temp = NULL;
          EvtNextChannelPath(winevtChannel->channels, bufferSize, buffer, &bufferUsed);
        } else {
          status = ERROR_OUTOFMEMORY;
          rb_raise(rb_eRuntimeError, "realloc failed");
        }
      } else {
        sprintf(errBuf, "EvtNextChannelPath failed with %lu.\n", status);
        rb_raise(rb_eRuntimeError, errBuf);
      }
    }

    result = wstr_to_mbstr(CP_UTF8, buffer, -1);

    rb_yield(rb_utf8_str_new_cstr(result));
  }

  return Qnil;
}