I NEED HELP BAD!!!! (DLLs)

Started by
4 comments, last by programering 18 years, 7 months ago
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!
Advertisement
Maybe #define than # define in your BUILDING_ALL-Block.
Are you sure that BUILDING_ALL is defined?


Regards,

Michael
done but i still cant get it to work :S
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.
ill trty it brb
#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_ */

This topic is closed to new replies.

Advertisement