Thread: Calling App Functions from DLL

  1. #1
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186

    Calling App Functions from DLL

    I've created a DLL, exported the proper functions, and everything seems to be working fine; the main application can look up the exported function with GetProcAddress(), call them, and nothing unexpected happens. Now I'm looking into having the DLL call some of the application's functions, and I'm running into trouble. The code I've attached illustrates the problem.

    When the main application (MainApp) starts, it immediately loads the MainAppDLL with LoadLibrary(). This calls the DLL's DllMain(), which senses that the application is loading the module into its space and attempts to call an application-side function. Since the main application is already technically loaded into the process space, the DLL uses GetModuleHandle(NULL) to get a handle to the main application's instance for use in GetProcAddress() rather than LoadLibrary(). However, GetProcAddress() always returns NULL and GetLastError() reports that the procedure hasn't been found.

    I can see why this is; opening the MainApp executable in a hex editor, I wasn't able to find the standard (function name)@(number) text anywhere in the file as I would see in a DLL even though the function is exported in MainApp's code. Is __declspec(dllexport) meaningless when used in an executable? Is there a way to get a pointer to a function in an application so that a DLL may call it?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Hmm..it seems to work ok for me....I compiled your code and it called the Hello Function fine

  3. #3
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Strange. Which compiler are you using? Are there any extra options that may be in use other than generating an executable for MainApp and a DLL for the DLL code? Does "Hello@" appear anywhere within the generated executable?
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I use VC++6...

    "Hello" appears as a func in the exe when I look at it under dependancy viewer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Calling Question
    By mercury529 in forum Windows Programming
    Replies: 11
    Last Post: 01-09-2007, 06:15 PM
  2. Calling a class member through a pointer to a class in a DLL
    By cboard_member in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 10:55 AM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. COM DLL Functions in C
    By CoolDudeMan in forum Windows Programming
    Replies: 4
    Last Post: 06-02-2005, 09:53 AM
  5. Callback functions from dll
    By Calthun in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2004, 04:13 PM