I found this function used in hook function. but I can't find any explain of it. 
it's declared like this:
extern SceUID sceKernelSearchModuleByName (const char *);
I use it in this way:
SceUID thid;
thid=sceKernelSearchModuleByName(mainHookSave[0].modname);
where mainHookSave[o].modname is "sceController_Service". the return value is -2147352365, I think this value incorrect.
I run this code under 3.71 M33, could you like give some information of it ? anythink will be ok.
TKS!
			
			
									
									
						what is " sceKernelSearchModuleByName "
- 
				Pirata Nervo
 - Posts: 409
 - Joined: Tue Oct 09, 2007 4:22 am
 
thid is meant to be "thread ID" and that function is for modules not threads :P
However, you can give whatever name you want.
the function is:
SceModule* sceKernelFindModuleByName(const char * modname);
http://alek.dark-alex.org/pspsdkdocs/gr ... ee904fd688
			
			
									
									
						However, you can give whatever name you want.
the function is:
SceModule* sceKernelFindModuleByName(const char * modname);
http://alek.dark-alex.org/pspsdkdocs/gr ... ee904fd688
Thank you
Thank you very much!
however, I want to hook the key, when I press LEFT, it runs RIGHT, and when I press RIGHT, it runs LEFT. Here is my code. if there no problem with sceKernelFindModuleByName, I don't know why....
this is the main.c of PRX
this is the head file of main.c, the main.h
and this is head file of hooktable.h
the next is the PRX loader
I modify the code base on RemaPSP. I modify the
void overrideControls(SceCtrlData *pad_data)
function, left others.
			
			
									
									
						however, I want to hook the key, when I press LEFT, it runs RIGHT, and when I press RIGHT, it runs LEFT. Here is my code. if there no problem with sceKernelFindModuleByName, I don't know why....
this is the main.c of PRX
Code: Select all
#include <pspctrl.h>
#include <psphprm.h>
#include <psppower.h>
#include <time.h>
#include <psprtc.h>
#include "main.h"
#include "hooktable.h"
char mainUmdId[16];
// *** MODULE INITIALISATION ***
PSP_MODULE_INFO("KeyHook",0x1000,1,1);
// *** FUNCTIONS PATCH ***
typedef int (*FUNC_CTRL) (SceCtrlData*, int);
typedef int (*FUNC_LATCH) (SceCtrlLatch*);
#define PAD_THRESHOLD 50
SceCtrlData dupe_pad;
struct ctrlSetup controllerConfig;
unsigned char holdingNote = 0;
unsigned char enabled    = 0;
unsigned char inTurbo = 0; //Toggle every time we are called
//Macro Variables
unsigned char inMacro = 0; //0 if not in a macro, 1/2 if we are - index of macro + 1
u64 macroStartTime = 0; //Time the current macro piece started
unsigned char macroUpToIndex; //Index of current Macro Piece
//Works out how far an analog is pressed for remapping the analogs
//This returns the difference from center that the given control is, based on dupe_pad
void overrideControls(SceCtrlData *pad_data)
{
	memcpy(&dupe_pad, pad_data, sizeof(SceCtrlData));
	///Set up the variables for processing
	inTurbo = !inTurbo; //Toggle the turbos~
	//Clear off the area for the remote, some useless bits up there
	dupe_pad.Buttons &= 0x00ffffff;
	//Set up some extra bits in the dupe_pad.Buttons (remote and analog)
	if (sceHprmIsRemoteExist()) ///Remote
	{
		u32 hprm = 0;
		sceHprmPeekCurrentKey(&hprm);
		dupe_pad.Buttons |= (hprm<<24);
	}
		
		//Digitals
		pad_data->Buttons |= ((controllerConfig.digital_R.pressed & dupe_pad.Buttons) || (inTurbo && (controllerConfig.digital_R.turbo & dupe_pad.Buttons)))?PSP_CTRL_LEFT:0;
		pad_data->Buttons |= ((controllerConfig.digital_L.pressed & dupe_pad.Buttons) || (inTurbo && (controllerConfig.digital_L.turbo & dupe_pad.Buttons)))?PSP_CTRL_RIGHT:0;
	if (pad_data->Buttons) //if there are any keys set, send a power stay on!
		scePowerTick(0);
}
int sceCtrlPeekBufferPositiveFake(SceCtrlData *pad_data, int count)
{
	u32 k1;
	k1 = pspSdkSetK1(0);
	int res = ((FUNC_CTRL) mainHookSave[0].modfunc.addr)(pad_data, count);
	overrideControls(pad_data);
	pspSdkSetK1(k1);
	return res;
}
int sceCtrlPeekBufferNegativeFake(SceCtrlData *pad_data, int count)
{
	u32 k1;
	k1 = pspSdkSetK1(0);
	int res = ((FUNC_CTRL) mainHookSave[1].modfunc.addr)(pad_data, count);
	overrideControls(pad_data);
	pspSdkSetK1(k1);
	return res;
}
int sceCtrlReadBufferPositiveFake(SceCtrlData *pad_data, int count)
{
	u32 k1;
	k1 = pspSdkSetK1(0);
	int res = ((FUNC_CTRL) mainHookSave[2].modfunc.addr)(pad_data, count);
	overrideControls(pad_data);
	pspSdkSetK1(k1);
	return res;
}
int sceCtrlReadBufferNegativeFake(SceCtrlData *pad_data, int count)
{
	u32 k1;
	k1 = pspSdkSetK1(0);
	int res = ((FUNC_CTRL) mainHookSave[3].modfunc.addr)(pad_data, count);
	overrideControls(pad_data);
	pspSdkSetK1(k1);
	return res;
}
unsigned int previousPressed;
int sceCtrlPeekLatchFake(SceCtrlLatch *latch_data)
{
	SceCtrlData pad;
	int res = sceCtrlPeekBufferPositiveFake(&pad, 1);
	//Gen new Latch
	latch_data->uiMake  = (previousPressed ^ pad.Buttons) & pad.Buttons;
	latch_data->uiBreak = (previousPressed ^ pad.Buttons) & previousPressed;
	latch_data->uiPress   =  pad.Buttons;
	latch_data->uiRelease = ~pad.Buttons;
	previousPressed = pad.Buttons;
	return res;
}
int sceCtrlReadLatchFake(SceCtrlLatch *latch_data)
{
	SceCtrlData pad;
	int res = sceCtrlPeekBufferPositiveFake(&pad, 1);
	//For one reason or another this is no good. Makes games run too slow
	//int res = sceCtrlReadBufferPositiveFake(&pad, 1);
	//Gen new Latch
	latch_data->uiMake  = (previousPressed ^ pad.Buttons) & pad.Buttons;
	latch_data->uiBreak = (previousPressed ^ pad.Buttons) & previousPressed;
	latch_data->uiPress   =  pad.Buttons;
	latch_data->uiRelease = ~pad.Buttons;
	previousPressed = pad.Buttons;
	return res;
}
// hook 线程,重新定义函数
int HookThread (SceSize args, void *argp)
{
	//SceUID loaderid, fd;
	int ret, x;
	//fd = 0; //shutup about unused ;)
	previousPressed = 0;
	//Make sure kernel is in analog sampling mode!
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
	
	// Patch syscall table, here the program use jmp func in stub table, so patch not applied here but in all user modules
  // u32 moduleHookFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func)
	for (x=0;x<MAIN_HOOK_NBR;x++)
	{
		ret = moduleHookFunc(&mainHookSave[x].modfunc, sceKernelSearchModuleByName(mainHookSave[x].modname), mainHookSave[x].libname, mainHookSave[x].nid, mainHookSave[x].func);
	}
	sceKernelExitDeleteThread(0);
	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
int HookStart(void)
{
	SceUID thid;
    int ret[MAIN_HOOK_NBR], x;
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
	for (x=0;x<MAIN_HOOK_NBR;x++)
       {
		ret[x] = moduleHookFunc(&mainHookSave[x].modfunc, sceKernelSearchModuleByName(mainHookSave[x].modname), mainHookSave[x].libname, mainHookSave[x].nid, mainHookSave[x].func);
	}
    for(x=0; x<MAIN_HOOK_NBR; x++)
         {
      if(ret[x]<=3)
              {
         return ret[x];
               }
         }
	return 0;
}
int module_start(SceSize args, void *argp)
{
	return 0;
}
/*****************************************************************************/
/* This is apparently never called when running as a VSH OE plugin.          */
/*****************************************************************************/
int module_stop(SceSize args, void *argp)
{
	int x;
	// Restore functions
	for (x = 0; x < MAIN_HOOK_NBR; x++)
		moduleRestoreFunc(&mainHookSave[x].modfunc);
        return 0;
}
this is the head file of main.c, the main.h
Code: Select all
#ifndef __MAIN_H__
#define __MAIN_H__
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspumd.h>
#include <stdio.h>
#include <stdlib.h>
#include "conf.h"
#include "Utils/module.h"
extern SceUID sceKernelSearchModuleByName (const char *);
//Global for use with the menu :)
extern struct ctrlSetup controllerConfig;
typedef struct MainHook
{
 ModuleFunc modfunc;
 char modname[32];
 char libname[32];
 u32 nid;
 void *func;
} MainHook;
// *** FUNCTIONS DECLARATIONS ***
int sceCtrlPeekBufferPositiveFake(SceCtrlData *pad_data, int count);
int sceCtrlPeekBufferNegativeFake(SceCtrlData *pad_data, int count);
int sceCtrlReadBufferPositiveFake(SceCtrlData *pad_data, int count);
int sceCtrlReadBufferNegativeFake(SceCtrlData *pad_data, int count);
//int vshCtrlReadBufferPositiveFake(SceCtrlData *pad_data, int count);
int sceCtrlPeekLatchFake(SceCtrlLatch *latch_data);
int sceCtrlReadLatchFake(SceCtrlLatch *latch_data);
extern unsigned char enabled;
#endif
and this is head file of hooktable.h
Code: Select all
#ifndef HOOKTABLE_INCLUDED
#define HOOKTABLE_INCLUDED
#define	 MAIN_HOOK_NBR	6
MainHook mainHookSave[MAIN_HOOK_NBR] =
{
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0x3A622550, sceCtrlPeekBufferPositiveFake },
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0xC152080A, sceCtrlPeekBufferNegativeFake },
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0x1F803938, sceCtrlReadBufferPositiveFake },
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0x60B81F86, sceCtrlReadBufferNegativeFake },
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0xb1d0e5cd, sceCtrlPeekLatchFake },
	{ { 0, NULL }, "sceController_Service", "sceCtrl", 0x0b588501, sceCtrlReadLatchFake },
//	Attempt at getting nid overloading working with menus.
//	Can't get this to work: moduleHookFunc[4] return : 0x2
//  Incorrect NID or something? :(
//	{ { 0, NULL }, "sceVshBridge_Driver", "sceVshBridge", 0xc6395c03, vshCtrlReadBufferPositiveFake },
};
#endif
the next is the PRX loader
Code: Select all
#include <pspsdk.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>
#include <pspdisplay.h>
#include "grap.h"
/* Define the module info section */
PSP_MODULE_INFO("CONTROLTEST", 0, 1, 1);
PSP_HEAP_SIZE_KB(20480);
/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
/* Define printf, just to make typing easier */
#define printf	pspDebugScreenPrintf
void dump_threadstatus(void);
int HookStart(void);
int done = 0;
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
	done = 1;
	return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
	int cbid;
	cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
	sceKernelRegisterExitCallback(cbid);
	sceKernelSleepThreadCB();
	return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
	int thid = 0;
	thid = sceKernelCreateThread("update_thread", CallbackThread,
				     0x11, 0xFA0, 0, 0);
	if(thid >= 0)
	{
		sceKernelStartThread(thid, 0, 0);
	}
	return thid;
}
void DrawPic(int x, int y, int Flag, u32 color)
{
    switch(Flag)
     {
     case 0:     // 三角
           DrawTri(x, y, color);
           break;
      case 1:    // 方块
          DrawRect(x, y, color);
          break;
      case 2:    //  叉
          DrawCross(x, y, color);
          break;
      case 3:   // 圆圈
          DrawCir(x, y, color);
          break;
     }
}
int main(void)
{
      int x=200, y=100;
      int flag=0;
      u32 color=ARGB(255,0,255,0);
      int Counter=0;
    int rets;
	SceCtrlData pad;
   SceUID modid;
	pspDebugScreenInit();
	SetupCallbacks();
      // mode 0, pixel format: ABGR
	sceDisplaySetMode(0, SCREEN_WIDTH, SCREEN_HEIGHT);
	sceCtrlSetSamplingCycle(0);
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
    modid = pspSdkLoadStartModule("keyhook.prx", PSP_MEMORY_PARTITION_KERNEL);
    if (modid < 0)
        {
        pspDebugScreenPrintf("Error 0x%08X loading/starting keyhook.prx.\n", modid);
        sceKernelDelayThread(5*1000*1000);
        sceKernelExitGame();
        }
   pspDebugScreenPrintf("load hook\n");
   rets=HookStart();
   if(rets!=0)
       {
      pspDebugScreenPrintf("load failed. return value: %d\n", rets);
       }
   else
      pspDebugScreenPrintf("load ok. return value: %d\n",rets);
	while(!done){
		pspDebugScreenSetXY(0, 2);
    		sceCtrlReadBufferPositive(&pad, 1);
            printf("This is a PSP key test program. have fun!         www.ACGclub.com  \n"); 
		printf("Analog X = %d ", pad.Lx);
		printf("Analog Y = %d \n", pad.Ly);
             FillRect(x-1,y-1,19,19, ARGB(255,0,0,0));
             DrawPic(x,y, flag, color);
		if (pad.Buttons != 0){
			if (pad.Buttons & PSP_CTRL_SQUARE){
				printf("Square pressed \n");
                         flag = 1;
			}
			if (pad.Buttons & PSP_CTRL_TRIANGLE){
				printf("Triangle pressed \n");
                         flag =0;
			} 
			if (pad.Buttons & PSP_CTRL_CIRCLE){
				printf("Cicle pressed \n");
                         flag =3;
			} 
			if (pad.Buttons & PSP_CTRL_CROSS){
				printf("Cross pressed \n");
                         flag =2;
			} 
			if (pad.Buttons & PSP_CTRL_UP){
				printf("Up pressed \n");
                         y--;
                         if(y<0){
                                y=0;
                                }
			} 
			if (pad.Buttons & PSP_CTRL_DOWN){
				printf("Down pressed \n");
                         y++;
                         if(y>SCREEN_HEIGHT) y=SCREEN_HEIGHT;
			} 
			if (pad.Buttons & PSP_CTRL_LEFT){
				printf("Left pressed \n");
                         x--;
                         if(x<0) x=0;
			} 
			if (pad.Buttons & PSP_CTRL_RIGHT){
				printf("Right pressed \n");
                         x++;
                         if(x>SCREEN_WIDTH)  x=SCREEN_WIDTH;
			}      
			if (pad.Buttons & PSP_CTRL_START){
				printf("Start pressed \n");
                         Counter++;
                         if(Counter>3) Counter = 0;
                         switch(Counter){
                          case 0: color=ARGB(255, 255, 0, 0); break;
                          case 1: color=ARGB(255, 255, 255, 0); break;
                          case 2: color=ARGB(255, 255, 0, 255); break;
                          case 3: color=ARGB(255, 0, 255, 0);   break;
                                }
			}
			if (pad.Buttons & PSP_CTRL_SELECT){
				printf("Select pressed \n");
			}
			if (pad.Buttons & PSP_CTRL_LTRIGGER){
				printf("L-trigger pressed \n");
			}
			if (pad.Buttons & PSP_CTRL_RTRIGGER){
				printf("R-trigger pressed \n");
			}      
		}
	}
	sceKernelExitGame();
	return 0;
}
I modify the code base on RemaPSP. I modify the
void overrideControls(SceCtrlData *pad_data)
function, left others.
I find out that when I call moduleHookFunc function like this:
ret = moduleHookFunc(&mainHookSave[x].modfunc, sceKernelSearchModuleByName(mainHookSave[x].modname), mainHookSave[x].libname, mainHookSave[x].nid, mainHookSave[x].func);
it always stop here( the red line next), and return 2, it seems no address found.
the moduleHookFunc function from the module.c,
u32 moduleHookFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func)
{
u32 *addr;
// Verify parameters
if ((!(modfunc)) || (!(library)) || (!(func))) return 1;
// Find address of function in entry table and get pointer in entry table
addr = moduleFindFunc(moduleFindLibrary(modid,library),nid);
// If not found
if (!(addr)) return 2;
// Copy address of function in structure
modfunc->addr = *addr;
// Find address of function in syscall table and get pointer in syscall table
modfunc->sysaddr = moduleFindSyscallFunc(modfunc->addr);
// If not found
if (!(modfunc->sysaddr)) return 3;
// Hook function (copy func address to syscall table, overwrite old func)
return moduleHookAddr(modfunc->sysaddr,(u32) func);
}
			
			
									
									
						ret = moduleHookFunc(&mainHookSave[x].modfunc, sceKernelSearchModuleByName(mainHookSave[x].modname), mainHookSave[x].libname, mainHookSave[x].nid, mainHookSave[x].func);
it always stop here( the red line next), and return 2, it seems no address found.
the moduleHookFunc function from the module.c,
u32 moduleHookFunc (ModuleFunc *modfunc, SceUID modid, const char *library, SceUID nid, void *func)
{
u32 *addr;
// Verify parameters
if ((!(modfunc)) || (!(library)) || (!(func))) return 1;
// Find address of function in entry table and get pointer in entry table
addr = moduleFindFunc(moduleFindLibrary(modid,library),nid);
// If not found
if (!(addr)) return 2;
// Copy address of function in structure
modfunc->addr = *addr;
// Find address of function in syscall table and get pointer in syscall table
modfunc->sysaddr = moduleFindSyscallFunc(modfunc->addr);
// If not found
if (!(modfunc->sysaddr)) return 3;
// Hook function (copy func address to syscall table, overwrite old func)
return moduleHookAddr(modfunc->sysaddr,(u32) func);
}
