Thread: LoadLibrary with variable path

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    13

    LoadLibrary with variable path

    hey guys.

    I have the following problem.

    I am using

    Code:
    hinstlib = LoadLibrary(L"D:\\FOLDER\\Function.dll");
    and

    Code:
    CopyFileA("D:\\Folder\\Parameters.txt","Parameters.txt",0);
    to call a dll from a certain directory and to copy a corresponding parameter file which works quite good.

    The problem now is:

    The directory D:\Folder might change from one computer to another and/or it is not possible to create it.

    so what i want to do is to change the specified directory to a variable.
    The program from which I call the dll comes with an ascii file, in which the directory could be stored. The user could write the path into it where the dll and parameters and txt file is stored on his machine.

    so at the end it should look as follows:

    Code:
    "open Ascii file"
    "read Directory (maybe as a string)"
    copy the string into a char variable
    and finally 
    change 
    hinstlib = LoadLibrary(L"D:\\FOLDER\\Function.dll");
    to 
    hinstlib = LoadLibrary(L"STRING\\Function.dll");
    What I tried so far does not seem to work at all - maybe you can help me out
    Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hack way:

    Use a char array, and use strcpy / strcat to build the filename. Use that array in LoadLibrary() call.

    Smart way:
    Use a std::string foo; and build your string.
    Then use LoadLibrary(foo.c_str()); to load the library.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If using a path, stick to absolute paths for LoadLibrary. The manual discusses how relative paths are treated:
    LoadLibrary Function (Windows)
    Dynamic-Link Library Search Order (Windows)

    gg

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Why do you mix ansi with wide? In case you want to do it in 'smart way' and use LoadLibraryW, you need std::wstring for wide strings.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    I get the string

    Code:
     D:\FOLDER\
    from the ASCII file using

    Code:
    FILE * pFile;								
    char datastring [100];
    pFile = fopen ("asciifile.txt" , "r");
    if (pFile == NULL) perror ("error");  //error message if open doesn't work
    {
    fgets (datastring , 100 , pFile);
    puts (datastring); 	//Output of the string for testing purpose
    fclose (pFile);
    }
    which works quite well.

    But I'm not sure how the syntax of the LoadLibrary Call has to be...

    BTW: does it work to only use the path in the string and not the file in addition?

    As you know, planned is a combiation of path (string) and filename.

    like
    Code:
     ...LoadLibrary(STRING "dllName.dll");
    Does the c-syntax with double-backslash has to be in the string?

    Thanks for replies!

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    BTW: does it work to only use the path in the string and not the file in addition?
    You want only the path? Without filename? How do you imagine it working?

    Read string - C++ Reference and try to build your own path.

    Code:
    ...
    std::string path(datastring);
    path.append("\\dll_name.dll");
    LoadLibrary(path.c_str());
    ...
    I highly doubt you will manage to make use of your DLL, you have no basic knowledge.

    EDIT:
    Read about string concatenation, you cannot concatenate two strings, writing them side by side, like:
    datastring "\\lib.dll"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fgets (datastring , 100 , pFile);
    Remember to erase the newline at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by DampfHans View Post
    ...
    Code:
    FILE * pFile;								
    char datastring [100];
    pFile = fopen ("asciifile.txt" , "r");
    if (pFile == NULL) perror ("error");  //error message if open doesn't work
    {
    fgets (datastring , 100 , pFile);
    puts (datastring); 	//Output of the string for testing purpose
    fclose (pFile);
    }
    ...
    Why, why WHY are you using C?
    ...When you obviously posted on the C++ board.

    Quote Originally Posted by kmdv View Post
    You want only the path? Without filename? How do you imagine it working?

    Read string - C++ Reference and try to build your own path.

    Code:
    ...
    std::string path(datastring);
    path.append("\\dll_name.dll");
    LoadLibrary(path.c_str());
    ...
    I highly doubt you will manage to make use of your DLL, you have no basic knowledge.

    EDIT:
    Read about string concatenation, you cannot concatenate two strings, writing them side by side, like:
    datastring "\\lib.dll"
    Or how about a more natural way:
    Code:
    ...
    std::string path(datastring);
    path += "\\dll_name.dll";
    LoadLibrary(path.c_str());
    ...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  3. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-23-2008, 11:47 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. filestream- can a path name be a variable
    By stevew2607 in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2002, 04:02 PM