I did make my dll file out off those two files

dll.h

#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 welcome();

class DLLIMPORT DllClass
{
  public:
    DllClass();
    virtual ~DllClass(void);

  private:

};


#endif /* _DLL_H_ */

and dllmain.cpp

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include<iostream>
using namespace std;
DllClass::DllClass()
{

}


DllClass::~DllClass ()
{

}

DLLIMPORT void welcome()
{
  cout << " Welcome home " << 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;
}

now I want to call welcome() function in this DLL from my test_main.cpp

main.cpp

#include <cstdlib>
#include <iostream>
#include<project3.dll> // <<---------- the dll file include 
 
using namespace std;

int main(int argc, char *argv[])
{
    welcome();           // <<------ this function in the DLL
    system("PAUSE");
    return EXIT_SUCCESS;
}

but I got those errors

3 \including dll\main.cpp project3.dll: No such file or directory. 
9 \including dll\main.cpp `welcome' undeclared (first use this function)

I have project.dll in the directory

And when I made the dll file I didn't have anyfile *.lib

I only have a file libProject3.def I tried to include it but it won't work either

any advice please ?

Thanks

Dani AI

Generated

Good call by and nice follow-up by — include the DLL header in the EXE project and link an import library, do not #include the .dll file itself. For future readers: there are two ways to call DLL functions on Windows — implicit linking (compile/link against an import library, .lib for MSVC or .a for MinGW) or explicit loading at run time with LoadLibrary / GetProcAddress. The import library supplies the linker with the symbol stubs; the actual .dll must be present at run time.

If you only have a .def or a .dll and no import lib: with Visual Studio use lib.exe to create a .lib from the .def (for example: lib /def:libProject3.def /out:project3.lib /machine:X86 — use X64 for 64-bit). With MinGW an .a import lib is normal; link it directly or use -lproject3. To confirm what the DLL exports, use dumpbin /exports project3.dll (MSVC) or objdump -p project3.dll (MinGW tooling).

A minimal explicit-load example (no import lib required):

typedef void (*PFN_WELCOME)();
HMODULE h = LoadLibraryA("project3.dll");
if (h) {
    PFN_WELCOME welcome = (PFN_WELCOME)GetProcAddress(h, "welcome");
    if (welcome) welcome();
    FreeLibrary(h);
}

Common pitfalls: C++ name mangling (use extern "C" for C-style exports if you want stable names), mismatched calling conventions (__cdecl vs __stdcall), exporting C++ classes across module boundaries (fragile unless same compiler/settings), and CRT/runtime mismatches (debug vs release or static vs dynamic CRT can cause crashes when using iostreams or heap across DLL boundary). Quick checklist: include the header, link the correct import lib for your toolchain, verify exports, match calling convention and runtime settings.

Recommended Answers

All 2 Replies

include the dll.h header, and not the project3.dll file.

Make an import library - the .lib file, should be an option in your compiler/linker

link the .lib file into your project. The program will know how to access the .dll file from the details stored in the .lib file.

include the dll.h header, and not the project3.dll file.

Make an import library - the .lib file, should be an option in your compiler/linker

link the .lib file into your project. The program will know how to access the .dll file from the details stored in the .lib file.

Thanks man I found out that the lib file was with extinction .a and I just linked in the project options as you said

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.