C ++ Dll函数调用在返回时崩溃

时间:2016-04-12 14:20:12

标签: c++ windows dll

_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; 
}

我从另一个应用程序调用上面的函数

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 ;
    }

调用堆栈向我显示:已加载符号。 HEAP [MaterialCheck.exe]:为RtlValidateHeap(0000000000390000,0000000002B82D30)指定的无效地址,当它说它试图&gt;时发生崩溃。 mfc110ud.dll!operator delete(void * p)第351行C ++

请有人帮助我。感谢

1 个答案:

答案 0 :(得分:0)

!!!!这是一个重要的发现..问题得到了解决,这对我来说是一个新的学习。

将字符串作为参数传递给另一个应用程序的dll函数调用时,请将参数传递为&#34; const char *&#34;而不是本机字符串。

请参考: C++ Passing std::string by reference to function in dll