Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function pointers from DLL [modified]

Function pointers from DLL [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++game-devtutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Siroen
    wrote on last edited by
    #1

    I was busy creating a Dll for some basic game engine (just for myself) For the input, I would like to have some general function pointers inside my dll which I want to be able to reference to other functions from code based upon my exe file. just like this: on_a = function_to_do_on_A; on_esc = function_to_do_on_Esc; where on_a comes from the dll and the function_to_do_on_A comes from the application. this is my code: Dll: .h #define ENGINE_EXPORTS #ifdef ENGINE_EXPORTS #define ENGINE_API __declspec(dllexport) #else #define ENGINE_API __declspec(dllimport) #endif .cpp namespace Engine { namespace Input { ENGINE_API void (*on_a)(void); ENGINE_API void (*on_b)(void); ENGINE_API void (*on_c)(void); } } Exe: .cpp void FuncA(void) { MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK); return; } int main() { Engine::Input::on_a = &FuncA; return 0; } errors on_a is not a member of Engine::Input on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Exe: .cpp int main() { { using namespace Engine::Input; on_a = &FuncA; } return 0; } errors on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- .h namespace Engine { namespace Input { extern ENGINE_API void(*on_a)(void); } } error LNK2001: unresolved external symbol "void (__cdecl* Engine::Input::on_a)(void)" (?on_a@Input@Engine@@3P6AXXZA) What am I doing wrong? Why is my function pointer not being exported? And how to fix this problem? Many thanks in advance. ps. I am using VS2008

    modified on Sunday, April 27, 2008 4:19 PM

    M O 3 Replies Last reply
    0
    • M Michael Siroen

      I was busy creating a Dll for some basic game engine (just for myself) For the input, I would like to have some general function pointers inside my dll which I want to be able to reference to other functions from code based upon my exe file. just like this: on_a = function_to_do_on_A; on_esc = function_to_do_on_Esc; where on_a comes from the dll and the function_to_do_on_A comes from the application. this is my code: Dll: .h #define ENGINE_EXPORTS #ifdef ENGINE_EXPORTS #define ENGINE_API __declspec(dllexport) #else #define ENGINE_API __declspec(dllimport) #endif .cpp namespace Engine { namespace Input { ENGINE_API void (*on_a)(void); ENGINE_API void (*on_b)(void); ENGINE_API void (*on_c)(void); } } Exe: .cpp void FuncA(void) { MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK); return; } int main() { Engine::Input::on_a = &FuncA; return 0; } errors on_a is not a member of Engine::Input on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Exe: .cpp int main() { { using namespace Engine::Input; on_a = &FuncA; } return 0; } errors on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- .h namespace Engine { namespace Input { extern ENGINE_API void(*on_a)(void); } } error LNK2001: unresolved external symbol "void (__cdecl* Engine::Input::on_a)(void)" (?on_a@Input@Engine@@3P6AXXZA) What am I doing wrong? Why is my function pointer not being exported? And how to fix this problem? Many thanks in advance. ps. I am using VS2008

      modified on Sunday, April 27, 2008 4:19 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Are you linking the exe to the import library (.lib) created by the DLL build? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        Are you linking the exe to the import library (.lib) created by the DLL build? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        Michael Siroen
        wrote on last edited by
        #3

        yes as follows.. #include "..\Engine\Engine.h" #if defined(DEBUG) | defined(_DEBUG) #define USE_DEBUG_ENGINE #endif #ifdef USE_DEBUG_ENGINE #pragma comment(lib, "..\\Debug\\Engine.lib") #else #pragma comment(lib, "..\\Release\\Engine.lib") #endif normal functions (and classes) declared within the namespace are working properly for instance: int Height = Engine::GetHeight(); returns the proper value

        1 Reply Last reply
        0
        • M Michael Siroen

          I was busy creating a Dll for some basic game engine (just for myself) For the input, I would like to have some general function pointers inside my dll which I want to be able to reference to other functions from code based upon my exe file. just like this: on_a = function_to_do_on_A; on_esc = function_to_do_on_Esc; where on_a comes from the dll and the function_to_do_on_A comes from the application. this is my code: Dll: .h #define ENGINE_EXPORTS #ifdef ENGINE_EXPORTS #define ENGINE_API __declspec(dllexport) #else #define ENGINE_API __declspec(dllimport) #endif .cpp namespace Engine { namespace Input { ENGINE_API void (*on_a)(void); ENGINE_API void (*on_b)(void); ENGINE_API void (*on_c)(void); } } Exe: .cpp void FuncA(void) { MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK); return; } int main() { Engine::Input::on_a = &FuncA; return 0; } errors on_a is not a member of Engine::Input on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Exe: .cpp int main() { { using namespace Engine::Input; on_a = &FuncA; } return 0; } errors on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- .h namespace Engine { namespace Input { extern ENGINE_API void(*on_a)(void); } } error LNK2001: unresolved external symbol "void (__cdecl* Engine::Input::on_a)(void)" (?on_a@Input@Engine@@3P6AXXZA) What am I doing wrong? Why is my function pointer not being exported? And how to fix this problem? Many thanks in advance. ps. I am using VS2008

          modified on Sunday, April 27, 2008 4:19 PM

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          The first version of your EXE code works for me as long as that last header file code is included in the EXE cpp file.

          Exe:
          .cpp

          namespace Engine
          {
          namespace Input
          {
          __declspec(dllimport) void (*on_a)(void);
          }
          }

          void FuncA(void)
          {
          MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK);
          return;
          }

          int main()
          {
          Engine::Input::on_a = &FuncA;
          return 0;
          }

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M 1 Reply Last reply
          0
          • M Michael Siroen

            I was busy creating a Dll for some basic game engine (just for myself) For the input, I would like to have some general function pointers inside my dll which I want to be able to reference to other functions from code based upon my exe file. just like this: on_a = function_to_do_on_A; on_esc = function_to_do_on_Esc; where on_a comes from the dll and the function_to_do_on_A comes from the application. this is my code: Dll: .h #define ENGINE_EXPORTS #ifdef ENGINE_EXPORTS #define ENGINE_API __declspec(dllexport) #else #define ENGINE_API __declspec(dllimport) #endif .cpp namespace Engine { namespace Input { ENGINE_API void (*on_a)(void); ENGINE_API void (*on_b)(void); ENGINE_API void (*on_c)(void); } } Exe: .cpp void FuncA(void) { MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK); return; } int main() { Engine::Input::on_a = &FuncA; return 0; } errors on_a is not a member of Engine::Input on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Exe: .cpp int main() { { using namespace Engine::Input; on_a = &FuncA; } return 0; } errors on_a undeclared identifier -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- .h namespace Engine { namespace Input { extern ENGINE_API void(*on_a)(void); } } error LNK2001: unresolved external symbol "void (__cdecl* Engine::Input::on_a)(void)" (?on_a@Input@Engine@@3P6AXXZA) What am I doing wrong? Why is my function pointer not being exported? And how to fix this problem? Many thanks in advance. ps. I am using VS2008

            modified on Sunday, April 27, 2008 4:19 PM

            O Offline
            O Offline
            Ozer Karaagac
            wrote on last edited by
            #5

            DllFile.h :

            #ifdef ENGINE_EXPORTS
            #define ENGINE_API __declspec(dllexport)
            #else
            #define ENGINE_API __declspec(dllimport)
            #endif
            
            namespace Engine
            {
              namespace Input
              {
                ENGINE_API void (*on_a)(void);
                ENGINE_API void (*on_b)(void);
                ENGINE_API void (*on_c)(void);
              }
            }
            

            DllFile.cpp :

            #define ENGINE_EXPORTS
            #include "DllFile.h"
            

            ExeFile.cpp :

            #include "DllFile.h"
            
            int main()
            {
              Engine::Input::on_a = &FuncA;
              return 0;
            }
            
            M 1 Reply Last reply
            0
            • M Mark Salsbery

              The first version of your EXE code works for me as long as that last header file code is included in the EXE cpp file.

              Exe:
              .cpp

              namespace Engine
              {
              namespace Input
              {
              __declspec(dllimport) void (*on_a)(void);
              }
              }

              void FuncA(void)
              {
              MessageBox(NULL, L"FuncA called!", L"NOTIFICATION!", MB_OK);
              return;
              }

              int main()
              {
              Engine::Input::on_a = &FuncA;
              return 0;
              }

              Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              M Offline
              M Offline
              Michael Siroen
              wrote on last edited by
              #6

              The namespace Engine and it's 'member'- namespace Input are within the Dll file.. This means that the declaration is within the Dll file: ENGINE_API void (*on_a)(void); if I put the following code above the main function of mine: namespace Engine { namespace Input { __declspec(dllimport) void (*on_a)(void); }} I get the following warning: warning C4273: 'Engine::Input::on_a' : inconsistent dll linkage followed up with an error: error C3861: 'on_a': identifier not found

              1 Reply Last reply
              0
              • O Ozer Karaagac

                DllFile.h :

                #ifdef ENGINE_EXPORTS
                #define ENGINE_API __declspec(dllexport)
                #else
                #define ENGINE_API __declspec(dllimport)
                #endif
                
                namespace Engine
                {
                  namespace Input
                  {
                    ENGINE_API void (*on_a)(void);
                    ENGINE_API void (*on_b)(void);
                    ENGINE_API void (*on_c)(void);
                  }
                }
                

                DllFile.cpp :

                #define ENGINE_EXPORTS
                #include "DllFile.h"
                

                ExeFile.cpp :

                #include "DllFile.h"
                
                int main()
                {
                  Engine::Input::on_a = &FuncA;
                  return 0;
                }
                
                M Offline
                M Offline
                Michael Siroen
                wrote on last edited by
                #7

                thanks, that already helped a lot.. (the DllCpp was the problem, #define over there instead above the header..) Since multiple header files all include this one (Engine.h), I get several (7) Errors Error LNK2005: "void (__cdecl* Engine::Input::on_a)(void)" (?on_a@Input@Engine@@3P6AXXZA) already defined in Engine.obj LuaScriptor.obj But I think I can get around that problem, or just by simply putting everything inside a single header and source file. Thanks to all for the support and the quick help :) EDIT: Fixed all (Copy everything to Engine.cpp and Engine.h), thanks

                modified on Sunday, April 27, 2008 6:10 PM

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups