Changeset e4d3f23 in nscp for include


Ignore:
Timestamp:
06/23/07 10:44:30 (6 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
e6e297e
Parents:
6c5375b
Message:

Fixed problem with missing \0 on ns-client streams (but I am doubtfull that it will "work" better it will simply not crash any longer, nsclient has to be \n terminated...

Location:
include
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • include/EnumNtSrv.cpp

    r6817602 re4d3f23  
    177177} 
    178178 
    179 #define SC_BUF_LEN 1024 
     179#define SC_BUF_LEN 4096 
    180180TNtServiceInfo TNtServiceInfo::GetService(std::string name) 
    181181{ 
     
    189189  if (!sh) { 
    190190    DWORD bufLen = SC_BUF_LEN; 
    191     TCHAR *buf = new TCHAR[bufLen]; 
    192     if (!GetServiceKeyName(scman, name.c_str(), buf, &bufLen)) { 
    193       throw NTServiceException(name, "Could not open Service", GetLastError()); 
     191    TCHAR *buf = new TCHAR[bufLen+1]; 
     192    std::cout << "name: '" << name << "'" << std::endl; 
     193    if (GetServiceKeyName(scman, name.c_str(), buf, &bufLen) == 0) { 
    194194      ::CloseServiceHandle(scman); 
    195     } 
     195      throw NTServiceException(name, "GetServiceKeyName: Could not translate service name", GetLastError()); 
     196    } 
     197    /* 
     198    Why does this not work? (a bug in the API? says it should return the correct size?) 
    196199    if (bufLen >= SC_BUF_LEN) { 
    197       throw NTServiceException(name, "Could not open Service", GetLastError()); 
    198200      ::CloseServiceHandle(scman); 
     201      throw NTServiceException(name, "Service name to long to handle", GetLastError()); 
    199202    } 
    200203    buf[bufLen] = 0; 
    201     SC_HANDLE sh = ::OpenService(scman,buf,SERVICE_QUERY_STATUS); 
    202     if (!sh) { 
    203       throw NTServiceException(name, "Could not open Service", GetLastError()); 
     204    */ 
     205    sh = ::OpenService(scman,buf,SERVICE_QUERY_STATUS); 
     206    if (sh == NULL) { 
    204207      ::CloseServiceHandle(scman); 
     208      throw NTServiceException(name, "OpenService: Could not open Service", GetLastError()); 
    205209    } 
    206210  } 
     
    210214    info.m_dwServiceType = state.dwServiceType; 
    211215  } else { 
    212     info.m_dwCurrentState = -1; 
    213     info.m_dwServiceType = -1; 
     216    ::CloseServiceHandle(sh); 
     217    ::CloseServiceHandle(scman); 
     218    throw NTServiceException(name, "QueryServiceStatus: Could not query service status", GetLastError()); 
    214219  } 
    215220  // TODO: Get more info here  
  • include/EnumNtSrv.h

    re26cfe0 re4d3f23  
    2525#include <list> 
    2626#include <string> 
     27#include <strEx.h> 
    2728 
    2829class TNtServiceInfo; 
     
    4445 
    4546  std::string getError() { 
    46     return "Service: " + name_ + " caused: " + msg_; 
     47    return "Service: " + name_ + " caused: " + msg_ + "(" + strEx::itos(error_) + ")"; 
    4748  } 
    4849}; 
  • include/PDHCounter.h

    rdc65e35 re4d3f23  
    326326        LPTSTR szInstanceBuffer = NULL; 
    327327        status = PdhEnumObjectItems(NULL, NULL, (*it).name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 
    328         if (status != PDH_MORE_DATA) 
    329           throw PDHException("PdhEnumObjectItems failed when trying to retrieve size for " + (*it).name, status); 
    330         szCounterBuffer = new char[dwCounterBufLen+1024]; 
    331         szInstanceBuffer = new char[dwInstanceBufLen+1024]; 
    332         status = PdhEnumObjectItems(NULL, NULL, (*it).name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 
    333         if (status != ERROR_SUCCESS) 
    334           throw PDHException("PdhEnumObjectItems failed when trying to retrieve buffer for " + (*it).name, status); 
    335  
    336         if (dwCounterBufLen > 0) { 
    337           cp=szCounterBuffer; 
    338           while(*cp != '\0') { 
    339             Counter o; 
    340             o.name = cp; 
    341             (*it).counters.push_back(o); 
    342             cp += lstrlen(cp)+1; 
     328        if (status == PDH_MORE_DATA) { 
     329          szCounterBuffer = new char[dwCounterBufLen+1024]; 
     330          szInstanceBuffer = new char[dwInstanceBufLen+1024]; 
     331          status = PdhEnumObjectItems(NULL, NULL, (*it).name.c_str(), szCounterBuffer, &dwCounterBufLen, szInstanceBuffer, &dwInstanceBufLen, dwDetailLevel, 0); 
     332          if (status != ERROR_SUCCESS) 
     333            throw PDHException("PdhEnumObjectItems failed when trying to retrieve buffer for " + (*it).name, status); 
     334 
     335          if (dwCounterBufLen > 0) { 
     336            cp=szCounterBuffer; 
     337            while(*cp != '\0') { 
     338              Counter o; 
     339              o.name = cp; 
     340              (*it).counters.push_back(o); 
     341              cp += lstrlen(cp)+1; 
     342            } 
    343343          } 
     344          if (dwInstanceBufLen > 0) { 
     345            cp=szInstanceBuffer; 
     346            while(*cp != '\0') { 
     347              Instance o; 
     348              o.name = cp; 
     349              (*it).instances.push_back(o); 
     350              cp += lstrlen(cp)+1; 
     351            } 
     352          } 
     353          delete [] szCounterBuffer; 
     354          delete [] szInstanceBuffer; 
     355          //throw PDHException("PdhEnumObjectItems failed when trying to retrieve size for " + (*it).name, status); 
    344356        } 
    345         if (dwInstanceBufLen > 0) { 
    346           cp=szInstanceBuffer; 
    347           while(*cp != '\0') { 
    348             Instance o; 
    349             o.name = cp; 
    350             (*it).instances.push_back(o); 
    351             cp += lstrlen(cp)+1; 
    352           } 
    353         } 
    354         delete [] szCounterBuffer; 
    355         delete [] szInstanceBuffer; 
    356357      } 
    357358      return ret; 
  • include/Socket.h

    rdc65e35 re4d3f23  
    4747    } 
    4848    DataBuffer(const DataBuffer &other) { 
    49       buffer_ = new char[other.getLength()]; 
    50       memcpy(buffer_, other.getBuffer(), other.getLength()); 
     49      buffer_ = new char[other.getLength()+2]; 
     50      memcpy(buffer_, other.getBuffer(), other.getLength()+1); 
    5151      length_ = other.getLength(); 
    5252    } 
     
    5454      delete [] buffer_; 
    5555      length_ = 0; 
     56      buffer_ = NULL; 
    5657    } 
    5758    void append(const char* buffer, const unsigned int length) { 
    58       char *tBuf = new char[length_+length+1]; 
     59      char *tBuf = new char[length_+length+2]; 
    5960      memcpy(tBuf, buffer_, length_); 
    6061      memcpy(&tBuf[length_], buffer, length); 
     
    6263      buffer_ = tBuf; 
    6364      length_ += length; 
    64     } 
     65      buffer_[length_+1] = 0; 
     66    } 
     67    /** 
     68     * returns a const reference to the internal buffer 
     69     * Use with care! 
     70     * 
     71     * @access public  
     72     * @returns const char * 
     73     * @qualifier const 
     74     */ 
    6575    const char * getBuffer() const { 
    6676      return buffer_; 
     
    6979      return length_; 
    7080    } 
     81    /** 
     82     * Eats a specified number of bytes from the beginning of the buffer 
     83     * @access public  
     84     * @returns void 
     85     * @qualifier 
     86     * @param const unsigned int length the amount of bytes to eat 
     87     */ 
    7188    void nibble(const unsigned int length) { 
    7289      if (length > length_) 
    7390        return; 
    7491      unsigned int newLen = length_-length; 
    75       char *tBuf = new char[newLen+1]; 
    76       memcpy(tBuf, &buffer_[length], newLen); 
     92      char *tBuf = new char[newLen+2]; 
     93      memcpy(tBuf, &buffer_[length], newLen+1); 
    7794      char *oldBuf = buffer_; 
    7895      buffer_ = tBuf; 
     
    87104      ret.copyFrom(buffer_, length); 
    88105      unsigned int newLen = length_-length; 
    89       char *tBuf = new char[newLen+1]; 
    90       memcpy(tBuf, &buffer_[length], newLen); 
     106      char *tBuf = new char[newLen+2]; 
     107      memcpy(tBuf, &buffer_[length], newLen+1); 
    91108      char *oldBuf = buffer_; 
    92109      buffer_ = tBuf; 
     
    107124    void copyFrom(const char* buffer, const unsigned int length) { 
    108125      delete [] buffer_; 
    109       buffer_ = new char[length+1]; 
     126      buffer_ = new char[length+2]; 
    110127      memcpy(buffer_, buffer, length); 
    111128      length_ = length; 
     129      buffer_[length_+1] = 0; 
    112130    } 
    113131  }; 
Note: See TracChangeset for help on using the changeset viewer.