Path: blob/master/src/java.base/windows/native/libjava/jni_util_md.c
41119 views
/*1* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <string.h>27#include <windows.h>28#include <locale.h>2930#include "jni.h"31#include "jni_util.h"3233void* getProcessHandle() {34return (void*)GetModuleHandle(NULL);35}3637/*38* Windows symbols can be simple like JNI_OnLoad or __stdcall format39* like _JNI_OnLoad@8. We need to handle both.40*/41void buildJniFunctionName(const char *sym, const char *cname,42char *jniEntryName) {43if (cname != NULL) {44char *p = strrchr(sym, '@');45if (p != NULL && p != sym) {46// sym == _JNI_OnLoad@847strncpy(jniEntryName, sym, (p - sym));48jniEntryName[(p-sym)] = '\0';49// jniEntryName == _JNI_OnLoad50strcat(jniEntryName, "_");51strcat(jniEntryName, cname);52strcat(jniEntryName, p);53//jniEntryName == _JNI_OnLoad_cname@854} else {55strcpy(jniEntryName, sym);56strcat(jniEntryName, "_");57strcat(jniEntryName, cname);58}59} else {60strcpy(jniEntryName, sym);61}62return;63}6465JNIEXPORT size_t JNICALL66getLastErrorString(char *buf, size_t len) {6768DWORD errval;6970if ((errval = GetLastError()) != 0) {71// DOS error72size_t n = (size_t)FormatMessage(73FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,74NULL,75errval,760,77buf,78(DWORD)len,79NULL);80if (n > 3) {81// Drop final '.', CR, LF82if (buf[n - 1] == '\n') n--;83if (buf[n - 1] == '\r') n--;84if (buf[n - 1] == '.') n--;85buf[n] = '\0';86}87return n;88}8990// C runtime error that has no corresponding DOS error code91if (errno == 0 || len < 1) return 0;92return strerror_s(buf, len, errno);93}9495JNIEXPORT int JNICALL96getErrorString(int err, char *buf, size_t len)97{98int ret = 0;99if (err == 0 || len < 1) return 0;100ret = strerror_s(buf, len, err);101return ret;102}103104105