Click here to Skip to main content
15,902,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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):

C++
#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.
Posted
Updated 16-Oct-11 19:17pm
v4

1 solution

Dear Dmitriy,

To export function from dll either use __declspec( dllexport ) or .def file both purpose are same ie export function from dll, but dont use both in same program. i recommand you to use .def file.

And to call function from dll try this if it is not working with the above changes
rpConnect=(fnRPC)GetProcAddress(hDLL,"rpc");
here replace "rpc" with its mangaled name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900