2012-12-02 52 views
0

我有一塊名爲DLP Discovery 4000的硬件,用於投影圖像。它可以由用戶編程。在硬件的編程指南中,它有一個可以在DLL API接口中訪問的函數列表。指南說這些函數可以通過C++/C/Java調用。如何在C++中爲USB投影機使用DLL接口

我希望能夠在C++程序中使用這些函數來控制電路板。但到目前爲止,我正在努力如何使用DLL API。

它配備了我安裝的軟件。該軟件具有功能有限的GUI。另外,該軟件在Windows目錄中放置一個名爲D4000_usb.dll的文件。我該如何使用這個.dll文件。

+0

請看手冊中有人的這個例子它是否給出了你可以調用的函數的定義。它是否在任何地方提及__declspec?或者它是否提到LoadLibrary或者.DEF文件?如果確實如此,請您告訴我們。手冊中是否有示例程序? –

回答

2

我發現呼籲MSDN

我前綴// ***一些輔助註釋註釋是同一個DLL給你一些指導

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 

typedef int (__cdecl *MYPROC)(LPWSTR); 

int main(void) 
{ 

    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    // *** This loads the API DLL into you program 

    // Get a handle to the DLL module. 
    hinstLib = LoadLibrary(TEXT("D4000_usb.dll")); 


    // *** Now we check if it loaded ok - ie we should have a handle to it 

    // If the handle is valid, try to get the function address. 
    if (hinstLib != NULL) 
    { 

     // *** Now we try and get a handle to the function GetNumDev 

     ProcAdd = (MYPROC) GetProcAddress(hinstLib, "GetNumDev"); 

     // *** Now we check if it that worked 

     // If the function address is valid, call the function. 
     if (NULL != ProcAdd) 
     { 
      fRunTimeLinkSuccess = TRUE; 

      // *** Now we call the function with a string parameter 

      (ProcAdd) (L"Message sent to the DLL function\n"); 


      // *** this is where you need to check if the function call worked 
      // *** and this where you need to read the manual to see what it returns 

     } 


     // *** Now we unload the API dll 

     // Free the DLL module. 
     fFreeResult = FreeLibrary(hinstLib); 
    } 

    // *** Here is a message to check if the the previous bit worked 


    // If unable to call the DLL function, use an alternative. 
    if (! fRunTimeLinkSuccess) 
     printf("Message printed from executable\n"); 

return 0; 
}