00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CMSWindowsUtil.h"
00016 #include "CStringUtil.h"
00017 #include <stdio.h>
00018
00019
00020
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
00031 int n = LoadString(instance, id, msg, size);
00032 msg[n] = '\0';
00033 if (n < size) {
00034 return msg;
00035 }
00036
00037
00038
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 }