Select the product you need help with
วิธีการใช้รีจิสทรี API เพื่อบันทึก และรับการตั้งค่าหมายเลขบทความ (Article ID): 145679 - ผลิตภัณฑ์ที่เกี่ยวข้องในบทความนี้ เนื้อหาบนหน้านี้สรุปใช้แม้ว่า Visual Basic รวมฟังก์ชัน SaveSetting และ GetSetting เพื่อบันทึก และดึงข้อมูลจากรีจิสทรี ฟังก์ชันเหล่านี้เท่านั้นงานในส่วนเฉพาะของรีจิสทรี การตั้งค่าโปรแกรม VBA ของคีย์หลัก HKEY_CURRENT_USER และ Visual Basic บทความนี้สรุปการใช้รุ่น 32 บิต Windows API ฟังก์ชัน ซึ่งสามารถใช้การตั้งค่า และเรียกค่าจากที่อื่น ในรีจิสทรี หัวข้อและฟังก์ชันการอ้างอิงในบทความนี้สามารถถูก generalized รีจิสทรีโปรแกรมแบบ 16 บิต ฟังก์ชัน API แบบ 32 บิตรวมยังสนับสนุนการรักษาความปลอดภัย ถึงแม้ว่าภาพรวมของการรักษาความปลอดภัยที่อยู่นอกขอบเขตของบทความนี้ หมายเหตุ: การฟังก์ชัน SaveSetting และ GetSetting จะไม่เป็นส่วนหนึ่งของไลบรารีของฟังก์ชัน VBA อย่างไรก็ตาม โค้ดตัวอย่างด้านล่างยังคงใช้กับโปรแกรมประยุกต์แบบ 32 บิตที่ใช้งาน VBA ข้อมูลเพิ่มเติมข้อมูลรีจิสทรีทั่วไปรีจิสทรีถูกใช้ โดยโปรแกรมประยุกต์และ Windows จะเก็บข้อมูลการกำหนดค่า การแทนที่สำหรับตัวเลขที่มีขนาดใหญ่ของแฟ้ม INI ที่ proliferated บนเครื่อง 3.x ของ Windows และยังใช้ heavily OLE ได้รีจิสทรีถูกจัดโดยใช้ชุดตามลำดับชั้นของคีย์และค่า resembling แบบแผนภูมิ แต่ละคีย์ ขึ้นต้น ด้วยคีย์รากที่กำหนดไว้ล่วงหน้า 6 อย่างใดอย่างหนึ่งสามารถมี sub-keys และค่าที่เกี่ยวข้องกับแฟ้มนั้น คีย์มีหน่วยองค์กร และตั้งชื่อ และปรากฏในตัวแก้ไขรีจิสทรีของ Windows เป็นโฟลเดอร์แฟ้ม ค่ามีข้อมูลรายการ และปรากฏเป็นรายการข้อความในบานหน้าต่างด้านขวาของหน้าต่างตัวแก้ไขรีจิสทรี คีย์ไม่จำเป็นต้องมีค่าใดที่เกี่ยวข้อง แต่อาจมีหลายอย่าง แต่ละค่ามีชนิดข้อมูลที่เกี่ยวข้อง ชนิดข้อมูลรีจิสทรีที่ใช้บ่อยที่สุดที่สองจะ REG_SZ สตริ งที่จบการทำงาน null และ REG_DWORD หมายเลขรุ่น 32 บิต กระบวนการพื้นฐานที่ใช้ในการเขียน หรืออ่านจากตำแหน่งที่ตั้งในรีจิสทรีจะเหมือนกัน การอ้างอิงคีย์ที่กำหนดหรือค่าใด ๆ คุณต้องมีหมายเลขอ้างอิงคีย์ เมื่อมีการขอรับหมายเลขอ้างอิงนี้ ค่าและ sub-keys ของคีย์ที่หมายเลขอ้างอิงนี้อ้างอิงถึงสามารถอ่าน เซ็ต หรืออยู่ในรายการ (ถูกระบุ) กำหนดตำแหน่งที่ตั้งในรีจิสทรี เพื่อขอรับหมายเลขอ้างอิงคีย์นั้น คุณต้องเริ่มต้น ด้วยหกกำหนดไว้ล่วงหน้าแป้น (HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG และ HKEY_DYN_DATA) อย่างใดอย่างหนึ่ง และดำเนินกับแผนภูมิของรีจิสทรีจนถึงคีย์ที่ต้องการ User programs most often read and write from HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE. If the keys being traversed exist already, you can use a series of calls to the RegOpenKey or RegOpenKeyEx functions. If the keys need to be created, the RegCreateKey and RegCreateKeyEx functions do the job. With the handle to the desired key, the functions used to list, set, and retrieve information can be called. In all cases, the functions with the Ex suffix will work only on 32-bit platforms. Functions without the suffix may work on both 16-bit and 32-bit versions of Windows. Keep in mind that not all registry functions lacking the 'Ex' suffix are functions provided for 16-bit compatibility. The Ex suffix was only added when the capabilities of 16-bit functions were expanded. Functions that are totally new and specific to 32-bit platforms do not possess an Ex extension. The RegSetValue and RegSetValueEx functions allow the settings of a value to be modified, while RegQueryValue and RegQueryValueEx retrieve the current setting of a value. The limitations of the non-Ex, 16-bit versions of these APIs are very evident here. When using the 16-bit RegSetValue function there is no way to name a value, and because of this, RegSetValue can't be used to associate more than one value with each key. In addition, all values written with RegSetValue have a data type of REG_SZ. These limitations are inherent with the 16-bit Registry. RegSetValueEx allows the creation of a multiple number of values with any available data type. How to Write to a Specific Registry LocationAfter determining what functions you will need to use for your project, copy the relevant declares from the code at the end of this article to a basic module. The two Visual Basic procedures included (SetValueEx and QueryValueEx) are wrappers for the RegSetValueEx and RegQueryValueEx API functions and greatly simplify their use. The notes below make use of these Visual Basic functions; however, you are free to make calls directly to the API if you wish.Creating/Modifying Keys and Values: With the declarations and procedures available, you can create and open keys, and add, modify, and read values. The three following sections explain how to create a key, set or modify a value, and query a value. Creating a New Key: Creating a new key is as simple as using the following procedure. CreateNewKey takes the name of the key to create, and the constant representing the predefined key to create the key under. The call to RegCreateKeyEx doesn't take advantage of the security mechanisms allowed, but could be modified to do so. A discussion of Registry security is outside the scope of this article. With this procedure a call of: will create a key called TestKey immediately under HKEY_LOCAL_MACHINE. Calling CreateNewKey like this: will create three-nested keys beginning with TestKey immediately under HKEY_CURRENT_USER, SubKey1 subordinate to TestKey, and SubKey3 under SubKey2. Setting/Modifying a Value: Creating and setting a value of a specified key can be accomplished with the following short procedure. SetKeyValue takes the key that the value will be associated with, the name of the value, the setting of the value, and the type of the value (the SetValueEx function only supports REG_SZ and REG_DWORD, but this can be modified if necessary). Specifying a new value for an existing sValueName will modify the current setting of that value. A call of: will create a value of type REG_SZ called "StringValue" with the setting of "Hello." This value will be associated with the key SubKey1 of "TestKey." In this case, "TestKey" is a subkey of HKEY_CURRENT_USER, but this can be modified by changing the call to RegOpenKeyEx. This call will fail if "TestKey\SubKey1" does not exist. To avoid this problem, use a call to RegCreateKeyEx instead of a call to RegOpenKeyEx. RegCreateKeyEx will open a specified key if it already exists. Querying a Value: The next procedure can be used to ascertain the setting of an existing value. QueryValue takes the name of the key and the name of a value associated with that key and displays a message box with the corresponding value. It uses a call to the QueryValueEx wrapper function defined below, that only supports REG_SZ and REG_DWORD types. With this procedure, a call of: will display a message box with the current setting of the "StringValue" value, and assumes that "StringValue" exists in the "TestKey\SubKey1" key. If the Value that you query does not exist then QueryValue will return an error code of 2 - 'ERROR_BADKEY'. Additional Notes: The above examples use the extended 32-bit versions of the registry functions exclusively. These functions allow more than one value to be associated with each key. As discussed above, the 16-bit RegSetValue and RegQueryValue act on a single value associated with the current key (which is always of the type REG_SZ). These functions appear in the 32-bit Registry Editor with a name of <no name="">. To set, modify, or query this special associated value, one must use the 16-bit registry functions. Reading and writing from the registry in a 16-bit environment is much simpler than in a 32-bit environment. The same basic procedure is followed: open a key and get a handle and then call your modification function with that handle, but no consideration needs to be made for multiple associated values or for different value data types. A 16-bit application can create and modify keys and values with the declarations of the RegCreateKey, RegOpenKey, RegQueryValue, RegSetValue, and RegCloseKey functions. </no> In some cases, there is no need for any values to be associated with a key. An application may only need to know if a certain key or value exists, and not care about the nature of the key's values. In a situation like this, the RegEnumKey, RegEnumKeyEx, and RegEnumValue functions can be used to determine whether a certain key or value exists. For more information on these functions refer to the API Text Viewer and/or Windows API reference. API Function and Constant DeclarationsSetValueEx and QueryValueEx Wrapper Functions: ข้อมูลอ้างอิงProgramming the Windows 95 User Interface, Chapter 10 - "Using the
Registry" For function references: Any guide to the Win16 or Win32 API. คุณสมบัติหมายเลขบทความ (Article ID): 145679 - รีวิวครั้งสุดท้าย: 6 มกราคม 2554 - Revision: 4.0 ใช้กับ
แปลโดยคอมพิวเตอร์ ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:145679
(http://support.microsoft.com/kb/145679/en-us/
)
| การแปลบทความ
|




กลับไปด้านบน








