Class: Winevt::EventLog::Channel

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

Instance Method Summary collapse

Constructor Details

#initializeObject



113
114
115
116
117
# File 'ext/winevt/winevt.c', line 113

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

Instance Method Details

#eachObject



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

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;
  ULONG len;

  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);
      }
    }

    len = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
    if (!(result = malloc(len))) return "";
    WideCharToMultiByte(CP_UTF8, 0, buffer, -1, result, len, NULL, NULL);

    rb_yield(rb_str_new2(result));
  }

  return Qnil;
}