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