I am making an application for xbox 360 that will import functions which were exported from a system dll and call them when needed. I thought I did everything right as far as exporting then importing the functions, but it crashes on a single line of code.
I started by defining the functions inside of the system dll as follows:
void (__cdecl *SV_GameSendServerCommand)(int Client, int Type, char *Command) = (void(__cdecl * )(int, int, char * ))0x82254940;
bool (__cdecl *Dvar_GetBool)(char *Dvar) = (bool(__cdecl * )(char * ))0x8229EF58;
I created a .def file to export the functions while assigning their ordinals:
LIBRARY testdll
EXPORTS
SV_GameSendServerCommand @1
Dvar_GetBool @2
I built the system dll and placed the resulting testdll.lib in the folder where my application's source code was. I then placed the following in stdafx.h of that application:
#pragma comment(lib, "testdll.lib")
I prototyped the functions to be imported and used a function called resolveFunct to get the addresses of the imported functions.
void (__cdecl *SV_GameSendServerCommand)(int Client, int Type, char *Command);
bool (__cdecl *Dvar_GetBool)(char *Dvar);
UINT32 resolveFunct(char* modname, UINT32 ord)
{
UINT32 ret=0, ptr2=0;
HANDLE ptr32 = 0;
ret = XexGetModuleHandle(modname, &ptr32);
if(ret == 0)
{
ret = XexGetProcedureAddress(ptr32, ord, &ptr2);
if(ptr2 != 0)
return ptr2;
}
return 0; // function not found
}
When I tried printing the address of the function, it was successful and read 0x91F8BF54. I did this twice, and it printed both times. The proceeding line of code caused my application to crash.
DWORD WINAPI Start(LPVOID)
{
for(;;)
{
if(!LoadedUp)
{
printf("0x%p\n", resolveFunct("testdll.xex",2));
if(Dvar_GetBool == NULL)
{
printf("0x%p\n", resolveFunct("testdll.xex",2));
Dvar_GetBool = (bool(__cdecl*)(char*))resolveFunct("testdll.xex",2);
I don't understand why this line of code causes my program to crash, though. Any answers/suggestions are appreciated. Thanks!