ดาวน์โหลดบทความ ดาวน์โหลดบทความ

ไฟล์ DLL ย่อมาจาก dynamic-linked library เป็นไฟล์ที่สร้างและสั่งการด้วยภาษา C++ โดยไฟล์ DLL นี้ทำให้แชร์ เก็บ และเซฟโค้ดได้ง่ายขึ้น บทความวิกิฮาวนี้จะแนะนำวิธีการสร้างไฟล์ DLL ด้วย Visual Studio , แอพใน Windows หรือ Visual Studio ใน Mac ให้คุณเอง อย่าลืมติ๊ก “Desktop Development with C++” ตอนติดตั้ง ถ้ามี Visual Studio อยู่แล้ว แต่ไม่ได้ติ๊กช่องนี้ ให้เปิดไฟล์ติดตั้งใหม่ แล้วติ๊กก่อน

  1. ปกติจะอยู่ใน Start Menu หรือโฟลเดอร์ Applications โดยไฟล์ DLL นั้นเป็นข้อมูล library เป็นแค่ส่วนหนึ่งของ project ปกติต้องมีแอพอื่นร่วมด้วย ถึงจะเปิดได้
  2. ปกติจะอยู่ด้านบนของพื้นที่ project (Windows) หรือยาวไปตามด้านบนของหน้าจอ (Macs)
  3. หน้าต่าง dialog box “Create a New Project” จะเปิดขึ้นมา
  4. จะเป็นตัวกำหนดเทมเพลต project ที่จะโผล่ขึ้นมา
    • คลิก Language เพื่อขยายเมนูลงมา แล้วคลิก C++
  5. เพื่อเน้นตัวเลือกด้วยสีฟ้า แล้วคลิก Next เพื่อไปต่อ
  6. เช่น พิมพ์ “MathLibrary” ในช่อง เป็นชื่อตัวอย่าง
  7. เท่านี้ก็สร้าง project DLL เรียบร้อย
  8. ทำได้โดยคลิก “ Add New Item ” ใน “ Project ” ในแถบเมนู
    • เลือก Visual C++ ในเมนูทางซ้ายของ dialog box
    • เลือก Header file (.h) ตรงกลาง dialog box
    • พิมพ์ชื่อ “MathLibrary.h” ในช่อง name ล่างตัวเลือกเมนูต่างๆ
    • คลิก Add แล้วจะได้ไฟล์ header ว่าง
    •  // MathLibrary.h - Contains declarations of math functions 
       #pragma once 
       #ifdef MATHLIBRARY_EXPORTS 
       #define MATHLIBRARY_API __declspec(dllexport) 
       #else 
       #define MATHLIBRARY_API __declspec(dllimport) 
       #endif 
       // The Fibonacci recurrence relation describes a sequence F 
       // where F(n) is { n = 0, a 
       //               { n = 1, b 
       //               { n > 1, F(n-2) + F(n-1) 
       // for some initial integral values a and b. 
       // If the sequence is initialized F(0) = 1, F(1) = 1, 
       // then this relation produces the well-known Fibonacci 
       // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 
       // Initialize a Fibonacci relation sequence 
       // such that F(0) = a, F(1) = b. 
       // This function must be called before any other function. 
       extern 
       "C" 
       MATHLIBRARY_API 
       void 
       fibonacci_init 
       ( 
       const 
       unsigned 
       long 
       long 
       a 
       , 
       const 
       unsigned 
       long 
       long 
       b 
       ); 
       // Produce the next value in the sequence. 
       // Returns true on success and updates current value and index; 
       // false on overflow, leaves current value and index unchanged. 
       extern 
       "C" 
       MATHLIBRARY_API 
       bool 
       fibonacci_next 
       (); 
       // Get the current value in the sequence. 
       extern 
       "C" 
       MATHLIBRARY_API 
       unsigned 
       long 
       long 
       fibonacci_current 
       (); 
       // Get the position of the current value in the sequence. 
       extern 
       "C" 
       MATHLIBRARY_API 
       unsigned 
       fibonacci_index 
       (); 
      
    • นี่เป็นโค้ดตัวอย่างจากเว็บช่วยเหลือของ Microsoft
  9. ทำได้โดยคลิก Add New Item ใน “Project” ในแถบเมนู
    • เลือก “ Visual C++ ” ในเมนูทางซ้ายของ dialog box
    • เลือก “ C++ File (.cpp) ” ตรงกลาง dialog box
    • พิมพ์ชื่อ “MathLibrary.cpp” ในช่อง name ล่างตัวเลือกเมนูต่างๆ
    • คลิก Add เพื่อสร้างไฟล์ว่าง
    •  // MathLibrary.cpp : Defines the exported functions for the DLL. 
       #include 
       "stdafx.h" // use pch.h in Visual Studio 2019 
        
       #include 
       <utility> 
        
       #include 
       <limits.h> 
        
       #include 
       "MathLibrary.h" 
        
       // DLL internal state variables: 
       static 
       unsigned 
       long 
       long 
       previous_ 
       ; 
       // Previous value, if any 
       static 
       unsigned 
       long 
       long 
       current_ 
       ; 
       // Current sequence value 
       static 
       unsigned 
       index_ 
       ; 
       // Current seq. position 
       // Initialize a Fibonacci relation sequence 
       // such that F(0) = a, F(1) = b. 
       // This function must be called before any other function. 
       void 
       fibonacci_init 
       ( 
       const 
       unsigned 
       long 
       long 
       a 
       , 
       const 
       unsigned 
       long 
       long 
       b 
       ) 
       { 
       index_ 
       = 
       0 
       ; 
       current_ 
       = 
       a 
       ; 
       previous_ 
       = 
       b 
       ; 
       // see special case when initialized 
       } 
       // Produce the next value in the sequence. 
       // Returns true on success, false on overflow. 
       bool 
       fibonacci_next 
       () 
       { 
       // check to see if we'd overflow result or position 
       if 
       (( 
       ULLONG_MAX 
       - 
       previous_ 
       < 
       current_ 
       ) 
       || 
       ( 
       UINT_MAX 
       == 
       index_ 
       )) 
       { 
       return 
       false 
       ; 
       } 
       // Special case when index == 0, just return b value 
       if 
       ( 
       index_ 
       > 
       0 
       ) 
       { 
       // otherwise, calculate next sequence value 
       previous_ 
       += 
       current_ 
       ; 
       } 
       std 
       :: 
       swap 
       ( 
       current_ 
       , 
       previous_ 
       ); 
       ++ 
       index_ 
       ; 
       return 
       true 
       ; 
       } 
       // Get the current value in the sequence. 
       unsigned 
       long 
       long 
       fibonacci_current 
       () 
       { 
       return 
       current_ 
       ; 
       } 
       // Get the current index position in the sequence. 
       unsigned 
       fibonacci_index 
       () 
       { 
       return 
       index_ 
       ; 
       } 
      
    • นี่เป็นโค้ดตัวอย่างจากเว็บช่วยเหลือของ Microsoft
  10. ปกติจะอยู่เหนือพื้นที่ project (Windows) หรือยาวไปตามด้านบนของหน้าจอ (Macs)
  11. คลิกแล้วจะเห็นข้อความประมาณด้านล่างนี้
       1 
       >------ 
       Build 
       started 
       : 
       Project 
       : 
       MathLibrary 
       , 
       Configuration 
       : 
       Debug 
       Win32 
       
      1 > MathLibrary . cpp 1 > dllmain . cpp 1 > Generating Code ... 1 > Creating library C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . lib and object C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . exp 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . dll 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . pdb ( Partial PDB ) ========== Build : 1 succeeded , 0 failed , 0 up - to - date , 0 skipped ==========
    • ถ้าสร้างไฟล์ DLL สำเร็จ จะขึ้นตรงนี้ แต่ถ้า error ก็จะขึ้นบอกให้แก้ไข [1]
    โฆษณา

เกี่ยวกับวิกิฮาวนี้

มีการเข้าถึงหน้านี้ 3,748 ครั้ง

บทความนี้เป็นประโยชน์กับคุณไหม

โฆษณา