0

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!

Randy
  • 11
  • 1
  • If you have an import lib why are you linking explicitly? – David Heffernan Aug 08 '16 at 07:12
  • Besides poor programming practice, I don't see how that is an issue? – Randy Aug 12 '16 at 14:08
  • I'm trying to understand why you are doing what you are doing. Often it pays to dig a bit deeper when a question is asked. Often what is revealed can be significant. Anyway, sounds like you don't need my help. Good luck. – David Heffernan Aug 12 '16 at 14:12
  • Because of the `#pragma` directive, `resolveFunct` is not necessary. Just declare the funcs and then call them. Although, the 1st snippet (manually assigning an address to a func ptr is kind of unusual - and a perfect candidate for crash on my opinion). It's the function call (or the next line) that crashes right? – CristiFati Jan 16 '18 at 15:12

0 Answers0