Skip to content

Commit

Permalink
src,deps: replace LoadLibrary by LoadLibraryW
Browse files Browse the repository at this point in the history
On Windows, when compiling with `UNICODE` defined, `LoadLibrary` becomes
`LoadLibraryW`. When an ASCII string is passed to that function it
crashes.

PR-URL: #226
Reviewed-By: Bert Belder <[email protected]>
  • Loading branch information
zcbenz authored and piscisaureus committed Jan 7, 2015
1 parent cbf76c1 commit 604b876
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions deps/cares/src/ares_library_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static int ares_win32_init(void)
#ifdef USE_WINSOCK

hnd_iphlpapi = 0;
hnd_iphlpapi = LoadLibrary("iphlpapi.dll");
hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
if (!hnd_iphlpapi)
return ARES_ELOADIPHLPAPI;

Expand Down Expand Up @@ -73,7 +73,7 @@ static int ares_win32_init(void)
*/

hnd_advapi32 = 0;
hnd_advapi32 = LoadLibrary("advapi32.dll");
hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
if (hnd_advapi32)
{
ares_fpSystemFunction036 = (fpSystemFunction036_t)
Expand Down
2 changes: 1 addition & 1 deletion src/node_win32_etw_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void NTAPI etw_events_enable_callback(
void init_etw() {
events_enabled = 0;

advapi = LoadLibrary("advapi32.dll");
advapi = LoadLibraryW(L"advapi32.dll");
if (advapi) {
event_register = (EventRegisterFunc)
GetProcAddress(advapi, "EventRegister");
Expand Down
2 changes: 1 addition & 1 deletion src/node_win32_perfctr_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void InitPerfCountersWin32() {
wcscpy_s(Inst, INST_MAX_LEN, INST_PREFIX);
_itow_s(pid, Inst + INST_PREFIX_LEN, INST_MAX_LEN - INST_PREFIX_LEN, 10);

advapimod = LoadLibrary("advapi32.dll");
advapimod = LoadLibraryW(L"advapi32.dll");
if (advapimod) {
perfctr_startProvider = (PerfStartProviderExFunc)
GetProcAddress(advapimod, "PerfStartProviderEx");
Expand Down

3 comments on commit 604b876

@kant2002
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better used

 LoadLibrary(_T("advapi32.dll"));

since this code will work with Unicode and non-Unicode builds in natural ways.

@piscisaureus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO NO NO
Read the pull request. For your convenience only I've added a link in the commit message.

@kant2002
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read explanation. I understand why app is crashing when UNICODE is defined. Maybe you have some very specific needs which here is not described, but I don't get why my solution is not working.
I refer to MS docs and what seem conventional wisdom for me. See http://msdn.microsoft.com/en-us/library/dybsewaf.aspx

Maybe you miss _T macro which expand string to different literals based on presence of UNICODE define.

So basically when UNICODE not defined you will have

LoadLibraryA( "advapi32.dll" );

With UNICODE defined you will have

LoadLibraryW( L"advapi32.dll" );

Your solution should crash on 32-bit Windows I believe.
Again this is so regular stuff, so maybe I miss something very obvious to you, but not visible to me?

Please sign in to comment.