VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/ldrNative-win.cpp@ 46701

最後變更 在這個檔案從46701是 46701,由 vboxsync 提交於 12 年 前

mid air collision.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 4.5 KB
 
1/* $Id: ldrNative-win.cpp 46701 2013-06-20 11:07:30Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Win32 native.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#define LOG_GROUP RTLOGGROUP_LDR
31#include <Windows.h>
32
33#include <iprt/ldr.h>
34#include "internal/iprt.h"
35
36#include <iprt/alloca.h>
37#include <iprt/assert.h>
38#include <iprt/err.h>
39#include <iprt/file.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42
43#include <iprt/once.h>
44#include <iprt/string.h>
45#include "internal/ldr.h"
46
47
48int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)
49{
50 Assert(sizeof(*phHandle) >= sizeof(HMODULE));
51 AssertReturn(fFlags == 0 || fFlags == RTLDRLOAD_FLAGS_NO_UNLOAD, VERR_INVALID_PARAMETER);
52 AssertLogRelMsgReturn(RTPathStartsWithRoot(pszFilename), /* Relative names will still be applied to the search path. */
53 ("pszFilename='%s'\n", pszFilename),
54 VERR_INTERNAL_ERROR_2);
55
56 /*
57 * Do we need to add an extension?
58 */
59 if (!RTPathHaveExt(pszFilename))
60 {
61 size_t cch = strlen(pszFilename);
62 char *psz = (char *)alloca(cch + sizeof(".DLL"));
63 if (!psz)
64 return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "alloca failed");
65 memcpy(psz, pszFilename, cch);
66 memcpy(psz + cch, ".DLL", sizeof(".DLL"));
67 pszFilename = psz;
68 }
69
70 /*
71 * Attempt load.
72 */
73 HMODULE hmod = LoadLibrary(pszFilename);
74 if (hmod)
75 {
76 *phHandle = (uintptr_t)hmod;
77 return VINF_SUCCESS;
78 }
79
80 /*
81 * Try figure why it failed to load.
82 */
83 DWORD dwErr = GetLastError();
84 int rc = RTErrConvertFromWin32(dwErr);
85 return RTErrInfoSetF(pErrInfo, rc, "GetLastError=%u", dwErr);
86}
87
88
89DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
90{
91 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
92 FARPROC pfn = GetProcAddress((HMODULE)pModNative->hNative, pszSymbol);
93 if (pfn)
94 {
95 *ppvValue = (void *)pfn;
96 return VINF_SUCCESS;
97 }
98 *ppvValue = NULL;
99 return RTErrConvertFromWin32(GetLastError());
100}
101
102
103DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
104{
105 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
106 if ( (pModNative->fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
107 || FreeLibrary((HMODULE)pModNative->hNative))
108 {
109 pModNative->hNative = (uintptr_t)INVALID_HANDLE_VALUE;
110 return VINF_SUCCESS;
111 }
112 return RTErrConvertFromWin32(GetLastError());
113}
114
115
116int rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)
117{
118 /*
119 * We only try the System32 directory.
120 */
121 WCHAR wszSysDir[MAX_PATH];
122 UINT cwcSysDir = GetSystemDirectoryW(wszSysDir, MAX_PATH);
123 if (cwcSysDir >= MAX_PATH)
124 return VERR_FILENAME_TOO_LONG;
125
126 char szPath[RTPATH_MAX];
127 char *pszPath = szPath;
128 int rc = RTUtf16ToUtf8Ex(wszSysDir, RTSTR_MAX, &pszPath, sizeof(szPath), NULL);
129 if (RT_SUCCESS(rc))
130 {
131 rc = RTPathAppend(szPath, sizeof(szPath), pszFilename);
132 if (pszExt && RT_SUCCESS(rc))
133 rc = RTStrCat(szPath, sizeof(szPath), pszExt);
134 if (RT_SUCCESS(rc))
135 {
136 if (RTFileExists(szPath))
137 rc = RTLdrLoadEx(szPath, phLdrMod, fFlags, NULL);
138 else
139 rc = VERR_MODULE_NOT_FOUND;
140 }
141 }
142
143 return rc;
144}
145
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette