Thread: pasting code form a .txt file...

  1. #76
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    How long does it take to build the dll.h file?

  2. #77
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Shouldnt take but a couple of seconds.

    Something might have went wrong somewhere.
    What is C++?

  3. #78
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    you should probably just link the lib and screw manually loading the functions altogether
    There is a reason we are doing it this way. Check out the first page of this thread.

  4. #79
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    well I got the probeblem and fixed it...now I have anoutehr probeblem. Why won;t it rebuild!!??!!?? I can't compile it only rebuild and that just never ends. The program is working... but no dll.

  5. #80
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Do you know how you would load a class? I though maybe you could create a pointer and assign it to the classes address in the dll but couldnt figure out how.

    [edit]
    RUNE I would start over, delete all of your files that have to do with this dll and start over.

    [/edit]
    What is C++?

  6. #81
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    lol I did. nd this is what happened. I got the 3 files, dll.h, dll.cpp, and the main .cpp file. And well I;ll keep trying stuff...

  7. #82
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Sometimes, when your trying to figure out stuff your file and your code will get all cluttered and you will become confused and nothing will make sense.

    So here, A little step by step refresher...

    • New Project
      - DLL
      - - Modify the dll.h and dll.cpp
      - - Hit Compile

      New Project
      - Console App
      - - Write the code to load the DLL and use if statements to catch errors.

      Let us know what happens
    What is C++?

  8. #83
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do you know how you would load a class? I though maybe you could create a pointer and assign it to the classes address in the dll but couldnt figure out how.
    Check out this article:
    http://codeguru.earthweb.com/win32/dyndllclass.shtml

  9. #84
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    but I don't want to use classes. I want to use functions...

  10. #85
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    but I don't want to use classes. I want to use functions...
    We realize that. His question was just for his own curiousity.

  11. #86
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok I ddi what you said...

    So here, A little step by step refresher...


    New Project
    - DLL
    - - Modify the dll.h and dll.cpp
    - - Hit Compile

    New Project
    - Console App
    - - Write the code to load the DLL and use if statements to catch errors.

    Let us know what happens

    I did it all and NOW the dll.h and dll.cpp files compile just FINE! But now (I used a sorce file instead) and it compiles but then it sits there, I go to the log, and it says compling please wait.. or something and then says the directory it is "compliling" to... It stays tehre for ever. then if I hit cancle I get 433 errors. and at the end of the error lists it says something like "There must some horibly wrong wiht your code, plese fix it".

    Now what?

    here si my code...

    dll.h...

    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    
    DLLIMPORT void test();
    
    #endif /* _DLL_H_ */

    dll.cpp...

    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    #include <windows.h>
    #include <iostream.h>
    
    void test()
    {
        cout<<"It worked!!!!";
    }    
    
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }

    mainfile.cpp...

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include "project1.dll"
    int main()
    {
        test();     
            
        getch();
        return 0;    
        
    }

    Alright now lets get some things traight right away!...

    No matter what I put in the mainfile.cpp it will give that error. I tried many many things but notin worked. So please don't say notin there I only have that becuase that is what I tried last.

    Thanks alot still, your really helping me alot! (says in a non sarcastic way)
    Last edited by Rune Hunter; 09-10-2004 at 03:08 PM.

  12. #87
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Ok, I'm downloading the dev-cpp compiler right now. Once I've finished, I will try to give you step by step instructions.

  13. #88
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    changes are bold
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    
    extern "C" DLLIMPORT void test();
    
    #endif /* _DLL_H_ */
    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    #include <iostream>
    
    DLLIMPORT void test()
    {
        std::cout<<"It worked!!!!";
    }    
    
    // The message function thingie is not needed
    Now you cannot do a #include "dll.dll"

    It wont work.

    Create a function pointer like in my main.cpp example up there.

    [edit]
    Code:
    #include <windows.h>
    #include <iostream>
    
    int main()
    {
        
        // Function Pointer
        void(*TextFunc)();
        
        // Load DLL 
        HMODULE hMod = LoadLibrary("TestPlug.dll");
        
        // Make sure its loaded
        if(!hMod) std::cout << "Error Loading DLL";
        
        // If it is..
        else {
            
            // Assign the function pointer to the 
            // address of the function in the dll
            TextFunc = (void(*)())GetProcAddress(hMod, "test");
            
            // If it found it
            if(TextFunc) TextFunc();
                
            // If not
            else std::cout << "Error Finding Function";    
            
        }      
            
        std::cin.get();
        return 0;    
        
    }
    Oh, and dont put
    Code:
    #define BUILDING_DLL
    in your code.

    DevCpp automatically defines it via compiler linker option. If you were to put it in your code, you would never be able to use it "correctly"
    Last edited by Vicious; 09-10-2004 at 03:24 PM.
    What is C++?

  14. #89
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I got it...

  15. #90
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Did it work? Finally!?
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM