Results 1 to 3 of 3

Thread: VB เขียนติดต่อ DLL ทำไงครับ

  1. #1
    Junior Member
    Join Date
    Dec 2006
    Posts
    0


    VB เขียนติดต่อ DLL ทำไงครับ

    คือผมเขียน VB มาระยะนึงแล้วนะครับทราบว่าว่า การสร้าง DLL ของตัวเอง และการไปใช้ของที่มีอยู่แล้วต้องทำยังไงครับ

  2. #2
    BingLi224
    Guest


    Re: VB เขียนติดต่อ DLL ทำไงครับ

    DLL ที่สร้างด้วย VB จะเป็น ActiveX DLL รายละเอียดหาได้ใน MSDN ของ Microsoft.

    ส่วนการเรียก DLL ทั่วไปมาใช้คือ

    [src][Public | Private] Declare Function <FunctionName> Lib "<LibraryPath>" (<Arguments>) [As <VariableType>]
    [/src]

    [src] < LibraryPath > [/src] จะเป็นชื่อ DLL หรือพร้อม path ก็ได้ ในกรณีที่ไม่มี path คือจะหาจาก <WindowsDirectory>/System และ directory ที่ application (App.Path) อยู่
    เช่น program อยู่ที่ d:/program/ และมี WindowsDirectory ที่ c:/Windows/
    ถ้าใส่

    Public Declare Function CountDigit Lib "str.dll" (ByRef Txt As String) As Integer

    คือจะหาที่ c:/Windows/System/str.dll หรือถ้าไม่เจอจะหา d:/programs/str.dll แทน ถ้าไม่เจออีกจะ error.

    Public Declare Function CountDigit Lib "c:/mydlls/str.dll" (ByRef Txt As String) As Integer

    คือจะหา c:/mydlls/str.dll โดยตรง แต่ถ้าไม่เจอจะ error

    สามารถเรียกใช้ DLL แบบ relative ได้
    เช่น
    Public Declare Function CountDigit Lib "tools/str.dll" (ByRef Txt As String) As Integer

    คือจะหา ที่ d:/programs/tools/str.dll

    ส่วนวิธีการเรียกใช้ก็เหมือน Sub หรือ Function ทั่วไปใน VB
    เช่น

    Dim i%
    i=CountDigit("More Emoticons...")

  3. #3
    Senior Member
    Join Date
    Sep 2003
    Location
    Thailand
    Posts
    136


    Re: VB เขียนติดต่อ DLL ทำไงครับ

    Visual Basic อนุญาตให้โปรแกรมเมอร์สร้าง DLL เพื่อใช้งานร่วมกับโปรแกรมอื่น ๆ ที่สนับสนุนเทคโนโลยี ActiveX แต่เท่าที่สังเกตดูมักจะเกิดปัญหาอยู่บ่อยมากในการสร้าง DLL ด้วย VB

    Visual C++ สามารถสร้าง DLL ซึ่งบรรจุฟังก์ชั่นที่สร้างขึ้นใหม่ และนำไปเรียกใช้ใน vb ได้
    รายละเอียดค่อนข้างมาก ดังที่คุณ BingLi224 ได้อธิบายไปแล้วนะครับ..

    หลักการคร่าว ๆ
    1. สร้าง DLL โดยใช้ VC และเลือกชนิด DLL ในตอนเริ่มต้น
    2. เขียนฟังก์ชั่น สมมติ
    3. คอมไพล์ได้ผลลัพธ์เป็น .DLL

    การใช้งาน
    1. ประกาศ ใช้ฟังก์ชั่นด้วยคำสั่ง Declare Function ..................... ตามด้วยอร์กิวเมนต์
    2. สามารถเรียกใช้ใน VB ได้
    ====================================
    ตัวอย่าง : คัดมาจากหนังสือ Visual Basic 6.0 Developer's Workshop
    ====================================
    [src]
    #include < windows.h >
    #include < ole2.h >

    BYTE _stdcall TestByte( BYTE a, LPBYTE b )
    {
    *b = a + a;
    return( *b + a );
    }

    short _stdcall TestInteger( short a, short far * b )
    {
    *b = a + a;
    return( *b + a );
    }

    LONG _stdcall TestLong( LONG a, LPLONG b )
    {
    *b = a + a;
    return( *b + a );
    }
    float _stdcall TestSingle( float a, float far * b )
    {
    *b = a + a;
    return( *b + a );
    }

    double _stdcall TestDouble( double a, double far * b )
    {
    *b = a + a;
    return( *b + a );
    }

    void _stdcall ReverseString( BSTR a )
    {
    int i, iLen;
    BSTR b;
    LPSTR pA, pB;

    iLen = strlen( (LPCSTR)a );
    b = SysAllocStringLen( NULL, iLen );

    pA = (LPSTR)a;
    pB = (LPSTR)b + iLen -1;

    for ( i = 0; i < iLen; i++ )
    *pB-- = *pA++;

    pA = (LPSTR)a;
    pB = (LPSTR)b;

    for ( i = 0; i < iLen; i++ )
    *pA++ = *pB++;

    SysFreeString( b );
    }

    =========================================
    Option Explicit

    Private Declare Function TestByte _
    Lib "mydll.dll" ( _
    ByVal a As Byte, _
    ByRef b As Byte _
    ) As Byte

    Private Declare Function TestInteger _
    Lib "mydll.dll" ( _
    ByVal a As Integer, _
    ByRef b As Integer _
    ) As Integer

    Private Declare Function TestLong _
    Lib "mydll.dll" ( _
    ByVal a As Long, _
    ByRef b As Long _
    ) As Long

    Private Declare Function TestSingle _
    Lib "mydll.dll" ( _
    ByVal a As Single, _
    ByRef b As Single _
    ) As Single

    Private Declare Function TestDouble _
    Lib "mydll.dll" ( _
    ByVal a As Double, _
    ByRef b As Double _
    ) As Double

    Private Declare Sub ReverseString _
    Lib "mydll.dll" ( _
    ByVal a As String _
    )

    Private Sub cmdGo_Click()
    Dim bytA As Byte
    Dim bytB As Byte
    Dim bytC As Byte
    Dim intA As Integer
    Dim intB As Integer
    Dim intC As Integer
    Dim lngA As Long
    Dim lngB As Long
    Dim lngC As Long
    Dim sngA As Single
    Dim sngB As Single
    Dim sngC As Single
    Dim dblA As Double
    Dim dblB As Double
    Dim dblC As Double
    Dim strA As String

    bytA = 17
    bytC = TestByte(bytA, byt
    Print bytA, bytB, bytC

    intA = 17
    intC = TestInteger(intA, int
    Print intA, intB, intC

    lngA = 17
    lngC = TestLong(lngA, lng
    Print lngA, lngB, lngC

    sngA = 17
    sngC = TestSingle(sngA, sng
    Print sngA, sngB, sngC

    dblA = 17
    dblC = TestDouble(dblA, dbl
    Print dblA, dblB, dblC

    strA = "This string will be reversed"
    Print strA
    ReverseString (strA)
    Print strA
    End Sub[/src]
    ==================================

Similar Threads

  1. Replies: 4
    Last Post: 25-01-2005, 12:17 PM
  2. Replies: 1
    Last Post: 07-01-2005, 01:36 PM
  3. Replies: 2
    Last Post: 27-12-2004, 09:16 AM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •