Can you please answer on my question about DLL functions export? I have tricky error and don't know how to resolve it.
I have DLL file with functions:
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
using namespace std;
#define EXPORT(RETTYPE) extern "C" __declspec( dllexport ) RETTYPE __stdcall
EXPORT(HANDLE) rpc(LPCWSTR s, int p);
EXPORT(int) rpd(HANDLE h);
EXPORT(int) rpps(HANDLE h, LPCWSTR sp);
EXPORT(int) rppsl(HANDLE h, LPCWSTR sp);
EXPORT(int) rppf(HANDLE h, LPCWSTR sf);
EXPORT (HANDLE) rpc(LPCWSTR s, int p)
{
HANDLE p;
p = malloc(sizeof(SOCKET_HANDLE));
if (p == NULL) return NULL;
PSOCKET_HANDLE h;
h= (PSOCKET_HANDLE)p;
WSADATA wsaData;
h->caSize = sizeof(h->ca);
int Ret;
...
}
EXPORT (int) rpps(HANDLE p, LPCWSTR s)
{
PSOCKET_HANDLE h = (PSOCKET_HANDLE)p;
if (h == NULL) return -1;
...
}
EXPORT(int) rppf(HANDLE p, LPCWSTR s)
{
PSOCKET_HANDLE h = (PSOCKET_HANDLE)p;
if (h== NULL) return -1;
...
}
And i have console exe file that use this dll(VS C++ 2010 Express both DLL and exe, both unicode, non CLI):
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL = LoadLibrary(L"mydll.dll");
if(hDLL == NULL)
{
printf("Error!");
return 1;
}
typedef HANDLE(*fnRPC)(LPCWSTR s, int p);
fnRPC rc;
rpc=(fnRPC)GetProcAddress(hDLL,"rpc");
typedef int(*fnRPD)(HANDLE h);
fnRPD rd;
rpd=(fnRPD)GetProcAddress(hDLL,"rpd");
typedef int(*fnRPPS)(HANDLE h, LPCWSTR s);
fnRPPS rpps;
rpps=(fnRPPS)GetProcAddress(hDLL,"rpps");
typedef int(*fnRPPSLF)(HANDLE h, LPCWSTR s);
fnRPPSLF rppslf;
rppslf=(fnRPPSLF)GetProcAddress(hDLL,"rppslf");
typedef int(*fnRPPF)(HANDLE h, LPCWSTR s);
fnRPPF rppf;
rppf=(fnRPPF)GetProcAddress(hDLL,"rppf");
gets_s(s);
char p[255];
printf("\n\n ----Type number and press Enter to continue\n\n");
gets_s(p);
HANDLE h = rpc((LPCWSTR)s, atoi(p));
result = rpps(h, (LPCWSTR)"text");
getchar();
result = rppf(h, (LPCWSTR)"text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n text\n");
....
All function are exported throught .def file too:
LIBRARY sdll
EXPORTS
rpc @1
rpd @2
rpps @3
rppsl @4
rppf @5
It's compiled normally. But I have error when I call the dll's function (rppf) - Unhandled exception at 0x01071220 in CWRPRNcon.exe: 0xC0000005: Access violation.
This error occurs only when I call this function. All functions before it(rpc,rpps) works without errors. And when I remove __stdcall from dll #define I have no errors at all! When i type it back then I have error again.
Can you please help me to resolve this? I try to but I can't.
P.S. updated question
Dmitriy Bogdanov.