2017-09-20 1 views
-1

je charge une dll dans mon code C++ mais quand exécuter j'obtiens l'erreur:fonction de la charge avec des paramètres de dll

dll:

`extern "C" __declspec(dllexport) int Add(int a,int b) 
    { 
     return a+b; 
    }` 

mon fichier console:

typedef int(__stdcall *f_funci) (int , int); 
int main() 
{ 
    HINSTANCE hGetProcIDDLL = LoadLibrary("ConsoleApplication1.dll"); 
    if (!hGetProcIDDLL) { 
     std::cout << "could not load the dynamic library" << std::endl; 
     system("pause"); 
     return EXIT_FAILURE; 
    } 
    f_funci funci = 0; 
    funci = (f_funci)GetProcAddress(hGetProcIDDLL, "Add"); 
    if (!funci) { 
     std::cout << "could not locate the function" << std::endl; 
     system("pause"); 
     return EXIT_FAILURE; 
    } 
    int t = 0; 
    t= (*funci)(2, 3); 
    std::cout << "funci() returned "<<t<< std::endl; 
    system("pause"); 
    return EXIT_SUCCESS; 
} 

cette erreur: enter image description here

+0

Je ne vois pas __stdcall dans la déclaration de fonction - mais vous l'avez ajouté à la définition du pointeur. Si vous appelez la fonction avec de mauvaises conventions d'appel, vous obtiendrez des erreurs comme indiqué sur l'écran d'impression –

Répondre

0

Le __stdcall change comment une fonction est appelée.

Appel à __stdcall sur x86 ...

push b; 
    push a; 
    call fnAdd; # stack is restored by the callee. with ret 8 

Appel à __cdecl

push b; 
    push a; 
    call fnAdd; 
    add esp, 8 # restore stack. 

Cela doit être adapté à la fonction avec la ret correcte ou ret 8.

Dans votre cas, __stdcall provoque l'interférence des valeurs a et b avec les registres qui conservent la trame de la fonction.