Patchwork [1,of,2,V2] exewrapper: add .dll to LoadLibrary() argument

mail settings
Submitter Gregory Szorc
Date April 27, 2016, 4:46 p.m.
Message ID <5b7f85ccc5851871997f.1461775618@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/14806/
State Superseded
Headers show


Gregory Szorc - April 27, 2016, 4:46 p.m.
# HG changeset patch
# User Gregory Szorc <>
# Date 1461774219 25200
#      Wed Apr 27 09:23:39 2016 -0700
# Branch stable
# Node ID 5b7f85ccc5851871997f0383331f2982c6db1f43
# Parent  87d4a6c5567e81386b8c2209d95060d5bf72e064
exewrapper: add .dll to LoadLibrary() argument

LoadLibrary() changes behavior depending on whether the argument
passed to it contains a period. From the MSDN docs:

If no file name extension is specified in the lpFileName parameter,
the default library extension .dll is appended. However, the file name
string can include a trailing point character (.) to indicate that the
module name has no extension. When no path is specified, the function
searches for loaded modules whose base name matches the base name of
the module to be loaded. If the name matches, the load succeeds.
Otherwise, the function searches for the file.

As the subsequent patch will show, some environments on Windows
define their Python library as e.g. "libpython2.7.dll." The existing
code would pass "libpython2.7" into LoadLibrary(). It would assume
"7" was the file extension and look for a "libpython2.dll" to load.

By passing ".dll" into LoadLibrary(), we force it to search for the
exact basename we want, even if it contains a period.


diff --git a/mercurial/exewrapper.c b/mercurial/exewrapper.c
--- a/mercurial/exewrapper.c
+++ b/mercurial/exewrapper.c
@@ -91,17 +91,17 @@  int main(int argc, char *argv[])
 		/* check for private Python of HackableMercurial */
 		strcat_s(pyhome, sizeof(pyhome), "\\hg-python");
 		hfind = FindFirstFile(pyhome, &fdata);
 		if (hfind != INVALID_HANDLE_VALUE) {
 			/* path pyhome exists, let's use it */
 			strcpy_s(pydllfile, sizeof(pydllfile), pyhome);
-			strcat_s(pydllfile, sizeof(pydllfile), "\\" HGPYTHONLIB);
+			strcat_s(pydllfile, sizeof(pydllfile), "\\" HGPYTHONLIB ".dll");
 			pydll = LoadLibrary(pydllfile);
 			if (pydll == NULL) {
 				err = "failed to load private Python DLL "
 				      HGPYTHONLIB ".dll";
 				goto bail;
 			Py_SetPythonHome = (void*)GetProcAddress(pydll,
@@ -109,17 +109,17 @@  int main(int argc, char *argv[])
 				err = "failed to get Py_SetPythonHome";
 				goto bail;
 	if (pydll == NULL) {
-		pydll = LoadLibrary(HGPYTHONLIB);
+		pydll = LoadLibrary(HGPYTHONLIB ".dll");
 		if (pydll == NULL) {
 			err = "failed to load Python DLL " HGPYTHONLIB ".dll";
 			goto bail;
 	Py_Main = (void*)GetProcAddress(pydll, "Py_Main");
 	if (Py_Main == NULL) {