001/* 002 * Copyright (C) 2009-2019 the original author(s). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fusesource.jansi; 017 018import java.io.UnsupportedEncodingException; 019 020import static org.fusesource.jansi.internal.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM; 021import static org.fusesource.jansi.internal.Kernel32.FormatMessageW; 022import static org.fusesource.jansi.internal.Kernel32.GetLastError; 023 024public class WindowsSupport { 025 026 public static String getLastErrorMessage() { 027 int errorCode = GetLastError(); 028 return getErrorMessage(errorCode); 029 } 030 031 public static String getErrorMessage(int errorCode) { 032 int bufferSize = 160; 033 byte data[] = new byte[bufferSize]; 034 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null); 035 try { 036 return new String(data, "UTF-16LE").trim(); 037 } catch (UnsupportedEncodingException e) { 038 throw new IllegalStateException(e); 039 } 040 } 041 042}