Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11a3f0c

Browse files
committed
NT specific change for nicer error message (Mark H)
1 parent 2271bf7 commit 11a3f0c

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

Python/importdl.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,38 @@ load_dynamic_module(name, pathname, fp)
336336
HINSTANCE hDLL;
337337
hDLL = LoadLibrary(pathname);
338338
if (hDLL==NULL){
339-
char errBuf[64];
340-
sprintf(errBuf, "DLL load failed with error code %d",
341-
GetLastError());
339+
char errBuf[256];
340+
unsigned int errorCode;
341+
342+
/* Get an error string from Win32 error code */
343+
char theInfo[256]; /* Pointer to error text from system */
344+
int theLength; /* Length of error text */
345+
346+
errorCode = GetLastError();
347+
348+
theLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
349+
NULL, /* message source */
350+
errorCode, /* the message (error) ID */
351+
0, /* default language environment */
352+
(LPTSTR) theInfo, /* the buffer */
353+
sizeof(theInfo), /* the buffer size */
354+
NULL); /* no additional format args. */
355+
356+
/* Problem: could not get the error message. This should not happen if called correctly. */
357+
if (theLength == 0) {
358+
sprintf(errBuf, "DLL load failed with error code %d", errorCode);
359+
} else {
360+
int len;
361+
/* For some reason a \r\n is appended to the text */
362+
if (theLength >= 2 && theInfo[theLength-2] == '\r' && theInfo[theLength-1] == '\n') {
363+
theLength -= 2;
364+
theInfo[theLength] = '\0';
365+
}
366+
strcpy(errBuf, "DLL load failed: ");
367+
len = strlen(errBuf);
368+
strncpy(errBuf+len, theInfo, sizeof(errBuf)-len);
369+
errBuf[sizeof(errBuf)-1] = '\0';
370+
}
342371
err_setstr(ImportError, errBuf);
343372
return NULL;
344373
}

0 commit comments

Comments
 (0)