Thread: How to import DLL function from a separate C file

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    How to import DLL function from a separate C file

    Hi there,

    I am trying to create a function in a separate .C file (not the main.c) that imports the DLL functions. Below is what I have, but it doesn't compile.

    //importDLL.c
    Code:
    #include "importDLL.h"
    #include <windows.h>
    
    void importDLL()
    {
        typedef int (*importFunction)();
        importFunction impFcn;
        HINSTANCE hinstLib = LoadLibrary("someDLL.dll");
        impFcn = (importFunction)GetProcAddress(hinstLib, "impFcn");
    }


    //importDLL.h
    Code:
    void importDLL(void);
    The problem is in main.c it doesn't recognize the imported DLL function "impFcn". How can I fix it?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wait. Is this C, or C++? Also, what does "does not compile" mean? What are the errors?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Quote Originally Posted by Elysia View Post
    Wait. Is this C, or C++? Also, what does "does not compile" mean? What are the errors?
    It's for both C and C++. When it compiles, the main function doesn't recognize the imported DLL function.
    Last edited by high123_98; 11-10-2011 at 03:31 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Does it compile or doesn't it?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There must be a function with the non-mangled name impFcn in the dll you are loading or GetProcAddress() will return null. You have not given us any of the DLL code so there is no way we can tell you what is wrong.

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by high123_98 View Post
    It's for both C and C++. When it compiles, the main function doesn't recognize the imported DLL function.
    Declare all of your DLL imports as "extern C". C++ functions are by default subject to name-mangling (which necessarily precludes class members from being used as such entry points, of course). Also, be explicit about your calling convention (lest you break your ABI elsewhere, too)...
    Last edited by gardhr; 11-10-2011 at 09:23 PM.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You variable impFcn is local to your function. You need to be able to access it from main, so it needs to be global or better yet the return value of your import function.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You variable impFcn is local to your function.
    Wow I cannot believe I did not see that but you are absolutely right. impFcn will go out of scope at the end of the function. Most of use were concentrating on what the function was doing instead of seeing the obvious problem of scope. Nice catch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with using a Function from a separate file.
    By RelentlessWiz in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2011, 03:33 PM
  2. System import but can't find function - stumped please help
    By hpteenagewizkid in forum C# Programming
    Replies: 3
    Last Post: 05-15-2007, 05:40 AM
  3. put vars in separate function, but need reference
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 03-06-2003, 09:22 PM
  4. Replies: 7
    Last Post: 10-15-2002, 10:55 PM
  5. calling function from separate cpp file
    By dontknow in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2002, 10:42 AM