MongoDB  2.7.0
winutil.h
1 // @file winutil.cpp : Windows related utility functions
2 //
3 // /**
4 // * Copyright (C) 2008 10gen Inc.
5 // *
6 // * This program is free software: you can redistribute it and/or modify
7 // * it under the terms of the GNU Affero General Public License, version 3,
8 // * as published by the Free Software Foundation.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU Affero General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU Affero General Public License
16 // * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // *
18 // * As a special exception, the copyright holders give permission to link the
19 // * code of portions of this program with the OpenSSL library under certain
20 // * conditions as described in each individual source file and distribute
21 // * linked combinations including the program with the OpenSSL library. You
22 // * must comply with the GNU Affero General Public License in all respects
23 // * for all of the code used other than as permitted herein. If you modify
24 // * file(s) with this exception, you may extend this exception to your
25 // * version of the file(s), but you are not obligated to do so. If you do not
26 // * wish to do so, delete this exception statement from your version. If you
27 // * delete this exception statement from all source files in the program,
28 // * then also delete it in the license file.
29 // */
30 //
31 // #include "mongo/pch.h"
32 
33 #pragma once
34 
35 #if defined(_WIN32)
36 #include <windows.h>
37 #include "text.h"
38 
39 namespace mongo {
40 
41  inline string GetWinErrMsg(DWORD err) {
42  LPTSTR errMsg;
43  ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, (LPTSTR)&errMsg, 0, NULL );
44  std::string errMsgStr = toUtf8String( errMsg );
45  ::LocalFree( errMsg );
46  // FormatMessage() appends a newline to the end of error messages, we trim it because endl flushes the buffer.
47  errMsgStr = errMsgStr.erase( errMsgStr.length() - 2 );
48  std::ostringstream output;
49  output << errMsgStr << " (" << err << ")";
50 
51  return output.str();
52  }
53 }
54 
55 #endif
56