|
-
October 21st, 2010, 05:23 AM
#1
string concatination in loadlibrary()
i have to add path + dll name in loadlibrary()
[code]
LPCSTR path = "CommonFilesPath" ;
HINSTANCE hLib = LoadLibrary(path + "sample.dll") ;
[\code]
where CommonFilesPath = C:\PROGRAM FILES\folder
AND THIS CONTAINS DLL (sample.dll).
when i run with above code it gives me following error:
error C2110: '+' : cannot add two pointers
how do i solve this.
i am not suppose to give the path there because it is dynamic.
plz help.
-
October 21st, 2010, 05:41 AM
#2
Re: string concatination in loadlibrary()
Code:
LPCSTR path = "CommonFilesPath" ;
LPSTR pathNew = strcat ((LPSTR)path ,"sample.dll");
HINSTANCE hLib = LoadLibraryA(pathNew) ;
This is ANSI version for UNICODE use wcscat.
-
October 21st, 2010, 06:03 AM
#3
Re: string concatination in loadlibrary()
 Originally Posted by hypheni
Code:
LPCSTR path = "CommonFilesPath" ;
LPSTR pathNew = strcat ((LPSTR)path ,"sample.dll");
HINSTANCE hLib = LoadLibraryA(pathNew) ;
I'm sorry, but this code is wrong! 
The only result of it (in ANSI debug build) is:
---------------------------
Microsoft Visual C++
---------------------------
Unhandled exception in De.exe (MSVCRTD.DLL): 0xC0000005: Access Violation.
---------------------------
OK
---------------------------
The correct way is using CString or std::string (wstring):
Code:
CString path (_T( "CommonFilesPath"));
path += _T("sample.dll");
Better way is using ::PathAppend API
Victor Nijegorodov
-
October 21st, 2010, 06:19 AM
#4
Re: string concatination in loadlibrary()
Both "strings" are actually pointers to an sequence of characters. You cannot add two pointers. Hence the error. You can do something like this:
Code:
CString path = "CommonFilesPath" ;
HINSTANCE hLib = LoadLibrary(path + "sample.dll") ;
or
Code:
LPCSTR path = "CommonFilesPath" ;
HINSTANCE hLib = LoadLibrary(path + CString("sample.dll")) ;
-
October 21st, 2010, 03:02 PM
#5
Re: string concatination in loadlibrary()
 Originally Posted by hypheni
Code:
LPCSTR path = "CommonFilesPath" ;
LPSTR pathNew = strcat ((LPSTR)path ,"sample.dll");
So where do those characters go that you're adding to the end of a string-literal? What if that string-literal in memory is like this:
Code:
CommonFilesPath0x340x20(some pointer to a variable) etc...
? Where are those extra characters going? Aren't they going to overwrite what comes after that string, causing memory corruption?
Hopefully you haven't written code like this in real applications. You cannot concatenate onto a string literal. Doing so is undefined behaviour.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|