• Main Page
  • Classes
  • Files
  • File List

CMSWindowsUtil.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CMSWindowsUtil.h"
00020 #include "CStringUtil.h"
00021 #include <stdio.h>
00022 
00023 //
00024 // CMSWindowsUtil
00025 //
00026 
00027 CString
00028 CMSWindowsUtil::getString(HINSTANCE instance, DWORD id)
00029 {
00030     char buffer[1024];
00031     int size = static_cast<int>(sizeof(buffer) / sizeof(buffer[0]));
00032     char* msg = buffer;
00033 
00034     // load string
00035     int n = LoadString(instance, id, msg, size);
00036     msg[n] = '\0';
00037     if (n < size) {
00038         return msg;
00039     }
00040 
00041     // not enough buffer space.  keep trying larger buffers until
00042     // we get the whole string.
00043     msg = NULL;
00044     do {
00045         size <<= 1;
00046         delete[] msg;
00047         char* msg = new char[size];
00048         n = LoadString(instance, id, msg, size);
00049     } while (n == size);
00050     msg[n] = '\0';
00051 
00052     CString result(msg);
00053     delete[] msg;
00054     return result;
00055 }
00056 
00057 CString
00058 CMSWindowsUtil::getErrorString(HINSTANCE hinstance, DWORD error, DWORD id)
00059 {
00060     char* buffer;
00061     if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00062                                 FORMAT_MESSAGE_IGNORE_INSERTS |
00063                                 FORMAT_MESSAGE_FROM_SYSTEM,
00064                                 0,
00065                                 error,
00066                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00067                                 (LPTSTR)&buffer,
00068                                 0,
00069                                 NULL) == 0) {
00070         CString errorString = CStringUtil::print("%d", error);
00071         return CStringUtil::format(getString(hinstance, id).c_str(),
00072                             errorString.c_str());
00073     }
00074     else {
00075         CString result(buffer);
00076         LocalFree(buffer);
00077         return result;
00078     }
00079 }

Generated on Thu Jun 20 2013 00:00:05 for Synergy by  doxygen 1.7.1