본문 바로가기
Study/C++

C DLL을 비쥬얼베이직에서 사용하기

by 뿡뿡대마왕 2015. 10. 15.
반응형

c++로 만든 dll 비베에서 사용하는 경우가 있는데


MSDN에 예제가 잘되어 있어 발취해옴


URL

https://support.microsoft.com/ko-kr/kb/187912


단계별 예제 사용자 C DLL을 만듭니다.

  1. Visual C++ 5.0 열고 파일 메뉴에서 새로 만들기를 클릭 합니다. 프로젝트 탭에서 "Win32 동적 연결 라이브러리"를 선택 하 고 프로젝트의 이름을 "StrSamp."
  2. 다시 한번 파일 메뉴에서 새로 만들기를 클릭 하 고 선택 "" c + + 소스 파일. 파일 탭에서 "StrSamp.c," 파일 이름을 확인 합니다.
  3. 파일 형식으로 "텍스트 파일"이 시간을 선택 하 고 2 단계를 반복 합니다. "StrSamp.def," 파일 이름을 지정 하 고 확인을 누릅니다.
  4. 그런 다음, 다음 코드를 추가 "StrSamp.c."
    
          #include <windows.h>
    
          void __stdcall DisplayStringByVal(LPCSTR pszString)
          {
             //pszString is a pointer to a string
             MessageBox(NULL, pszString, "Display String ByVal",
                       MB_OK | MB_ICONINFORMATION);
          }
    
          void __stdcall DisplayStringByRef(LPCSTR* ppszString)
          {
             //ppszSting is a pointer to a pointer to a string
             MessageBox(NULL, *ppszString, "Display String ByRef",
                       MB_OK | MB_ICONINFORMATION);
          }
    
             void __stdcall FillString(LPSTR pszString, LONG cSize)
          {
             // Create a temp buffer with our string
             char buffer[] = "Hello from the C DLL!";
    
             // Copy our temp string to pszString
             // but check the size to make sure we have a buffer
             // big enough to hold the entire string.
             if (cSize > strlen(buffer))
                strcpy(pszString, buffer);
          }
    
          int __stdcall InStrRev(LPCSTR pszString, short iChar)
          {
             // This function is similar to Visual Basic's InStr function
             // except that it searches for the given ASCII character from
             // right to left, returning the character position of the
             // last occurrence (rather than the first) of the character
             // in the string.
             char* pszTmp;
             int nRet = 0;
    
             // Scan for iChar in pszString backwards
             pszTmp = strrchr(pszString, (int)iChar);
             if(pszTmp != NULL)
                nRet = pszTmp - pszString + 1;
    
             return nRet;
          }
    
    					
  5. 함수를 내보낼 수 있게 하려면 다음 추가 "StrSamp.def."
    
          LIBRARY StrSamp
          DESCRIPTION 'Microsoft KB Sample DLL'
          EXPORTS
             DisplayStringByVal
             DisplayStringByRef
             FillString
             InStrRev
    
    					
  6. 빌드 메뉴에서 DLL을 컴파일하십시오. 완료 되 면 새 DLL을 테스트 하는 데 Visual Basic 디렉터리에 복사 합니다.

단계별 예제 VB 테스트 응용 프로그램:

  1. Visual Basic 표준 프로젝트를 만들고 이름을 "StrTest." Form1이 기본적으로 만들어집니다.
  2. 세 개의 명령 단추를 Form1에 추가 합니다.
  3. Form1 코드 창에서 일반 선언 구역에 다음 추가:
    
          Option Explicit
    
          Private Declare Sub DisplayStringByRef Lib "StrSamp.dll" _
             (sMyString As String)
          Private Declare Sub DisplayStringByVal Lib "StrSamp.dll" _
             (ByVal sMyString As String)
          Private Declare Sub FillString Lib "StrSamp.dll" _
             (ByVal sMyString As String, ByVal cBufferSize As Long)
          Private Declare Function InStrRev Lib "StrSamp.dll" _
             (ByVal sMyString As String, ByVal iChar As Integer) _
             As Long
    
    					
    참고 문자열은 대부분 ByVal를 선언 합니다. 값으로 이러한 문자열을 전달 하는 것은 아닙니다. 값으로 BSTR 변수의 값을 전달 하는 대신 (BSTR 변수 문자열에 대 한 포인터 임을 기억 하십시오). ByVal 키워드는 긴 포인터를 문자열 (LPSTR)로 전달, 않으므로 바로 어떤 C 함수 기대 합니다. 

  4. 각 해당 명령 단추에 click 이벤트에 다음 코드를 추가 합니다.
    
          Private Sub Command1_Click()
             Dim sTestString1 As String
             Dim sTestString2 As String
    
             sTestString1 = "This is my string passed to the dll by value."
             DisplayStringByVal sTestString1
    
             sTestString2 = "This is my string passed to the dll by reference."
             DisplayStringByRef sTestString2
    
          End Sub
    
          Private Sub Command2_Click()
             Dim sFillTest As String
    
             sFillTest = Space$(260)
             FillString sFillTest, 260
             MsgBox Trim$(sFillTest), vbInformation, "Fill String"
          End Sub
    
          Private Sub Command3_Click()
             Dim sPathString As String
             Dim sMsg As String
             Dim lCharPosition As Long
    
             sPathString = "C:\My Documents\Temp\Item.txt"
             lCharPosition = InStrRev(sPathString, Asc("\"))
    
             If CBool(lCharPosition) Then
                sMsg = "The file '" & Mid$(sPathString, lCharPosition + 1)
                sMsg = sMsg & "' is at this location:" & vbCrLf & vbCrLf
                sMsg = sMsg & Left$(sPathString, lCharPosition - 1)
                MsgBox sMsg, vbInformation, "InStrRev"
             Else
                MsgBox "Cannot find '/' in " & sPathString, vbCritical
            End If
    
          End Sub
    
    					
  5. IDE에서 Visual Basic 프로젝트를 실행 하려면 F5 키를 누릅니다. 

    참고: 오류 메시지를 나타나면 때문일 수도 있습니다 Visual Basic DLL을 찾을 수 없습니다. 테스트 응용 프로그램을 실행 하기 전에 Visual Basic 디렉터리에 복사 되어 있는지 확인 하십시오.


반응형

댓글