错误导出DLL __stdcall [英] error export DLL __stdcall
问题描述
请问我有关DLL函数导出的问题吗?我遇到了棘手的错误,不知道如何解决.
我有功能齐全的DLL文件:
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;
...
}
而且我有使用此dll的控制台exe文件(VS C ++ 2010 Express均提供DLL和exe,均为unicode,非CLI):
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");
....
所有功能也都通过.def文件导出:
All function are exported throught .def file too:
LIBRARY sdll
EXPORTS
rpc @1
rpd @2
rpps @3
rppsl @4
rppf @5
正常编译.但是当我调用dll的函数(rppf)时出现错误-CWRPRNcon.exe中0x01071220的未处理异常:0xC0000005:访问冲突.
仅当我调用此函数时,才会发生此错误.之前的所有功能(rpc,rpps)均可正常运行.当我从dll #define中删除__stdcall时,我完全没有错误!当我再次输入时,我又出现错误.
您能帮我解决这个问题吗?我尝试但不能.
P.S.更新的问题
Dmitriy Bogdanov.
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.
推荐答案
亲爱的Dmitriy,
要从dll导出功能,请使用__declspec(dllexport)或.def文件,两者的目的是相同的,即从dll导出功能,但不要在同一程序中同时使用两者.我建议您使用.def文件.
并从dll调用函数,如果上述更改不起作用,请尝试此操作
rpConnect =(fnRPC)GetProcAddress(hDLL,"rpc");
在此将"rpc"替换为错误的名称
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
这篇关于错误导出DLL __stdcall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!