CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Aug 2002
    Posts
    756

    DLL link error 2001, when accessing function from main module

    Hi, ALL,
    Here is my problem.
    I have an application with the CMainFrame class and the function Foo() in it, that described as follows:

    Code:
    class CMainFrame : public CMDIFrameWnd
    {
    public:
    .................................
           void Foo();
    }
    I also have a DLL that have another class with the function that calls the Foo() function. During DLL linking I am receiving the LNK2001 error.
    I looked at MSDN and Codeguru. Here I found the following link <a href=http://www.codeguru.com/dll/ExportingFromExe.html> Calling an exported function in an EXE from within DLL</a>. But I don't think this is my solution, since the article is talking about external exported function.

    Thank you in advance.

  2. #2
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    LNK2001 means unresolved external.

    You are using code and linking but linker does not see import library (.LIB file).

    You have to include lib file for proper linking.

    That has nothing to do with exporting function from exe module. While it is possible, it really is not practical.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Aug 2002
    Posts
    756
    Hi, JohnCz,
    Thank you for the responce.
    What kind of .LIB file could be generated with .EXE file?
    Once again, I am trying to call function from the main .EXE module inside .DLL.

    Thank you.
    P.S.: Maybe all I need to do is somehow export the CMainFrm::Foo() function from the main .EXE module, so it will be visible for DLL?
    Last edited by OneEyeMan; January 23rd, 2004 at 12:25 AM.

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    You are calling function in a dll therefore there is nothing you have to expose from EXE module. It is rather the other way around.

    I do not know what kind of DLL you are using, but compiling DLL there is import library with DLL name and .LIB extension. That is what you need to include in your project dependencies.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Aug 2002
    Posts
    756
    Hi, JohnCz,
    One more time: I am receiving the LNK2001 when compiling and linking the .DLL project not the main .EXE one, which means that I should put the .LIB file that will be generated from .EXE to the .DLL. But I don't know is it possible to generate the .LIB file out of main .EXE module.

    Thank you.
    P.S.:
    I do not know what kind of DLL you are using, but compiling DLL there is import library with DLL name and .LIB extension
    I couldn't compile the .DLL project. You solution is for other way around.
    Last edited by OneEyeMan; January 23rd, 2004 at 04:26 PM.

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    export the function __declspec(dllexport) and you will produce a lib for the .exe.

  7. #7
    Join Date
    Aug 2002
    Posts
    756
    Hi, Mick,
    So it is possible to generate .LIB for the .EXE module. Thank you.
    As for the syntax. Will this construction be OK?
    Code:
    class CMainFrame : public CMDIFrameWnd
    {
    public:
    .................................
           void __declspec(dllexport) Foo();
    }

  8. #8
    Join Date
    Dec 2003
    Location
    Los Angeles
    Posts
    6
    I think you need to export the whole class (to prevent problems with the default constructor etc.)

    Code:
    __declspec(dllexport) class CMainFrame : public CMDIFrameWnd ...
    Btw, why do you need to import functions from .exe to .dll? That sounds weird...

  9. #9
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    There is an article abt loading the dlls dynamically. Check this out, if u find it useful.
    http://www.codersource.net/win32_dlls.html

    Thanks
    Muthu

  10. #10
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Elron
    I think you need to export the whole class (to prevent problems with the default constructor etc.)

    Code:
    __declspec(dllexport) class CMainFrame : public CMDIFrameWnd ...
    Btw, why do you need to import functions from .exe to .dll? That sounds weird...
    yea missed the class part even though it's in his intitial post that's what you get for jumping right to the bottom of the thread...anyways, if they are non-static member functions, then export the class.

  11. #11
    Join Date
    Aug 2002
    Posts
    756
    Thank you, guys!
    I understand that this is the safest way: just export the whole class.
    Now, one last thing:
    Macros AFX_EXT_CLASS design for extension DLL only, right? Which means I couldn't use it in my case.

  12. #12
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by OneEyeMan
    Thank you, guys!
    I understand that this is the safest way: just export the whole class.
    Now, one last thing:
    Macros AFX_EXT_CLASS design for extension DLL only, right? Which means I couldn't use it in my case.
    the AFX_EXT_CLASS macro resolves to either

    AFX_CLASS_IMPORT
    AFX_CLASS_EXPORT

    which is simply

    _declspec(dllimport)
    or
    _declspec(dllexport)

  13. #13
    Join Date
    Aug 2002
    Posts
    756
    Hi, guys,
    Thank you for the fast reply, but.....
    Neither class export, nor function export does not solve the problem. When I compile my .DLL with the .LIB file generated it gives me following:
    LNK2001: Unresolved external symbol 'public: static struct CRuntimeClass const CMainFrame::classCMainFrame"

    which actually means that I have only one option left: add the CMainFrame class in the .DLL project. I guess the 'static' stuff comes from CMDIFrame, which is parent class.
    I tried both methods, which are:
    Code:
    class __declspec(dllexport) CMainFrame : public CMDIFrame
    {
    public:
           void Foo();
    };
    
    class CMainFrame : public CMDIFrame
    {
    public:
         void __declspec(dllexport) Foo();
    };
    I guess, because of this error, I couldn't create the external exported function, which will call CMainFrame::Foo();

    Any suggestion???

  14. #14
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    You have to export and import.
    See attached sample.

    See ExpImp.h included in StdAxf in both projects.

    In VS use menu: Project Settings. In Dialog select C++ tab.

    For ExeExport project:
    Check Preprocessor Definitions, it has EXP_EXE_ defined.
    For Dll see that it does not contain EXP_EXE_.

    Change to Link tab
    See Output filename is changed to create dll in the same directory as exe module.

    Select Input for category Object/Library modules contain ExeExport.lib and Additional library path contains ..\

    See exported function and variable that holds pointer to a current instance of CMainFrame object.

    Compile both and rum exe. Use Load menu and select dll to load dll. It will call exported function. Message box is indication that function was called.
    Attached Files Attached Files
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  15. #15
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    The easiest solution would be to make Foo() a callback function and pass it's address to the DLL from the exe.

    Is the DLL an MFC extension DLL?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured