Why c++ string can't be used in a dll?

C

cai

Hi ,everyone, I come up with a problem, and I struggle with it for time ,
but I can't find the answer out.
I got a dll file like this:

/////////dll file////////
typedef struct tagIEPROXY
{
DWORD dwProxyEnable;
#ifdef UNICODE
wstring strProxyOverride;
wstring strProxyServer;
#else
string strProxyOverride;
string strProxyServer;
#endif
}IEPROXY,*PIEPROXY;

BOOL GetIEProxy(OUT IEPROXY& IEProxy)
{
IEProxy.strProxyServer = _T("Test");
return TRUE;
}

/////// the EXE file /////////
VOID main()
{
IEPROXY IeProxy;
if(!GetIEProxy(IeProxy))
return;
IeProxy.strProxyServer = _T("");
printf("%s","OK\n");
}

In debug mode, when I run up, it shows up an error:
_crtIsValidHeapPointer(pUserData). Why?
 
D

David White

cai said:
Hi ,everyone, I come up with a problem, and I struggle with it for time ,
but I can't find the answer out.
I got a dll file like this:

/////////dll file////////
typedef struct tagIEPROXY
{
DWORD dwProxyEnable;
#ifdef UNICODE
wstring strProxyOverride;
wstring strProxyServer;
#else
string strProxyOverride;
string strProxyServer;
#endif
}IEPROXY,*PIEPROXY;

BOOL GetIEProxy(OUT IEPROXY& IEProxy)
{
IEProxy.strProxyServer = _T("Test");
return TRUE;
}

/////// the EXE file /////////
VOID main()
{
IEPROXY IeProxy;
if(!GetIEProxy(IeProxy))
return;
IeProxy.strProxyServer = _T("");
printf("%s","OK\n");
}

This newsgroup is for discussion of standard C++ only. No one here knows
what a dll file is, or can tell you why you can't use a string in one. This
might direct you to the right place: http://www.slack.net/~shiva/welcome.txt

DW
 
R

Ron Natalie

cai said:
Hi ,everyone, I come up with a problem, and I struggle with it for time ,
but I can't find the answer out.

Ask in a microsoft.public.vc.language. The short answer is that your DLL and
your application are using a different C++ runtime library so the string in one is
NOT the same as the string in the other.
 
G

GrOrH

typedef struct tagIEPROXY
{
DWORD dwProxyEnable;
#ifdef UNICODE
wstring strProxyOverride;
wstring strProxyServer;
#else
string strProxyOverride;
string strProxyServer;
#endif
}IEPROXY,*PIEPROXY;
<snip>
This is a VC++ template issue when passing STL containers by reference to a
DLL. The workaround is to wrapper an instance of your struct in a simple
class and pass that class by reference, having your struct publicly
available.

Pseudo code (untested): -
// DLL Code
class ProxyWrapper
{
public:
tagIEPROXY m_prox;
};

void MyDllFunction(ProxyWrapper& rProx)
{
rProx.m_prox.strProxyOveride = "wibble";
}

// Calling code
ProxyWrapper prox;
MyDllFunction(prox);
std::string szRes(prox.m_prox.strProxyOveride);

HTH,
Great Orange Hunter
 
G

GrOrH

<snip>
Apologies, this work around does not work for all STL containers. It works
for vector, but not string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top