Chắc các anh em đều đã biết Delphi hay VC đều có thể tạo được API.
Theo lý thuyết thì VB6 không làm được điều này.
Do đó, ở topic này, tôi sẽ hướng dẫn anh em làm điều này.
Lợi ích: File DLL anh em tạo ra sẽ không cần đăng ký. Có nghĩa là khi chia sẻ ra bên ngoài sẽ vô cùng thuận lợi.
Kết hợp với
1. Tạo file Link.exe
Mở VB6 với quyền admin. Chọn standard exe.
Remove form vì không cần.
Trên đó tạo module bất kỳ, ghi code như sau:
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Build ra file Link.exe, copy vào thư mục ngoài Destop .
Mình có chia sẻ file Link.exe do mình tạo ra, các bạn có thể dùng luôn.
2. Đổi tên file Link.exe thành LinkLnk.exe
File này trên máy tính của mình nó nằm ở đây:
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Việc đổi tên thành như trên là rất quan trọng, tốt nhất hãy copy tên kẻo nhầm lẫn.
Sau đó copy file Link.exe mà bạn đã tạo ở bước 1 vào thư mục VB98 ở trên.
3. Tạo DLL
3.1 Tạo file def
Tôi muốn tạo ra file Fibonacci.dll
Do đó trước hết tôi sẽ tạo ra file Fibonacci.def
Nội dung của nó như sau:
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
3.2 Tạo file dll
Tạo ActiveX DLL project
Class1 để đó, không có nội dung gì.
Chèn Module mới, trên đó viết code:
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Lưu project cùng đường dẫn với file .def mà bạn tạo ở trên.
Tiếp theo build ra dll.
Vậy là xong. Bây giờ mình sẽ test thử DLL này.
Tạo project standard exe, trong form 1 viết code như sau:
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
Như vậy là thành công. Nói cách khác, DLL này cũng có thể thông qua Exe và chạy trên
Nguồn tham khảo :
	
								Theo lý thuyết thì VB6 không làm được điều này.
Do đó, ở topic này, tôi sẽ hướng dẫn anh em làm điều này.
Lợi ích: File DLL anh em tạo ra sẽ không cần đăng ký. Có nghĩa là khi chia sẻ ra bên ngoài sẽ vô cùng thuận lợi.
Kết hợp với
	Bạn cần đăng nhập để thấy link
 (dự kiến công khai 28/10/20201), tôi hi vọng anh em sẽ có thêm một sự lựa chọn tốt để bảo mật code của mình.1. Tạo file Link.exe
Mở VB6 với quyền admin. Chọn standard exe.
Remove form vì không cần.
Trên đó tạo module bất kỳ, ghi code như sau:
			
				Mã:
			
		
		
		Public Sub Main()
   Dim SpecialLink As Boolean, fCPL As Boolean, fResource _
       As Boolean
   Dim intPos As Integer
   Dim strCmd As String
   Dim strPath As String
   Dim strFileContents As String
   Dim strDefFile As String, strResFile As String
   Dim oFS As New Scripting.FileSystemObject
   Dim fld As Folder
   Dim fil As File
   Dim ts As TextStream, tsDef As TextStream
   strCmd = Command
   Set ts = oFS.CreateTextFile(App.Path & "\lnklog.txt")
   ts.WriteLine "Beginning execution at " & Date & " " & _
       Time()
   ts.WriteBlankLines 1
   ts.WriteLine "Command line arguments to LINK call:"
   ts.WriteBlankLines 1
   ts.WriteLine "   " & strCmd
   ts.WriteBlankLines 2
   ' Determine if .DEF file exists
   '
   ' Extract path from first .obj argument
   intPos = InStr(1, strCmd, ".OBJ", vbTextCompare)
   strPath = Mid(strCmd, 2, intPos + 2)
   intPos = InStrRev(strPath, "\")
   strPath = Left(strPath, intPos - 1)
   ' Open folder
   Set fld = oFS.GetFolder(strPath)
   ' Get files in folder
   For Each fil In fld.Files
      If UCase(oFS.GetExtensionName(fil)) = "DEF" Then
         strDefFile = fil
         SpecialLink = True
      End If
      If UCase(oFS.GetExtensionName(fil)) = "RES" Then
         strResFile = fil
         fResource = True
      End If
      If SpecialLink And fResource Then Exit For
   Next
    
   ' Change command line arguments if flag set
   If SpecialLink Then
      ' Determine contents of .DEF file
      Set tsDef = oFS.OpenTextFile(strDefFile)
      strFileContents = tsDef.ReadAll
      If InStr(1, strFileContents, "CplApplet", _
          vbTextCompare) > 0 Then
         fCPL = True
      End If
    
      ' Add module definition before /DLL switch
      intPos = InStr(1, strCmd, "/DLL", vbTextCompare)
      If intPos > 0 Then
         strCmd = Left(strCmd, intPos - 1) & _
               " /DEF:" & Chr(34) & strDefFile & Chr(34) & _
                   " " & _
               Mid(strCmd, intPos)
      End If
      ' Include .RES file if one exists
      If fResource Then
         intPos = InStr(1, strCmd, "/ENTRY", vbTextCompare)
         strCmd = Left(strCmd, intPos - 1) & Chr(34) & _
             strResFile & _
                  Chr(34) & " " & Mid(strCmd, intPos)
      End If
    
      ' If Control Panel applet, change "DLL" extension to
      ' "CPL"
      If fCPL Then
         strCmd = Replace(strCmd, ".dll", ".cpl", 1, , _
             vbTextCompare)
      End If
    
      ' Write linker options to output file
      ts.WriteLine "Command line arguments after " & _
          "modification:"
      ts.WriteBlankLines 1
      ts.WriteLine "   " & strCmd
      ts.WriteBlankLines 2
   End If
   ts.WriteLine "Calling LINK.EXE linker"
   Shell "linklnk.exe " & strCmd
   If Err.Number <> 0 Then
      ts.WriteLine "Error in calling linker..."
      Err.Clear
   End If
   ts.WriteBlankLines 1
   ts.WriteLine "Returned from linker call"
   ts.Close
End SubMình có chia sẻ file Link.exe do mình tạo ra, các bạn có thể dùng luôn.
2. Đổi tên file Link.exe thành LinkLnk.exe
File này trên máy tính của mình nó nằm ở đây:
			
				Mã:
			
		
		
		C:\Program Files (x86)\Microsoft Visual Studio\VB98Sau đó copy file Link.exe mà bạn đã tạo ở bước 1 vào thư mục VB98 ở trên.
3. Tạo DLL
3.1 Tạo file def
Tôi muốn tạo ra file Fibonacci.dll
Do đó trước hết tôi sẽ tạo ra file Fibonacci.def
Nội dung của nó như sau:
			
				Mã:
			
		
		
		NAME MathLib
LIBRARY MathMod
DESCRIPTION "Add-on Library of Mathematical Routines"
EXPORTS DllMain @1
        Fibo @2Tạo ActiveX DLL project
Class1 để đó, không có nội dung gì.
Chèn Module mới, trên đó viết code:
			
				Mã:
			
		
		
		Public Const DLL_PROCESS_DETACH = 0
Public Const DLL_PROCESS_ATTACH = 1
Public Const DLL_THREAD_ATTACH = 2
Public Const DLL_THREAD_DETACH = 3
Public Function DllMain(hInst As Long, fdwReason As Long, _
    lpvReserved As Long) As Boolean
   Select Case fdwReason
      Case DLL_PROCESS_DETACH
         ' No per-process cleanup needed
      Case DLL_PROCESS_ATTACH
         DllMain = True
      Case DLL_THREAD_ATTACH
         ' No per-thread initialization needed
      Case DLL_THREAD_DETACH
         ' No per-thread cleanup needed
   End Select
End Function
' Return a Fibonacci number.
Public Function Fibo(ByVal N As Integer) As Long
    If N <= 1 Then
        Fibo = 1
    Else
        Fibo = Fibo(N - 1) + Fibo(N - 2)
    End If
End FunctionTiếp theo build ra dll.
Vậy là xong. Bây giờ mình sẽ test thử DLL này.
Tạo project standard exe, trong form 1 viết code như sau:
			
				Mã:
			
		
		
		Private Declare Function Fibo Lib "C:\Users\user\Desktop\TEST1\Fibonacci.dll" (ByVal N As Integer) As Long
Private Sub cmdCalculate_Click()
    lblFibo.Caption = Format$(Fibo(CInt(txtN.Text)))
End Sub
	Bạn cần đăng nhập để thấy đính kèm
Như vậy là thành công. Nói cách khác, DLL này cũng có thể thông qua Exe và chạy trên
	Bạn cần đăng nhập để thấy link
.Nguồn tham khảo :
	Bạn cần đăng nhập để thấy link
Đính kèm
- 
		
			
		
		
		4.3 KB Lượt xem: 21
- 
		
			
		
		
		10.4 KB Lượt xem: 15
 
				 
 
		