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,"\\root1\\root2\\test.dll")
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!

Recommended Answers

All 3 Replies

> 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 ) ;

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.

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);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.