Jump to content
  • Advertisement
Sign in to follow this  
C plus noob

I NEED HELP BAD!!!! (DLLs)

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok im learning how to create and use DLL's it runse but doesnt import anything from my DLL :S this is my Main dll cpp file (main.cpp to create the DLL)
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#pragma warning( disable : -Wno-deprecated )



DllClass::DllClass()
{

}


DllClass::~DllClass()
{

}

int DllClass::Add(int a, int b)
{
    return a + b;
}

int DllClass::Sub(int a, int b)
{
    return a - b;
}

void DllFunc1(char* a)
{
    cout << a << endl;
}


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;
}

this is dll.h used by the DLL and the APP
#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 */

using namespace std;

extern "C" DLLIMPORT void DllFunc1(char*);

class DLLIMPORT DllClass
{
  public:
    DllClass();
    virtual ~DllClass(void);
    int Add(int, int);
    int Sub(int, int);

  private:

};


#endif /* _DLL_H_ */

and this is main.cpp for the app thats SUPPOSED to get the functions
#include "dll.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>

int main()
{
    int a, b;
    
    DllClass FromDll;
    
    a = FromDll.Add(2, 2);
    b = FromDll.Sub(2, 2);
    
    cout << "Add 2 + 2 =" << a << "!" << endl;
    cout << "Subtract 2-2 =" << b << "!" << endl;
    getch();
    
    DllFunc1("LOOK!");
    getch();
    
    system("PAUSE");
    
    return 0;
}

i am using Dev Cpp and i DID link the .a lib for the dll i made sure i put it in my linker settings PLEASE HELP!

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what you are doing wrong, but all that code works fine for me. I just made a DevCpp .dll project and built your dll and compiled. I then made a new console project in the same directory as the .a file, and pasted in your main.cpp code. I linked in the libProject1.a file (just the name of the library) and compiled. It worked fine for me.

So redo things.

1. Open up DevCPP and make a new DLL project.
2. Copy and paste in the code you have posted in each respective file and save.
3. Compile to build the dll/a file
4. Create a new console project in the same directory as your dll/a project.
5. Paste in your code you have posted here to the main.cpp file
6. Link in the libProject1.a though the Project->Project Options->Parameter->Add Library or Object button
7. Compile and run, there is not reason it should not work.

Share this post


Link to post
Share on other sites

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLEXPORT __declspec (dllexport) // <-- HERE!!!
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

using namespace std;

extern "C" DLLIMPORT void DllFunc1(char*);

class DLLIMPORT DllClass
{
public:
DllClass();
virtual ~DllClass(void);
int Add(int, int);
int Sub(int, int);

private:

};


#endif /* _DLL_H_ */

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!