how to use a third party dll

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sam1900
    New Member
    • Apr 2010
    • 3

    how to use a third party dll

    hi
    i want to call some functions from a DLL(which is provided by a hardware manufacturer without the .lib and .def). I've used these following code


    Code:
      HINSTANCE hinstLib; 
        MYPROC ProcAdd=NULL; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary(TEXT("C:\My.dll")); 
     
        // If the handle is valid, try to get the function address.
     
        if (hinstLib != NULL) 
        { 
    		std::cout<<"loaded\n";
    		   
          
    
     ProcAdd = (MYPROC) GetProcAddress(hinstLib, "funcinDLL");
          
     // If the function address is valid, call the function.
         
            if (NULL != ProcAdd) 
            {
                fRunTimeLinkSuccess = TRUE;
                (*ProcAdd) (); 
            }
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
    
        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess) 
            printf("Message printed from executable\n");
    but LoadLybrary is failing to load the DLL..
    can anyone suggest me , how can i do the job?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    When LoadLibrary fails it must be for a reason. Assuming that you have the path correct I suggest you call and print the return value from GetLastError to get some clarificaion of what is causing the problem.

    You can look up the value returned from GetLastError in winerror.h

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Your path to the dll must be C:\\My.dll. The \ is an escape character so you need \\ to get an actual \ in your path.

      Also, the function name used by GetProcAddress must be exported by the DLL and if the DLL is written in C++ that must also be an extern "C" function.

      Comment

      • sam1900
        New Member
        • Apr 2010
        • 3

        #4
        hi
        thanks for your reply.
        i used the following code to get the last error code.

        LPTSTR lpszFunction=TE XT("GetProcessI d");
        LPVOID lpMsgBuf;
        LPVOID lpDisplayBuf;
        DWORD dw = GetLastError();

        FormatMessage(
        FORMAT_MESSAGE_ ALLOCATE_BUFFER |
        FORMAT_MESSAGE_ FROM_SYSTEM |
        FORMAT_MESSAGE_ IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG _NEUTRAL, SUBLANG_DEFAULT ),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
        std::cout<<"ent er2\n";
        // Display the error message and exit the process

        lpDisplayBuf = (LPVOID)LocalAl loc(LMEM_ZEROIN IT,
        (lstrlen((LPCTS TR)lpMsgBuf) + lstrlen((LPCTST R)lpszFunction) + 40) * sizeof(TCHAR));
        StringCchPrintf ((LPTSTR)lpDisp layBuf,
        LocalSize(lpDis playBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"),
        lpszFunction, dw, lpMsgBuf);
        MessageBox(NULL , (LPCTSTR)lpDisp layBuf, TEXT("Error"), MB_OK);
        std::cout<<"ent er3\n";
        LocalFree(lpMsg Buf);
        LocalFree(lpDis playBuf);
        ExitProcess(dw) ;
        But it didn't print the error code

        Comment

        • sam1900
          New Member
          • Apr 2010
          • 3

          #5
          hi
          thanks for your reply.
          the DLL is in VC++. I didn't get that you have said about the path. The actual location of the dll is
          C:\Documents and Settings\hexapo d\Desktop\cyton _important\My.d ll
          i used this path in the LoadLibrary.
          i kept a copy of the dll in project folder also.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You have to call GetLastError() immediately after the function:

            Code:
            AWindowsFunnction();
            DWORD dw = GetLastError();
            to get the error from AWindowsFunctio n.


            This path:

            C:\Documents and Settings\hexapo d\Desktop\cyton _important\My.d ll

            is seen by the compiler as:

            C:ocuments and Settingsexapode sktopyton_impor tanty.dll

            because the \ is the escape sequence. That means the character after the \ is a code and not a separate character. Like \n means newline character and not a \ followed by an n.

            You have to code \\ to tell the compiler that the code is \.

            Therefore:

            C:\\Documents and Settings\\hexap od\\Desktop\cyt on_important\\M y.dll

            compiles to:

            C:\Documents and Settings\hexapo d\Desktop\cyton _important\My.d ll

            which is what you want.

            Comment

            Working...