CMSWindowsUtil.cpp

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

Generated on Fri Nov 6 00:18:45 2009 for synergy-plus by  doxygen 1.4.7