-1
_declspec(dllexport) void Send(string strEmailAddress, string strHostAddress, string strUserName, string strPswrd, string strLocalFile, string strServerLocation, string strErrorMessage )
{
    nsFTP::CFTPClient ft ;
    wstring wstrHostName ( strHostAddress.begin(),strHostAddress.end() );
    string strApplicationUserName = "acpmat";
    string strApplicationPswrd = "A1c2p.M3a4t";
    wstring wstrUserName ( strApplicationUserName.begin(),strApplicationUserName.end() );
    wstring wstrPwrd     ( strApplicationPswrd.begin(),strApplicationPswrd.end() );
    wstring wstrLocalFile( strLocalFile.begin(),strLocalFile.end() );
    wstring wstrServerLoc( strServerLocation.begin(),strServerLocation.end() );

    nsFTP::CLogonInfo logonInfo(wstrHostName, 21, wstrUserName, wstrPwrd);
    // connect to server
    ft.Login(logonInfo);
    ft.UploadFile(wstrLocalFile, wstrServerLoc);

    CArray<CString, LPCTSTR> xToEmails;
    wstring strMailTo( strEmailAddress.begin(), strEmailAddress.end() );
    xToEmails.Add(strMailTo.c_str());

    const CString xCCEmail;
    const CString xReplyTo; 
    const CString xSubject(strErrorMessage.c_str());
    strUserName.append( "    " );
    strUserName.append( strLocalFile.c_str() );
    const CString xBodyFilePath( strUserName.c_str() );

    const CString& xFrom = _T("Exe_Crash@cat.com");
    const CString& xAttachmentFilePath = _T("");
    const CString& xServer = PES_EMAIL_SERVER;
    int xPort = PES_EMAIL_PORT;
    const CString& xCommand = PES_EMAIL_COMMAND;
    int lRes = email::Send(xToEmails, xCCEmail, xReplyTo, xSubject,    xBodyFilePath);

    return true; 
}

I call the above function from another application

typedef void (*FNPTR)(string a, string b, string c, string d, string e, string f, string g ); 
    //typedef int (__cdecl *MYPROC)(LPWSTR); 
    HINSTANCE hinstLib; 
    //MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess; 
    hinstLib = LoadLibrary(TEXT("DllCrashReport.dll")); 
    if (hinstLib != NULL) 
    {
        // If the handle is valid, try to get the function address.
        if (hinstLib != NULL) 
        { 
            FNPTR fn = (FNPTR)GetProcAddress(hinstLib, "Send"); 

            // If the function address is valid, call the function.
            if (NULL != fn) 
            {
                TCHAR name_1 [ UNLEN + 1 ];
                DWORD size_1 = UNLEN + 1;
                GetUserName( (TCHAR*)name_1, &size_1 );
                strUserName.clear();

                strUserName.append( name_1 );

                //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr)
                std::string converted_str( strUserName.begin() , strUserName.end() );


                fRunTimeLinkSuccess = TRUE;
                fn( strEmailAddress, strHostAddress, converted_str, strPswrd, strLocalFile, strServerLocation, strMailErrorMessage ); 
            }
            // Free the DLL module.
            fFreeResult = FreeLibrary(hinstLib); 
        } 

        // If unable to call the DLL function
        if (!fRunTimeLinkSuccess) 
            return ;
    }

The call stack shows me this: Symbols loaded. HEAP[MaterialCheck.exe]: Invalid address specified to RtlValidateHeap( 0000000000390000, 0000000002B82D30 ) and the crash occurs when it says that it tries to > mfc110ud.dll!operator delete(void * p) Line 351 C++

Can someone kindly help me please. THanks

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Please also note that its a typo, The data type of the return is a Void and not a Bool. Thank you – Jancy rani Apr 12 '16 at 14:21
  • Does `FNPTR` designate the correct function pointer type? In particular, are you using the correct calling convention? A [mcve] would help both you to identify the issue and us to help you. – IInspectable Apr 12 '16 at 14:45
  • The first thing I suggest you to clarify, whether the problem is specific to call from Dll or it's some problem with the Send() implementation itself. Try to move the Send() to exe and call it. Does the crash reproduce? – CodeFuller Apr 12 '16 at 14:48
  • It is a dll call error. I checked it by calling it as an exe and it works fine. I see that mfc110ud.dll! operator delete (void*p) Line 351 is where the crash happens – Jancy rani Apr 12 '16 at 15:20

1 Answers1

0

!!!! This is an important finding .. The problem got solved and this was a new learning for me.

While passing strings as parameter into a dll function call from another application , please pass the parameters as "const char*" and not as native strings.

Kindly refer: C++ Passing std::string by reference to function in dll

Community
  • 1
  • 1
  • If you can pass a `const char*` then you can pass a `const std::string&` as well. And if you can pass a `const std::string&` then it won't get modified. If it doesn't get modified, then passing resources across DLL boundaries is safe. The error you described in your question and this solution do not match. – IInspectable Apr 13 '16 at 16:06