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

Discussion in 'C++' started by cai, Dec 9, 2003.

  1. cai

    cai Guest

    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?
     
    cai, Dec 9, 2003
    #1
    1. Advertising

  2. cai

    David White Guest

    "cai" <> wrote in message
    news:br3e55$25sps2$-berlin.de...
    > 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
     
    David White, Dec 9, 2003
    #2
    1. Advertising

  3. cai

    Ron Natalie Guest

    "cai" <> wrote in message news:br3e55$25sps2$-berlin.de...
    > 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.
     
    Ron Natalie, Dec 9, 2003
    #3
  4. cai

    jeffc Guest

    "David White" <> wrote in message
    news:%fbBb.4967$...
    >
    > This newsgroup is for discussion of standard C++ only. No one here knows
    > what a dll file is...


    Now that would be astounding.
     
    jeffc, Dec 9, 2003
    #4
  5. cai

    GrOrH Guest

    [OT] Re: Why c++ string can't be used in a dll?

    > 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
     
    GrOrH, Dec 9, 2003
    #5
  6. cai

    GrOrH Guest

    Re: [OT] Re: Why c++ string can't be used in a dll?

    <snip>
    Apologies, this work around does not work for all STL containers. It works
    for vector, but not string.
     
    GrOrH, Dec 12, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. solartimba
    Replies:
    15
    Views:
    795
    red floyd
    Oct 20, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    978
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,222
    Smokey Grindel
    Dec 2, 2006
  4. Casey Hawthorne
    Replies:
    1
    Views:
    755
    Arne Vajhøj
    Mar 18, 2009
  5. H. Simpson
    Replies:
    4
    Views:
    342
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page