2013-08-31 3 views
0

나는 CPP를 처음 시작합니다. 나는 dll을 생성하는 비주얼 스튜디오 프로젝트를 가지고있다. 이 dll 함수를 호출하는 간단한 코드를 작성해야합니다.CPP에서 DLL 사용

지금까지 내가 훑어 본 대부분의 질문은 dll이 외부 응용 프로그램에서 호출 된 문제를 처리했습니다.

이 개념을 소개하는 매우 간단한 자습서가 필요합니다. DLL을 한 번로드 한 다음 응용 프로그램이 아닌 간단한 코드에서 반복적으로 함수를 호출합니다.

간단한 예제 또는 이에 대한 링크가 매우 유용합니다. 사전

+0

명확히하시기 바랍니다 점 :: 1. 당신은 당신이 exe 인 b.exe를 2. DLL을 A.DLL 있습니다. 3. b.exe에서 a.dll을로드하고 내 보낸 함수를 호출하도록합니다. 당신의 질문을 올바르게 이해 했습니까? – Abhineet

+0

'dll'myDll.dll '이 있다고 가정하십시오. 'myDll.dll'함수를 호출하는 코드 'testDll.cpp'을 작성하고 싶습니다. 나는 지금 어떤 감각을 만들었나요? –

+0

"cpp에서 dll 호출"당신은 b.exe를 작성하는 코드가 a.dll을로드하고 사용하는 정확한 소스 코드를 포함한다는 것을 알고 있습니다 ... – Abhineet

답변

1

에서

감사 기본 개념은

  1. 을 LoadLibrary ::입니다 : DLL을로드합니다.
  2. GetProcAddress : dll의 내 보낸 기능의 주소를 가져옵니다. 당신을 가정

MSDN Sample Code // 올바르게 수출 기능 DLL을 구축했다. // LoadLibrary 및 // GetProcAddress를 사용하여 Myputs.dll에서 myPuts에 액세스하는 간단한 프로그램입니다.

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

typedef int (__cdecl *MYPROC)(LPWSTR); 

int main(void) 
{ 
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    // Get a handle to the DLL module. 

    hinstLib = LoadLibrary(TEXT("MyPuts.dll")); 

    // If the handle is valid, try to get the function address. 

    if (hinstLib != NULL) 
    { 
     ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 

     // If the function address is valid, call the function. 

     if (NULL != ProcAdd) 
     { 
      fRunTimeLinkSuccess = TRUE; 
      (ProcAdd) (L"Message sent to the DLL function\n"); 
     } 
     // 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"); 

    return 0; 

} 
1

dll 프로젝트에서 함수를 내 보내야합니다.

Ex: "ExportFunc" 

그리고 당신은 DLL에 funciton 사용하는 다른 프로젝트에서 LoadLibrary, GetProcAddress를 사용할 수 있습니다. 예 :

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

    typedef int (__cdecl *MYPROC)(LPWSTR); 

    int main(void) 

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

    // Get a handle to the DLL module. 

    hinstLib = LoadLibrary(TEXT("DllName.dll")); 

    // If the handle is valid, try to get the function address. 

    if (hinstLib != NULL) 
    { 
     ProcAdd = (MYPRO`enter code here`C) GetProcAddress(hinstLib, "ExportFunction"); 

     // If the function address is valid, call the function. 

     if (NULL != ProcAdd) 
     { 
      fRunTimeLinkSuccess = TRUE; 
      (ProcAdd) (L"Message sent to the DLL function\n"); 
     } 
     // 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"); 

    return 0; 

}