944,191 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4256
  • C++ RSS
Jun 23rd, 2007
0

passing in ascii/unicode strings to LoadLibrary

Expand Post »
hi,

i have this code:


HINSTANCE hComponent = ::LoadLibrary("Component1.dll");


i get this compile error:

Error 1 error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [15]' to 'LPCWSTR' c:\Documents and Settings\Administrator\Desktop\Component1\Client1\Client1.cpp 9

so i changed the code:


HINSTANCE hComponent = ::LoadLibrary((LPCWSTR)"Component1.dll");


however GetLastError returns ERROR_MODULE_NOT_FOUND. the dll IS in the exe directory though, so its not the location that is the problem. in addition, if i call LoadLibraryA, the function loads the library successfully.

so why is LoadLibrary((LPCWSTR)"Component1.dll"); failing?

thanks!
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
unclepauly is offline Offline
42 posts
since Dec 2006
Jun 23rd, 2007
0

Re: passing in ascii/unicode strings to LoadLibrary

Use the conversion macros (for ASCII/UNICODE compatibility), not indiscriminate casting to make the compiler STFU.


::LoadLibrary( TEXT("Component1.dll") );


Just because you managed to silence your compiler is in no way a measure of run-time success.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 23rd, 2007
0

Re: passing in ascii/unicode strings to LoadLibrary

thats great thank you.
Reputation Points: 16
Solved Threads: 0
Light Poster
unclepauly is offline Offline
42 posts
since Dec 2006
Sep 29th, 2010
0
Re: passing in ascii/unicode strings to LoadLibrary
Hi, It's better to make it more dynamic. For example you have a code like that:
  1. void loadMyDllFile( const char * filename )
  2. {
  3. ::LoadLibrary( filename ) //it's not working....
  4. }

You have an option to change your compiler mode from UNICODE to MULTIBYTE.
Just go to the project property in visual studio and change the value of Character Set from "use Unicode character set" to "use Multi-Byte character set".

if the compiler gives you an error again, it means that you are using a header file that actually modify the LoadLibrary. To fix that issue just use LoadLibraryA() instead.

Hope it helps,
memphis
Reputation Points: 10
Solved Threads: 0
Newbie Poster
memphis.ray is offline Offline
2 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programming Problem
Next Thread in C++ Forum Timeline: overloading operator +





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC