CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    128

    Question 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.

  2. #2
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: string concatination in loadlibrary()

    Quote Originally Posted by hypheni View Post
    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

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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")) ;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string concatination in loadlibrary()

    Quote Originally Posted by hypheni View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured