pasting code form a .txt file...

This is a discussion on pasting code form a .txt file... within the C++ Programming forums, part of the General Programming Boards category; It mentioned that any applications referencing the DLL would have to be rebuilt if .DEF files were not used though....

  1. #46
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It mentioned that any applications referencing the DLL would have to be rebuilt if .DEF files were not used though.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #47
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    guess my posts are importent for more then just me. But!... what I mean by not work I mean it doesn't work. It doesn't compile there are alot o errors. It just doesn't work.

  3. #48
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    post what errors you are getting, and I will try to help you through it.

  4. #49
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok now here is the code that doesn't work if anyone gets confused...


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    int main()
    {
        void(*ptestfn)();
        HMODULE hMod = LoadLibrary("dll.dll");
        if(!hMod)
            return 0;  // failed to load dll
        // we need to get the address of the test() function that is
        // inside our dll
        ptestfn = (void (*)())GetProcAddress(hMod,"test");
        if(ptestfn)
            ptestfn();  // call function
        
    }

    Now here is a opy of the error window...


    Ok... I just found out there are NO errors and it DOES compile...


    But!!!, It doesn't work. Now the thing is when I have a .dll file thing being made and before I compile it askes for a host file. I can only select a .exe already made and without selecting one I can't compile it. So that is my first probeblem. And when I do compile it using a already made .exe host file it only shows black with the DOS curser thing in the DOS window and blinks and does notin no matter what. And I can't close the program through the program only clicking the X. So any help now?

  5. #50
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I am assuming that you do have a function called test in your dll?
    What is C++?

  6. #51
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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 */
    
    
    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>
    #include <conio.h>
    
    void test()
    {
        char player[100];
            
        cout<<"Player: ";
        cin>>player;
        cout<<"\n";
    
        
        if(!strcmpi("hello", player))
        {
            cout<<"hi";
            cout<<"\n\n";
        }
        getch();
    }
    Here is my main .cpp file again...

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    int main()
    {
        void(*ptestfn)();
        HMODULE hMod = LoadLibrary("dll.dll");
        if(!hMod)
            return 0;  // failed to load dll
        // we need to get the address of the test() function that is
        // inside our dll
        ptestfn = (void (*)())GetProcAddress(hMod,"test");
        if(ptestfn)
            ptestfn();  // call function
        
    }
    Last edited by Rune Hunter; 09-09-2004 at 05:13 PM.

  7. #52
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Change your code to this so that we can see if there are any problems loading the dll or finding the function:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    int main()
    {
        void(*ptestfn)();
        HMODULE hMod = LoadLibrary("dll.dll");
        if(!hMod)
    	{
    		printf("Error loading dll\n");
            return 0;  // failed to load dll
    	}
        // we need to get the address of the test() function that is
        // inside our dll
        ptestfn = (void (*)())GetProcAddress(hMod,"test");
        if(ptestfn)
            ptestfn();  // call function
    	else
    		printf("Unable to find function\n");
        
    }

  8. #53
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    still need a host file and when I do it is only blank like I said before...

  9. #54
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    ok, I see your problem. You need to export the test function. Change the dll code to:

    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #define BUILDING_DLL
    
    #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_ */
    Also add DLLIMPORT to the beginning of the test() function definition.

  10. #55
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Two problems I can think of. One is that test() isn't declared as DLLIMPORT, and therefore isn't DLL-import-able. Second, it's not declared as extern "C", so the name gets mangled up and you won't be able to load it using GetProcAddress() as simply "test"... it'll be "ta1@$!e$$s23t" or something, which you need to find somehow.

    **EDIT**
    Now I see why the tutorial used extern "C" in all its examples... it's so that they would work with the LoadLibrary method. Because if you have overloaded functions and they have the same name, then you're in deep doodoo when you try loading "test" and there's 3 of them with different implementations.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #56
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Ok, Im not sure why it's asking for a host file. If all you are doing is building the DLL (Not debugging it), then you shouldnt need any sort of host file.

  12. #57
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol... what IS a host file? Hah! **vanishes to google**

    **EDIT**
    "creating dll compile host file" results... "Trojan virus" "Virus alert" "How to prevent hacker access" "securing your system from invasion" "the end of the world is near" "don't press the button!" "AHH THE NUKES ARE FLYING!!" "NOO THE ROBOTS ARE COMING!!!"
    Last edited by Hunter2; 09-09-2004 at 05:23 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #58
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Im not sure what dev-cpp is calling a host file, but I can make a guess
    Debugging a DLL cant be done like a normal exe file. You need an exe file to load the dll in order to debug it. Im guessing the host file is just an exe file which loads the dll, and therefore can be used for debugging. Like I said before though, I've never used dev-cpp, so it's just a guess

  14. #59
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sounds like a good guess to me. Maybe turning off debug options will get rid of the host file prompt...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #60
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Go to

    Tools > Compiler Options > Settings > Linker > Generate Debugging Information ( NO )
    What is C++?

Page 4 of 7 FirstFirst 1234567 LastLast
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-22-2008, 12:19 AM
  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, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21