Click here to Skip to main content
15,921,905 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Debug Assertion : Project exe cannot be found or does not match. Pin
David Crow29-Jan-09 2:51
David Crow29-Jan-09 2:51 
QuestionChanging Background color and Static Control background color. Pin
Le@rner29-Jan-09 1:50
Le@rner29-Jan-09 1:50 
AnswerRe: Changing Background color and Static Control background color. Pin
ATM@CodeProject29-Jan-09 1:57
ATM@CodeProject29-Jan-09 1:57 
AnswerRe: Changing Background color and Static Control background color. Pin
Code-o-mat29-Jan-09 2:02
Code-o-mat29-Jan-09 2:02 
GeneralRe: Changing Background color and Static Control background color. Pin
Le@rner29-Jan-09 17:37
Le@rner29-Jan-09 17:37 
QuestionMerging the two video files Pin
raj157629-Jan-09 1:28
raj157629-Jan-09 1:28 
AnswerRe: Merging the two video files Pin
Code-o-mat29-Jan-09 2:09
Code-o-mat29-Jan-09 2:09 
QuestionDetours Simple sample applications dll export problem Pin
keret29-Jan-09 1:24
keret29-Jan-09 1:24 
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 <windows.h>
#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.
</windows.h>


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.
//
#include <stdio.h>
#include <windows.h>
#include "detours.h"

static LONG dwSlept = 0;
static VOID (WINAPI * TrueSleep)(DWORD dwMilliseconds) = Sleep;

VOID WINAPI TimedSleep(DWORD dwMilliseconds)
{
    DWORD dwBeg = GetTickCount();
    TrueSleep(dwMilliseconds);
    DWORD dwEnd = GetTickCount();

    InterlockedExchangeAdd(&dwSlept, dwEnd - dwBeg);
}

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

    if (dwReason == DLL_PROCESS_ATTACH) {
        printf("simple.dll: Starting.\n");
        fflush(stdout);

        DetourRestoreAfterWith();

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)TrueSleep, TimedSleep);
        error = DetourTransactionCommit();

        if (error == NO_ERROR) {
            printf("simple.dll: Detoured Sleep().\n");
        }
        else {
            printf("simple.dll: Error detouring Sleep(): %d\n", error);
        }
    }
    else if (dwReason == DLL_PROCESS_DETACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)TrueSleep, TimedSleep);
        error = DetourTransactionCommit();

        printf("simple.dll: Removed Sleep() (result=%d), slept %d ticks.\n",
               error, dwSlept);
        fflush(stdout);
    }
    return TRUE;
}

//
///////////////////////////////////////////////////////////////// End of File.
</windows.h></stdio.h>


So I tried to make it similar to detoured.dll:

simple.h:
#ifdef SIMPLE_EXPORTS
#define SIMPLE_API __declspec(dllexport)
#else
#define SIMPLE_API __declspec(dllimport)
#endif

HMODULE SIMPLE_API WINAPI Simple();


Simple.cpp:
//////////////////////////////////////////////////////////////////////////////
//
//  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.
//
#include <stdio.h>
#include <windows.h>
#include "..\detours\detours.h"
#define SIMPLE_EXPORTS

static LONG dwSlept = 0;
static VOID (WINAPI * TrueSleep)(DWORD dwMilliseconds) = Sleep;
static HMODULE s1_hDll;

HMODULE WINAPI Simple()
{
    return s1_hDll;
}

VOID WINAPI TimedSleep(DWORD dwMilliseconds)
{
    DWORD dwBeg = GetTickCount();
    TrueSleep(dwMilliseconds);
    DWORD dwEnd = GetTickCount();

    InterlockedExchangeAdd(&dwSlept, dwEnd - dwBeg);
}

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

    if (dwReason == DLL_PROCESS_ATTACH) {
		s1_hDll = hinst;
        printf("simple.dll: Starting.\n");
        fflush(stdout);

        DetourRestoreAfterWith();

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)TrueSleep, TimedSleep);
        error = DetourTransactionCommit();

        if (error == NO_ERROR) {
            printf("simple.dll: Detoured Sleep().\n");
        }
        else {
            printf("simple.dll: Error detouring Sleep(): %d\n", error);
        }
    }
    else if (dwReason == DLL_PROCESS_DETACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)TrueSleep, TimedSleep);
        error = DetourTransactionCommit();

        printf("simple.dll: Removed Sleep() (result=%d), slept %d ticks.\n",
               error, dwSlept);
        fflush(stdout);
    }
    return TRUE;
}

//
///////////////////////////////////////////////////////////////// End of File.
</windows.h></stdio.h>


But Simple.dll still doesn't export anything, because SIMPLE_EXPORTS is undefined. How to build this project correctly?
AnswerRe: Detours Simple sample applications dll export problem Pin
«_Superman_»29-Jan-09 1:49
professional«_Superman_»29-Jan-09 1:49 
GeneralRe: Detours Simple sample applications dll export problem Pin
keret29-Jan-09 3:41
keret29-Jan-09 3:41 
GeneralRe: Detours Simple sample applications dll export problem Pin
«_Superman_»29-Jan-09 19:49
professional«_Superman_»29-Jan-09 19:49 
GeneralRe: Detours Simple sample applications dll export problem Pin
keret30-Jan-09 0:35
keret30-Jan-09 0:35 
Questionhow to get file name to input to 'IWMMetadataEditor' in C++ ? Pin
Supriya Tonape29-Jan-09 1:22
Supriya Tonape29-Jan-09 1:22 
QuestionThread Question Pin
Dennis L29-Jan-09 0:54
Dennis L29-Jan-09 0:54 
AnswerRe: Thread Question Pin
Code-o-mat29-Jan-09 0:59
Code-o-mat29-Jan-09 0:59 
GeneralRe: Thread Question [modified] Pin
Dennis L29-Jan-09 1:36
Dennis L29-Jan-09 1:36 
GeneralRe: Thread Question Pin
Code-o-mat29-Jan-09 1:56
Code-o-mat29-Jan-09 1:56 
GeneralRe: Thread Question Pin
Dennis L29-Jan-09 2:17
Dennis L29-Jan-09 2:17 
GeneralRe: Thread Question Pin
Code-o-mat29-Jan-09 2:32
Code-o-mat29-Jan-09 2:32 
GeneralRe: Thread Question Pin
Dennis L29-Jan-09 3:03
Dennis L29-Jan-09 3:03 
GeneralRe: Thread Question Pin
Stephen Hewitt29-Jan-09 2:27
Stephen Hewitt29-Jan-09 2:27 
AnswerRe: Thread Question Pin
SandipG 29-Jan-09 1:02
SandipG 29-Jan-09 1:02 
QuestionHooking into mouse messages in a Command/MSDOS window Pin
goop0029-Jan-09 0:46
goop0029-Jan-09 0:46 
AnswerRe: Hooking into mouse messages in a Command/MSDOS window Pin
ATM@CodeProject29-Jan-09 0:49
ATM@CodeProject29-Jan-09 0:49 
GeneralRe: Hooking into mouse messages in a Command/MSDOS window Pin
goop0029-Jan-09 0:53
goop0029-Jan-09 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.