Click to See Complete Forum and Search --> : return error


Quell
August 12th, 2004, 12:22 PM
Hello.
i go this error in this line of code:
return((int)&sysMessageBox);
The error is:
error C2440: 'return' : cannot convert from 'unsigned int' to 'int (__stdcall *)(void)'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

JetDeveloper
August 12th, 2004, 12:37 PM
It would help if you gave more code, but it looks like the return value should be a function-pointer of the signature

int Func(void)

and you're trying to return a simple int.

You may need to use

FARPROC GetProcAddress(
HMODULE hModule, // Pointer to dll your func is from
LPCWSTR lpProcName); // Name of the func

Where hModule is found by
HMODULE hLib = GetModuleHandle ("SomeDLL.dll");

Then you should return GetProcAddress(hLib,"Func");

This is just my guess. Tell me if it works.

Quell
August 12th, 2004, 01:39 PM
basicly, i am injecting a dll into a process and hooking function from the dll.
this error was in dll.....
i hooked the GetProcAdress....
and if We call for messageBox (throu getProcAdress) then i pass the control to my function....
since the messageBox function is of int type...
when i return it in the getprocadress...i need to cast it to farproc?
that correct?

JetDeveloper
August 12th, 2004, 03:38 PM
Yeah, I think that it will work if you cast it to FARPROC.

Quell
August 12th, 2004, 04:58 PM
ok it worked...
but i got one more question....
the hooking seemes not to work every time...i mean it is more like it workes, then doesn';t work then workes again after liek 10 minutes.....
any ideas on waht this might be causing this?
or do u need the source code of the prog?

Mick
August 12th, 2004, 07:40 PM
Yes, post the source.

Quell
August 14th, 2004, 02:57 AM
ok...here is the source code of this test.
the test consists of 3 parts:
1->injected dll that will hook functions.
2->injecter that will inject dll into the process
3->tested app into which the dll will be injected.
i do not provide code of the injector since i tested it alot of times and it workes fine on many other ocasions withou errors....
1->Dll code:
0.1->main.cpp

#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"GLu32.lib")
#pragma comment(lib,"GLaux.lib")

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "apihook.h"

FARPROC ( WINAPI *pGetProcAddress )( HMODULE hModule, LPCSTR lpProcName );

FARPROC WINAPI sysGetProcAddress( HMODULE hModule, LPCSTR lpProcName );
int WINAPI sysMessageBox( HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption, UINT uType);

bool WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
{
if( fdwReason == DLL_PROCESS_ATTACH )
{
pGetProcAddress = ( FARPROC(WINAPI*)(HMODULE,LPCSTR))InterceptDllCall(GetModuleHandle(NULL )
,"Kernel32.dll"
,"GetProcAddress"
,(DWORD)&sysGetProcAddress );
}
return( true );
}

FARPROC WINAPI sysGetProcAddress( HMODULE hModule, LPCSTR lpProcName )
{
MessageBox(0,lpProcName,"sysGetProcAdress",0);
if(!lstrcmp(lpProcName,"GetProcAddressA" ))
{
return((FARPROC)&sysGetProcAddress);
}
else if(!lstrcmp(lpProcName,"MessageBoxA"))
{
return((FARPROC)&sysMessageBox);
}
else
{
return(GetProcAddress(hModule, lpProcName));
}
}

int WINAPI sysMessageBox( HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption, UINT uType)
{
MessageBox(0,"From hook ed MessegeBoxA",0,0);

return(MessageBox(hWnd,lpText,lpCaption, uType));
}

0.2->apihook.cpp

#include "apihook.h"

#define MakePtr( cast, ptr, addValue )( cast )( ( DWORD )( ptr ) + ( DWORD )( addValue ) )

void *InterceptDllCall( HMODULE hModule, char *szDllName, char *szFunctionName, DWORD pNewFunction )
{
PIMAGE_DOS_HEADER pDosHeader;
PIMAGE_NT_HEADERS pNTHeader;
PIMAGE_IMPORT_DESCRIPTOR pImportDesc;
PIMAGE_THUNK_DATA pThunk;
DWORD dwOldProtect;
DWORD dwOldProtect2;
void *pOldFunction;

if( !( pOldFunction = GetProcAddress( GetModuleHandle( szDllName ), szFunctionName ) ) )
return 0;

pDosHeader = ( PIMAGE_DOS_HEADER )hModule;
if( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE )
return( NULL );

pNTHeader = MakePtr( PIMAGE_NT_HEADERS, pDosHeader, pDosHeader->e_lfanew );
if( pNTHeader->Signature != IMAGE_NT_SIGNATURE
|| ( pImportDesc = MakePtr( PIMAGE_IMPORT_DESCRIPTOR, pDosHeader, pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress ) ) == ( PIMAGE_IMPORT_DESCRIPTOR )pNTHeader )
return( NULL );

while( pImportDesc->Name )
{
char *szModuleName = MakePtr( char *, pDosHeader, pImportDesc->Name );
if( !stricmp( szModuleName, szDllName ) )
break;
pImportDesc++;
}
if( pImportDesc->Name == NULL )
return( NULL );

pThunk = MakePtr( PIMAGE_THUNK_DATA, pDosHeader, pImportDesc->FirstThunk );
while( pThunk->u1.Function )
{
if( pThunk->u1.Function == ( PDWORD )pOldFunction )
{
VirtualProtect( ( void * )&pThunk->u1.Function, sizeof( DWORD ), PAGE_EXECUTE_READWRITE, &dwOldProtect );
pThunk->u1.Function = ( PDWORD )pNewFunction;
VirtualProtect( ( void * )&pThunk->u1.Function, sizeof( DWORD ), dwOldProtect, &dwOldProtect2 );
return( pOldFunction );
}
pThunk++;
}
return( NULL );
}

0.3 apihoook.h

#include <windows.h>
#pragma warning(disable:4311)
#pragma warning(disable:4312)

void *InterceptDllCall( HMODULE hModule, char *szDllName, char *szFunctionName, DWORD pNewFunction );



2->Injected INTO prog

// tests.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MessageBox(0,"test","test",0);
// TODO: Place code here.

return 0;
}


can somone please take a look at this code and see what did i do wrong with the dll so it does not hook the MessageBox?
i knwo it is alot of code but it is rather simple..
this is not the prog on wich the hooked function worked evry second time here it deosn't work at all, i didn;t post the other prog due to enourmouse amount of source....
thx in advnace