Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About loadlibrary problem. [modified]

About loadlibrary problem. [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebugging
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangningyu
    wrote on last edited by
    #1

    Hello everybody ! In my dll project,I export some functions, and I used them with the static library in main project,it's normal and have none errors.dll code like this:

    // Golbal variables
    unsigned char ParamBuffer[0x5c];
    unsigned char SendBuffer[1024];
    unsigned char RecvBuffer[1024];
    CWaitDlg *g_ProgressDlg;

    int InitParams()
    {
    // Initial the buffer before to write to the serialport.
    // On the side,or read the buffer from some files.
    }

    int SetSendBuffer(unsigned char *temp,int nLen)
    {
    memcpy(ParamBuffer,temp,nLen);
    return 0;
    }
    int ShowDlg()
    {
    // Show the mode progress windows ,and how many buffer was written.
    }

    int CreateDownThread()
    {
    // Here is used a new thread to write the buffer to the serialport.
    ...
    }

    But I used it with dynamic library in the main project,the GUI is hang-up,and the buffer isn't written to the serialport,because I use the Portmon to catch the serial event. the main project code like this:

    // this is all global variable
    typedef BOOL (CALLBACK *MyTxtFormat)(char *,char *); // this function int other dll
    typedef int (CALLBACK *MyShowLoadingDlg)(BOOL);
    typedef int (CALLBACK *MySetSendBuffer)(unsigned char *,int nLen);
    typedef int (CALLBACK *MyCreateDownThread)();
    typedef int (CALLBACK *MyInitialParam)();

    static MyInitialParam InitialParam;
    static MyShowLoadingDlg ShowLoadingDlg;
    static MySetSendBuffer SetSendBuffer;
    static MyCreateDownThread CreateDownThread;

    HINSTANCE hCode;
    HINSTANCE hHardware;
    MyTxtFormat TxtFormat;

    void CMainFrame::OnDownload()
    {
    TCHAR *szpBuffer = new TCHAR [8000];
    unsigned char szTemp[4096];

    BOOL		bRet = FALSE;
    CFile		pFile;
    DWORD		nActual = 0;
    
    memset(szpBuffer,255,8000);
    hCode=LoadLibrary("aaa.dll");
    TxtAAA=(MyTxtFormat)GetProcAddress(hCode,"TxtFormat");
    if (TxtToCode)
    {
    	bRet = TxtFormat(strLDPath.GetBuffer(strLDPath.GetLength()),
    		strBinPath.GetBuffer(strBinPath.GetLength()));
    	if(!bRet)
    	{
    		MessageBox("TxtFormat error!");
    		return;
    	}
        
    	// In the debug window,the szpBuffer is normal.
    	if ( pFile.Open( \_T(strBinPath),CFile::modeReadWrite | CFile::typeBinary, NULL ) )
    	{
    		nActual = pFile.Read(szpBuffer, 8000);
    	}
    }
    
    // This is my dll,I used F10 to debug step info, that
    hHardware = LoadLibrary("bbb.dll");
    if(hHardware)
    {
    	ShowLoadingDlg=(MyShowLoadingDlg)GetProcAddress(hHardware,"ShowLoadingDlg");
    	if(ShowLoadingDlg == NULL)
    	{
    		MessageBox("ShowLoadingDlg
    
    A L N 3 Replies Last reply
    0
    • W wangningyu

      Hello everybody ! In my dll project,I export some functions, and I used them with the static library in main project,it's normal and have none errors.dll code like this:

      // Golbal variables
      unsigned char ParamBuffer[0x5c];
      unsigned char SendBuffer[1024];
      unsigned char RecvBuffer[1024];
      CWaitDlg *g_ProgressDlg;

      int InitParams()
      {
      // Initial the buffer before to write to the serialport.
      // On the side,or read the buffer from some files.
      }

      int SetSendBuffer(unsigned char *temp,int nLen)
      {
      memcpy(ParamBuffer,temp,nLen);
      return 0;
      }
      int ShowDlg()
      {
      // Show the mode progress windows ,and how many buffer was written.
      }

      int CreateDownThread()
      {
      // Here is used a new thread to write the buffer to the serialport.
      ...
      }

      But I used it with dynamic library in the main project,the GUI is hang-up,and the buffer isn't written to the serialport,because I use the Portmon to catch the serial event. the main project code like this:

      // this is all global variable
      typedef BOOL (CALLBACK *MyTxtFormat)(char *,char *); // this function int other dll
      typedef int (CALLBACK *MyShowLoadingDlg)(BOOL);
      typedef int (CALLBACK *MySetSendBuffer)(unsigned char *,int nLen);
      typedef int (CALLBACK *MyCreateDownThread)();
      typedef int (CALLBACK *MyInitialParam)();

      static MyInitialParam InitialParam;
      static MyShowLoadingDlg ShowLoadingDlg;
      static MySetSendBuffer SetSendBuffer;
      static MyCreateDownThread CreateDownThread;

      HINSTANCE hCode;
      HINSTANCE hHardware;
      MyTxtFormat TxtFormat;

      void CMainFrame::OnDownload()
      {
      TCHAR *szpBuffer = new TCHAR [8000];
      unsigned char szTemp[4096];

      BOOL		bRet = FALSE;
      CFile		pFile;
      DWORD		nActual = 0;
      
      memset(szpBuffer,255,8000);
      hCode=LoadLibrary("aaa.dll");
      TxtAAA=(MyTxtFormat)GetProcAddress(hCode,"TxtFormat");
      if (TxtToCode)
      {
      	bRet = TxtFormat(strLDPath.GetBuffer(strLDPath.GetLength()),
      		strBinPath.GetBuffer(strBinPath.GetLength()));
      	if(!bRet)
      	{
      		MessageBox("TxtFormat error!");
      		return;
      	}
          
      	// In the debug window,the szpBuffer is normal.
      	if ( pFile.Open( \_T(strBinPath),CFile::modeReadWrite | CFile::typeBinary, NULL ) )
      	{
      		nActual = pFile.Read(szpBuffer, 8000);
      	}
      }
      
      // This is my dll,I used F10 to debug step info, that
      hHardware = LoadLibrary("bbb.dll");
      if(hHardware)
      {
      	ShowLoadingDlg=(MyShowLoadingDlg)GetProcAddress(hHardware,"ShowLoadingDlg");
      	if(ShowLoadingDlg == NULL)
      	{
      		MessageBox("ShowLoadingDlg
      
      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      Try calling GetLastError and passing the result through FormatMessage - that'll give you an idea of why LoadLibrary's failing. Generally it's because it can't find the library. Cheers, Ash

      1 Reply Last reply
      0
      • W wangningyu

        Hello everybody ! In my dll project,I export some functions, and I used them with the static library in main project,it's normal and have none errors.dll code like this:

        // Golbal variables
        unsigned char ParamBuffer[0x5c];
        unsigned char SendBuffer[1024];
        unsigned char RecvBuffer[1024];
        CWaitDlg *g_ProgressDlg;

        int InitParams()
        {
        // Initial the buffer before to write to the serialport.
        // On the side,or read the buffer from some files.
        }

        int SetSendBuffer(unsigned char *temp,int nLen)
        {
        memcpy(ParamBuffer,temp,nLen);
        return 0;
        }
        int ShowDlg()
        {
        // Show the mode progress windows ,and how many buffer was written.
        }

        int CreateDownThread()
        {
        // Here is used a new thread to write the buffer to the serialport.
        ...
        }

        But I used it with dynamic library in the main project,the GUI is hang-up,and the buffer isn't written to the serialport,because I use the Portmon to catch the serial event. the main project code like this:

        // this is all global variable
        typedef BOOL (CALLBACK *MyTxtFormat)(char *,char *); // this function int other dll
        typedef int (CALLBACK *MyShowLoadingDlg)(BOOL);
        typedef int (CALLBACK *MySetSendBuffer)(unsigned char *,int nLen);
        typedef int (CALLBACK *MyCreateDownThread)();
        typedef int (CALLBACK *MyInitialParam)();

        static MyInitialParam InitialParam;
        static MyShowLoadingDlg ShowLoadingDlg;
        static MySetSendBuffer SetSendBuffer;
        static MyCreateDownThread CreateDownThread;

        HINSTANCE hCode;
        HINSTANCE hHardware;
        MyTxtFormat TxtFormat;

        void CMainFrame::OnDownload()
        {
        TCHAR *szpBuffer = new TCHAR [8000];
        unsigned char szTemp[4096];

        BOOL		bRet = FALSE;
        CFile		pFile;
        DWORD		nActual = 0;
        
        memset(szpBuffer,255,8000);
        hCode=LoadLibrary("aaa.dll");
        TxtAAA=(MyTxtFormat)GetProcAddress(hCode,"TxtFormat");
        if (TxtToCode)
        {
        	bRet = TxtFormat(strLDPath.GetBuffer(strLDPath.GetLength()),
        		strBinPath.GetBuffer(strBinPath.GetLength()));
        	if(!bRet)
        	{
        		MessageBox("TxtFormat error!");
        		return;
        	}
            
        	// In the debug window,the szpBuffer is normal.
        	if ( pFile.Open( \_T(strBinPath),CFile::modeReadWrite | CFile::typeBinary, NULL ) )
        	{
        		nActual = pFile.Read(szpBuffer, 8000);
        	}
        }
        
        // This is my dll,I used F10 to debug step info, that
        hHardware = LoadLibrary("bbb.dll");
        if(hHardware)
        {
        	ShowLoadingDlg=(MyShowLoadingDlg)GetProcAddress(hHardware,"ShowLoadingDlg");
        	if(ShowLoadingDlg == NULL)
        	{
        		MessageBox("ShowLoadingDlg
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You have shown the code for your main program that calls the DLL but you have not indicated where it has stopped or what the code in the DLL is doing. Try stepping into the DLL code with your debugger to see why it is hanging.

        It's time for a new signature.

        1 Reply Last reply
        0
        • W wangningyu

          Hello everybody ! In my dll project,I export some functions, and I used them with the static library in main project,it's normal and have none errors.dll code like this:

          // Golbal variables
          unsigned char ParamBuffer[0x5c];
          unsigned char SendBuffer[1024];
          unsigned char RecvBuffer[1024];
          CWaitDlg *g_ProgressDlg;

          int InitParams()
          {
          // Initial the buffer before to write to the serialport.
          // On the side,or read the buffer from some files.
          }

          int SetSendBuffer(unsigned char *temp,int nLen)
          {
          memcpy(ParamBuffer,temp,nLen);
          return 0;
          }
          int ShowDlg()
          {
          // Show the mode progress windows ,and how many buffer was written.
          }

          int CreateDownThread()
          {
          // Here is used a new thread to write the buffer to the serialport.
          ...
          }

          But I used it with dynamic library in the main project,the GUI is hang-up,and the buffer isn't written to the serialport,because I use the Portmon to catch the serial event. the main project code like this:

          // this is all global variable
          typedef BOOL (CALLBACK *MyTxtFormat)(char *,char *); // this function int other dll
          typedef int (CALLBACK *MyShowLoadingDlg)(BOOL);
          typedef int (CALLBACK *MySetSendBuffer)(unsigned char *,int nLen);
          typedef int (CALLBACK *MyCreateDownThread)();
          typedef int (CALLBACK *MyInitialParam)();

          static MyInitialParam InitialParam;
          static MyShowLoadingDlg ShowLoadingDlg;
          static MySetSendBuffer SetSendBuffer;
          static MyCreateDownThread CreateDownThread;

          HINSTANCE hCode;
          HINSTANCE hHardware;
          MyTxtFormat TxtFormat;

          void CMainFrame::OnDownload()
          {
          TCHAR *szpBuffer = new TCHAR [8000];
          unsigned char szTemp[4096];

          BOOL		bRet = FALSE;
          CFile		pFile;
          DWORD		nActual = 0;
          
          memset(szpBuffer,255,8000);
          hCode=LoadLibrary("aaa.dll");
          TxtAAA=(MyTxtFormat)GetProcAddress(hCode,"TxtFormat");
          if (TxtToCode)
          {
          	bRet = TxtFormat(strLDPath.GetBuffer(strLDPath.GetLength()),
          		strBinPath.GetBuffer(strBinPath.GetLength()));
          	if(!bRet)
          	{
          		MessageBox("TxtFormat error!");
          		return;
          	}
              
          	// In the debug window,the szpBuffer is normal.
          	if ( pFile.Open( \_T(strBinPath),CFile::modeReadWrite | CFile::typeBinary, NULL ) )
          	{
          		nActual = pFile.Read(szpBuffer, 8000);
          	}
          }
          
          // This is my dll,I used F10 to debug step info, that
          hHardware = LoadLibrary("bbb.dll");
          if(hHardware)
          {
          	ShowLoadingDlg=(MyShowLoadingDlg)GetProcAddress(hHardware,"ShowLoadingDlg");
          	if(ShowLoadingDlg == NULL)
          	{
          		MessageBox("ShowLoadingDlg
          
          N Offline
          N Offline
          norish
          wrote on last edited by
          #4

          Check the calling convention of those exported functions. The default calling convention is __cdecl but some API function requires __stdcall.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups