现在的位置: 首页 > 综合 > 正文

DLL调用异常

2013年10月02日 ⁄ 综合 ⁄ 共 6601字 ⁄ 字号 评论关闭

 写了一个接口DLL,供华为IVR平台调用实现某功能。

奇怪的事情发生了,我用自己的demo调用一切OK,用IVR平台调用出现地址错误。

这个问题困惑了多天,最后经人指点,改成间接调用:IVR调用DLL时,DLL调用demo,demo再调用该DLL……。

想是只换了一个调用具体功能的父进程而已。

不知道问题出在哪,留此帖做记号。

代码如下:

  1. // faxdll.cpp : 定义 DLL 应用程序的入口点。
  2. #include "stdafx.h"
  3. #ifdef _MANAGED
  4. #pragma managed(push, off)
  5. #endif
  6. int g_c_debug;
  7. CString csIniFile = "C://CSoft//ConfigFax.ini";
  8. TCHAR g_szFaxPath[MAX_PATH];
  9. string strOther;
  10. CRITICAL_SECTION g_cs;
  11. BOOL APIENTRY DllMain( HMODULE hModule,
  12.                        DWORD  ul_reason_for_call,
  13.                        LPVOID lpReserved
  14.                      )
  15. {
  16.     switch (ul_reason_for_call)
  17.     {
  18.     case DLL_PROCESS_ATTACH:
  19.         g_c_debug = GetPrivateProfileInt("fax","DEBUG",1,csIniFile);
  20.         GetPrivateProfileString("fax","faxdir","C://CSoft//",g_szFaxPath,MAX_PATH,csIniFile);
  21.         //disDebug("PROCESS_ATTACH");
  22.         break;
  23.     case DLL_THREAD_ATTACH:
  24.         //disDebug("THREAD_ATTACH");
  25.         break;
  26.     case DLL_THREAD_DETACH:
  27.         //disDebug("THREAD_DETACH");
  28.         break;
  29.     case DLL_PROCESS_DETACH:
  30.         //DeleteCriticalSection(&g_cs);
  31.         //disDebug("PROCESS_DETACH");
  32.         break;
  33.     default:
  34.         //disDebug("default:%d!",ul_reason_for_call);
  35.         break;
  36.     }
  37.     return TRUE;
  38. }
  39. void _stdcall disDebug(const TCHAR *szFormat, ...)
  40. {
  41.     if (g_c_debug)
  42.     {
  43.         CString  csBuffer;
  44.         TCHAR   szBuffer [4096];
  45.         SYSTEMTIME  st;   
  46.         ::GetSystemTime(&st);
  47.         va_list pArgList ;
  48.         va_start (pArgList, szFormat) ;
  49.         _vsntprintf_s (szBuffer, sizeof (szBuffer) / sizeof (TCHAR), szFormat, pArgList) ;
  50.         csBuffer.Format("%02d:%02d:%02d/t%d/t%s/r/n",st.wHour + 8,st.wMinute,st.wSecond,::GetCurrentThreadId(),szBuffer);
  51.         va_end (pArgList) ;
  52.         CString m_logErrName;
  53.         m_logErrName.Format("%s%d.log","c://Csoft//",GetCurrentThreadId());
  54.         HANDLE fh = CreateFile(m_logErrName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 
  55.             FILE_ATTRIBUTE_NORMAL, NULL);
  56.         if(fh == INVALID_HANDLE_VALUE) 
  57.         {
  58.             return;
  59.         }
  60.         SetFilePointer(fh, 0, NULL, FILE_END);
  61.         DWORD length;
  62.         WriteFile(fh, csBuffer.GetBuffer(csBuffer.GetLength()), csBuffer.GetLength(), &length, NULL);
  63.         CloseHandle(fh);
  64.     }
  65.     return;
  66. }
  67. extern "C"
  68. {
  69.     int __stdcall CreateFaxFile(    
  70.         const char* pPhone,
  71.         const char* pCompanyName,
  72.         const char* pPhone2,
  73.         const char* pCompanyAddr,
  74.         const char* pCompanyPhone,
  75.         const char* pCompanyInfo,
  76.         char* filepath
  77.         )
  78.     {
  79.              char tiffpath[MAX_PATH];
  80.         TFAXBITMAPS faxtiff;
  81.         string sPhone = pPhone;
  82.         string sPhone2 = pPhone2;
  83.         string sCompanyName = pCompanyName;
  84.         string sCompanyAddr = pCompanyAddr;
  85.         string sCompanyPhone = pCompanyPhone;
  86.         string sCompanyInfo = pCompanyInfo;
  87.         sprintf_s(tiffpath,MAX_PATH,"%s%s.tiff",g_szFaxPath,pPhone);
  88.         //disDebug("before Createfaxfile/n");
  89.         faxtiff.CFax_BillFile(tiffpath,sPhone,sPhone2,sCompanyName,sCompanyAddr,sCompanyPhone,sCompanyInfo);
  90.         memset(filepath,0,sizeof(filepath));
  91.         strncpy(filepath,tiffpath,sizeof(tiffpath));
  92.         //disDebug("RET %s",filepath);
  93.         return 0;
  94.     }
  95.     int __stdcall DeleteFaxFile(char* pFilePath)
  96.     {
  97.         //disDebug("delete files:%s/n",pFilePath);
  98.         DWORD dwAttri = GetFileAttributes(pFilePath);
  99.         //disDebug("attribute:%x/n",dwAttri);
  100.         if (dwAttri == FILE_ATTRIBUTE_ARCHIVE)
  101.         {
  102.             //disDebug("before delete/n");
  103.             bool bdelete = DeleteFile(pFilePath);
  104.             //disDebug("after delete:%d/n",bdelete);
  105.             if(bdelete)
  106.             {
  107.                 return 0;
  108.             }
  109.         }
  110.         return 1;
  111.     }
  112. }
  113. #ifdef _MANAGED
  114. #pragma managed(pop)
  115. #endif

DEMO代码如下:

  1. // demo.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. #include <string>
  7. #include <iostream>
  8. using namespace std;
  9. typedef int (__stdcall* LPFNDLLFUNC1)(char*);
  10. typedef int (__stdcall* LPFNDLLFUNC2)(const char*,const char*,const char*,const char*,const char*,const char*,char*);
  11. typedef int (__stdcall* LPFNDLLFUNC3)();
  12. HINSTANCE hDLL;  
  13. LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
  14. LPFNDLLFUNC2 lpfnFaxFile;
  15. LPFNDLLFUNC3 lpfnTest;
  16. DWORD WINAPI ThreadFun(LPVOID n)
  17. {
  18.     
  19.     TCHAR buffer[MAX_PATH];
  20.     // Handle to DLL
  21.     //lpfnTest = (LPFNDLLFUNC3)GetProcAddress(hDLL,"MyTest");
  22.     int  nReturnVal = -1;
  23.     string strPhone,strPhone2,strCompanyInfo,strCompanyName,strCompanyAddr,strCompanyPhone;
  24.     
  25.     memset(buffer,0,sizeof(buffer));
  26.     itoa(GetCurrentThreadId(),buffer,10);
  27. //  nReturnVal = lpfnDllFunc1("C://CSoft//20080920_DEBUG.log");
  28. //  while(1)
  29.     {
  30. //      nReturnVal = lpfnTest();
  31. //      printf("ID:%d,nret:%d/n",GetCurrentThreadId(),nReturnVal);
  32. //      return 0;
  33.     }
  34.     //cout<<"dll文件当前目录"<<"/t"<<buffer<<endl;
  35.     //cout << "来电号码,查询号码,公司名称,公司地址,公司电话,,公司简介"<<endl;
  36.     printf("ID:%d,nret:%s/n",GetCurrentThreadId(),"before");
  37.     strPhone = itoa(GetCurrentThreadId(),buffer,10);
  38.     nReturnVal = lpfnFaxFile(strPhone.c_str(),strCompanyName.c_str(),strPhone2.c_str(),strCompanyAddr.c_str(),strCompanyPhone.c_str(),strCompanyInfo.c_str(),buffer);
  39.     printf("ID:%d,nret:%d/n",GetCurrentThreadId(),nReturnVal);
  40.     //cout << "返回值 "<<nReturnVal <<"thread:"<< GetCurrentThreadId() << endl;
  41.     //cout <<buffer<<endl;
  42.     return 0;
  43. }
  44. int _tmain(int argc, _TCHAR* argv[])
  45. {
  46.     const int nTheadNum = 2;
  47.     HANDLE harg[nTheadNum];
  48.     hDLL = ::LoadLibrary("faxdll.dll");
  49.     if (hDLL != NULL)
  50.     {
  51.         lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"DeleteFaxFile");
  52.         lpfnFaxFile = (LPFNDLLFUNC2)GetProcAddress(hDLL,"CreateFaxFile");
  53.         lpfnTest = (LPFNDLLFUNC3)GetProcAddress(hDLL,"MyTest");
  54.         if (!lpfnDllFunc1)
  55.         {
  56.             // handle the error
  57.             printf("入口装载失败/n");
  58.             FreeLibrary(hDLL);       
  59.             return 1;
  60.         }
  61.         else
  62.         {
  63.             // call the function
  64.             for (int i=0; i< nTheadNum; ++i)
  65.             {
  66.                 //ThreadFun(0);
  67.                 //Sleep(50);
  68.                 if((harg[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFun,0,0,NULL)) == NULL)
  69.                     cout << "线程创建失败" << endl;
  70.             }
  71.         }
  72.     }
  73.     else
  74.     {
  75.         printf("装载失败!/n");
  76.         DWORD derr = GetLastError();
  77.         printf("%x/n",derr);
  78.     }
  79.     DWORD dw = WaitForMultipleObjects(nTheadNum,harg,true,4000);
  80.     Sleep(100);
  81.     if(dw == WAIT_TIMEOUT)//INFINITE
  82.     {
  83.         for (int i = 0; i < nTheadNum; ++i)
  84.         {
  85.             TerminateThread(harg[i],1);
  86.         }
  87.         cout <<"/n/n超时退出/n/n"<<endl;
  88.     }
  89.     cout <<"/n/n/n退出码:"<<dw<<endl;
  90.     FreeLibrary(hDLL);
  91.     system("pause");
  92.     return 0;
  93. }

【上篇】
【下篇】

抱歉!评论已关闭.