954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

LoadLibrary() and getcwd()

I am using Visual C++ 2003,
Previously i am using this to call test.dll stored in the root directory folder name root1 and in the sub folder root2

HINSTANCE result = NULL;
result = LoadLibrary(L"..\\root1\\root2\\test.dll");

It work fine in normal OS. When i run in Win XP regional language setting to japanese, it can't work
Then i test with this and it work!

result = LoadLibrary(L"C:\\debug\\root1\\root2\\test.dll");

so i come out with an idea to use the getcwd() which return the current directory path.

char* buffer = _getcwd( NULL, 0);
strcat(buffer," <a href="file://\\root1\\root2\\test.dll">\\root1\\root2\\test.dll</a> ")
result = LoadLibrary((LPTSTR)buffer);

it is not working. I found the issue is:
buffer contain : "C:\debug\root1\root2\test.dll"
instead i think it will working find if:
buffer contain :"C:\\debug\\root1\\root2\\test.dll"
Any suggestion on my prob?? Your help is very needed, Thanks in advance!

patrick772goh
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

> it is not working.
two reasons:
a. strcat(buffer,"\\root1\\root2\\test.dll") ; causes a buffer overflow
b. LoadLibrary((LPTSTR)buffer) ; casting is not the way to convert narrow char strings to wide char strings.

wchar_t wbuffer[ MAX_PATH ] ;
wchar_t* result = _wgetcwd( wbuffer, MAX_PATH ) ;
assert( result ) ;
wcscat( wbuffer, L"\\root1\\root2\\test.dll" ) ;
result = LoadLibrary( wbuffer ) ;
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

you can not use strcat() on the pointer returned by getcwd () because _getcwd() allocates a buffer only large enough to hold the path. You need to allocate a second buffer that is large enough to hold the string returned by _getcwd() and the string you need to add.

The second problem with the solution you have is that depending on the current directory the final string may contain the wrong path too if _getcwd() returns "c:\debug\root1\root2" then your strcat() will result in "c:\debug\root1\root2\root1\root2\test.dll". Your program needs to verify the string that was returned by _getcwd() to make sure you don't strcat() duplicate text.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I had a problem where I could not get LoadLibrary to work from a variable.
TEXT("...") worked, but I could not pass a variable to TEXT().

After quite a bit of hacking I noticed that when I hovered over LoadLibrary in MSVS, it displayed LoadLibraryW. Odd.

I changed my LoadLibrary call to LoadLibraryA() (ASCII version) and it could
work with LPCSTR formats...

char *DLLnam;
...
LPCSTR nam;
nam = DLLnam;
dll = LoadLibraryA(nam);

Mr.What
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You