CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jan 2004
    Posts
    30

    Help with LoadLibrary

    Hi gurus!
    I'm using VC++ 7.0 and I created a console project:
    Code:
    #include "stdafx.h"
    #include "windows.h"
    int _tmain(int argc, _TCHAR* argv[])
    {      HMODULE hMod=LoadLibrary("mylib.dll");         //error 
    
             return 0;
    }
    The error is: 'error C2065: '' : undeclared identifier'.
    I dunno why. Help me!

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    First make sure the project compiles okay with the call to LoadLibrary().

    What OS is it?

    Kuphryn

  3. #3
    Join Date
    Jun 2003
    Posts
    22
    code seems ok...
    First try cutting HMODULE hMod=LoadLibrary("mylib.dll"); line into :

    HMODULE hMod;
    hMod=LoadLibrary("mylib.dll");

    And see if the problem comes from LoadLibrary or from hmodule like that.

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    This is a long shot.... The line

    #include "stdafx.h"

    indicates that this is an MFC app. MFC apps aren't supposed to #include "windows.h" explicitly. I can't remember why though.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    This is a long shot.... The line

    #include "stdafx.h"

    indicates that this is an MFC app. MFC apps aren't supposed to #include "windows.h" explicitly. I can't remember why though.
    seems that these files contain some #ifdef ... #undef stuff that cause the problem.

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    stdafx.h is just the name of the precompiled header that is auto generated from the wizard. What it contains depends on teh project type you choose. LoadLibrary(...) is a pretty standard function that is #defined pretty early on in the includes.

    You don't #include windows because it is already #included when you include various <afxXXX> headers.


    Are you sure it's LoadLibrary(...) and not _TCHAR that it is complaining about?

    At anyrate...forward declare it,depending if your using unicode (which you should be doing if your NT->) or what not. And find out what is wrong with your headers, because something is mucked up.

    Code:
    WINBASEAPI
    HMODULE
    WINAPI
    LoadLibraryExA(
        IN LPCSTR lpLibFileName,
        IN HANDLE hFile,
        IN DWORD dwFlags
        );
    WINBASEAPI
    HMODULE
    WINAPI
    LoadLibraryExW(
        IN LPCWSTR lpLibFileName,
        IN HANDLE hFile,
        IN DWORD dwFlags
        );

  7. #7
    Join Date
    Jan 2004
    Posts
    30
    Hi, thanks for your helping. I'm using WinXP Pro. I've tried what you suggested:
    Code:
    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {      HMODULE hMod;
            hMod=LoadLibrary("mylib.dll");          //error 
    
             return 0;
    }
    There're still errors. One is: error C2065: '' : undeclared identifier'. and one is error C2440: '=' : cannot convert from ''unknown-type'' to 'HMODULE'
    And here is all of my "stdafx.h":
    Code:
    #pragma once
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    Help me pliz!
    Last edited by tdc; April 25th, 2004 at 04:07 PM.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    1) Return type should ne HINSTANCE, but HMODULE

    2) You do not seem to have the correct header [winbase.h]. As a temp test, put the following line in your file.
    HINSTANCE LoadLibrary(
    LPCTSTR lpLibFileName // address of filename of executable module
    );
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jan 2004
    Posts
    30
    I changed HMODULE -> HINSTANCE, but the error is error C2440: '=' : cannot convert from ''unknown-type'' to 'HINSTANCE'

    Anyway, when I added a file into my project:
    Code:
    //LoadLib.cpp
    #include "stdafx.h"
    unsigned MyLoadLib()
    {	HMODULE h= LoadLibrary("mylib.dll");
    	return 1;
    }
    everything's OK! I dunno what's going on...
    Last edited by tdc; April 25th, 2004 at 04:13 PM.

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    You've repeatedly told us about the undeclared identifier but you still haven't said which symbol is unidentified. What is the entire error that you're getting?

    Also, is this an MFC application or not? If so, why does it have a 'main(...)' function? If not, why are you including stdafx.h?

    At the moment, we can only guess at this problem because you're keeping too much information to yourself. You'll have to be a bit more forthcoming if you want to be helped. Sorry...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by John E

    Also, is this an MFC application or not? If so, why does it have a 'main(...)' function? If not, why are you including stdafx.h?
    stdafx.h is just the name of the wizard auto generated pre-compiler header, it has nothing to do with MFC.

  12. #12
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835
    Originally posted by Mick
    stdafx.h is just the name of the wizard auto generated pre-compiler header, it has nothing to do with MFC.
    Are you sure about that Mick? I always thought that Afx is MFC's implementation of the standard Windows components?? Stdafx.h normally #includes the MFC core component headers, as well as MFC support for common controls etc.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  13. #13
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by John E
    Afx is MFC's implementation of the standard Windows components isn't it?? Stdafx.h normally #includes the MFC core component headers, as well as MFC support for common controls etc.
    What gets placed in the stdafx depends on what type of project you create through the wizard...it is just a precompiled header file name.

  14. #14
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383
    The entry-point function (like main()) should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary), because this can result in a DLL being used after the system has executed its termination code.

  15. #15
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    The fact that the resolution was 'I just added a file and it now works' leads me to believe the whole problem revolved around the pch files, in which the OP either.

    1. Including something before the precomplied headers, in this case #include <stdafx.h> such as:

    #include <windows.h>
    #include <stdafx.h>

    With no <windows.h> included in the <stdafx.h> though we are shown that, that is apparently not the case.

    Or

    The precompiled headers got into some fubar state.

    Mr. Tomaszek: I'm not sure where you brought the DLL comment into the issue, as we are not dealing with a DLL.

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