C++ Access Violation while calling dll function

0

I am actually using an unmanaged C++ DLL, and I don't have access to the .h, .cpp or .lib, but only to the .DLL.

After using PE Explorer and finding the function I wanted to use, here is what I get :

@Tdtm_Dossier@Logon$qv; Index 1310; Unmangled Borland C++ Function: qualified function Tdtm_Dossier::Logon()

And here is what I get from using dumpbin :

1310 11F9 00105234 @Tdtm_Dossier@Logon$qv

Here is the exception :

Unhandled Exception at 0x034B258C (modDll.dll) in functionsCpp.exe : 0xC0000005 : 
Access violation writting to 0x000000AC.

The code I am using to call and use this function is as follow :

#include <stdio.h>
#include <Windows.h>
#include <iostream>

typedef int (*Logon)();

int main()
{
  HMODULE modDll;
  int resultLogon;
  modDll= LoadLibrary("C:\\dll\\modDll.dll");

  Logon logon;
  logon = (Logon)GetProcAddress(modDll,"@Tdtm_Dossier@Logon$qv");

  if(logon)
  {
    resultLogon = logon(); //<-- This is where I get the exception
    printf("Function has been loaded\n");
  }
  else
   // TODO: Error message

  FreeLibrary(modDll);
}

Since the DLL documentation doesn't give me any interesting information on how to use the function, I can't count on it.

The DLL is correctly loaded and the GetProcAddress does return something. I guess (but I'm not sure) that it has something to do with my typedef, but I can't figure out what could be the return type of this function.

c++
exception
dll
asked on Stack Overflow Feb 19, 2014 by Hyarantar

1 Answer

1

If you read e.g. this document on Borland C++ name mangling you might figure out that the symbol name "@Tdtm_Dossier@Logon$qv" represents a non-static member function of the class Tdtm_Dossier. You can't call non-static member function like normal functions, they have a hidden first argument that becomes the this pointer in the member function.

What's happening here is probably that the Logon member function tries to access member variables of the object instance, of which there is none, which leads to undefined behavior and a crash.

To be able to use this library you need the header file and the link library. You can't just call functions (member or not) and hope for the best.


User contributions licensed under CC BY-SA 3.0