Постейшая DLL:
dllmain.cpp
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int DLLfunc(int i) {
int j = i;
j++;
return j++;
}
pch.h
#include "framework.h"
#ifdef DLLDIR_EX
#define DLLDIR __declspec(dllexport) __stdcall
#else
#define DLLDIR __declspec(dllimport)
#endif
extern "C" {
int DLLDIR DLLfunc(int);
};
Клиент:
#include <iostream>
#include "windows.h"
#include <tchar.h>
typedef int(__stdcall* myFunc)(int);
int main()
{
HINSTANCE hDLL;
hDLL = LoadLibrary(_T("...TestDll.dll"));
if (NULL != hDLL)
{
std::cout << "Loaded!\n";
myFunc funci = (myFunc)GetProcAddress(hDLL, "DLLfunc");
if (!funci) {
std::cout << "could not locate the function" << std::endl;
return EXIT_FAILURE;
}
int i = 19;
int j = funci(i);
std::cout << "myFunc() returned " << j << std::endl;
}
else {
std::cout << "Not Loaded!\n";
}
}
В строке:
int j = funci(i);
Ошибка:
Run-Time Check Failure #0 — The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Но если использовать typedef int(__cdecl* myFunc)(int) все работает без ошибок.
Тоже самое и в Delphi:
function DLLfunc(i : Integer) : Integer; stdcall; external 'TestDll2.dll'; // Access violation
function DLLfunc(i : Integer) : Integer; cdecl; external 'TestDll2.dll'; // Ok
Почему вызов с __stdcall не работает, хотя это используется при описании интерфейса в DLL?
#define DLLDIR __declspec(dllexport) __stdcall
Савсибо!