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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Detours Simple sample applications dll export problem

Detours Simple sample applications dll export problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpcomjsonannouncement
5 Posts 2 Posters 2 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.
  • K Offline
    K Offline
    keret
    wrote on last edited by
    #1

    I'm new to C++. I try to build the Simple application in Microsoft's detours project in VS08. Detours is used to inject dlls into API processes or into applications. You can find it at: http://research.microsoft.com/en-us/projects/detours/ I managed to build: detoured.dll detours.lib Simple.dll (That's the hook) setdll.exe (The program that injects the hook) sleep5.exe (The exe that uses the hooked API) When I try to run the hook: setdll /d:simple.dll I get the error message: Error: simple.dll does not export function with ordinal #1. It's because Simple.dll doesn't export anything. Detoured.dll does export the function detoured. detoured.h:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Presence of this DLL (detoured.dll) marks a process as detoured.
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //

    #ifdef DETOURED_EXPORTS
    #define DETOURED_API __declspec(dllexport)
    #else
    #define DETOURED_API __declspec(dllimport)
    #endif

    HMODULE DETOURED_API WINAPI Detoured();

    //
    ///////////////////////////////////////////////////////////////// End of File.

    detoured.cpp:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Presence of this DLL (detoured.dll) marks a process as detoured.
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //

    #include #include "detoured.h"

    static HMODULE s_hDll;

    HMODULE WINAPI Detoured()
    {
    return s_hDll;
    }

    BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
    {
    (void)reserved;

    if (dwReason == DLL\_PROCESS\_ATTACH) {
        s\_hDll = hinst;
        DisableThreadLibraryCalls(hinst);
    }
    return TRUE;
    

    }

    //
    ///////////////////////////////////////////////////////////////// End of File.

    But simple.cpp is just this in the package:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Detours Test Program (simple.cpp of simple.dll)
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //
    // This DLL will detour the Windows Sleep API so that TimedSleep function
    // gets called instead. TimedSleep records the before and after times, and
    // calls the real Sleep API through the TrueSleep function pointer.

    _ 1 Reply Last reply
    0
    • K keret

      I'm new to C++. I try to build the Simple application in Microsoft's detours project in VS08. Detours is used to inject dlls into API processes or into applications. You can find it at: http://research.microsoft.com/en-us/projects/detours/ I managed to build: detoured.dll detours.lib Simple.dll (That's the hook) setdll.exe (The program that injects the hook) sleep5.exe (The exe that uses the hooked API) When I try to run the hook: setdll /d:simple.dll I get the error message: Error: simple.dll does not export function with ordinal #1. It's because Simple.dll doesn't export anything. Detoured.dll does export the function detoured. detoured.h:

      //////////////////////////////////////////////////////////////////////////////
      //
      // Presence of this DLL (detoured.dll) marks a process as detoured.
      //
      // Microsoft Research Detours Package, Version 2.1.
      //
      // Copyright (c) Microsoft Corporation. All rights reserved.
      //

      #ifdef DETOURED_EXPORTS
      #define DETOURED_API __declspec(dllexport)
      #else
      #define DETOURED_API __declspec(dllimport)
      #endif

      HMODULE DETOURED_API WINAPI Detoured();

      //
      ///////////////////////////////////////////////////////////////// End of File.

      detoured.cpp:

      //////////////////////////////////////////////////////////////////////////////
      //
      // Presence of this DLL (detoured.dll) marks a process as detoured.
      //
      // Microsoft Research Detours Package, Version 2.1.
      //
      // Copyright (c) Microsoft Corporation. All rights reserved.
      //

      #include #include "detoured.h"

      static HMODULE s_hDll;

      HMODULE WINAPI Detoured()
      {
      return s_hDll;
      }

      BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
      {
      (void)reserved;

      if (dwReason == DLL\_PROCESS\_ATTACH) {
          s\_hDll = hinst;
          DisableThreadLibraryCalls(hinst);
      }
      return TRUE;
      

      }

      //
      ///////////////////////////////////////////////////////////////// End of File.

      But simple.cpp is just this in the package:

      //////////////////////////////////////////////////////////////////////////////
      //
      // Detours Test Program (simple.cpp of simple.dll)
      //
      // Microsoft Research Detours Package, Version 2.1.
      //
      // Copyright (c) Microsoft Corporation. All rights reserved.
      //
      // This DLL will detour the Windows Sleep API so that TimedSleep function
      // gets called instead. TimedSleep records the before and after times, and
      // calls the real Sleep API through the TrueSleep function pointer.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Put the line

      #define SIMPLE_EXPORTS

      before the line

      #include "simple.h"

      «_Superman_»

      K 1 Reply Last reply
      0
      • _ _Superman_

        Put the line

        #define SIMPLE_EXPORTS

        before the line

        #include "simple.h"

        «_Superman_»

        K Offline
        K Offline
        keret
        wrote on last edited by
        #3

        Thanks, that worked. But I still don't know, why detoured.dll exported without "#define DETOURED_EXPORTS".

        _ 1 Reply Last reply
        0
        • K keret

          Thanks, that worked. But I still don't know, why detoured.dll exported without "#define DETOURED_EXPORTS".

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          The pre-processor definitions can also be given as a project settings instead of putting it inside the code file. Check the PreprocessorDefinitions key inside .vcproj file. It could be there.

          «_Superman_»

          K 1 Reply Last reply
          0
          • _ _Superman_

            The pre-processor definitions can also be given as a project settings instead of putting it inside the code file. Check the PreprocessorDefinitions key inside .vcproj file. It could be there.

            «_Superman_»

            K Offline
            K Offline
            keret
            wrote on last edited by
            #5

            Bingo! I copied it from an other project and I forgot about it. Man, it's so complicated!

            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