de_cuong_ltw

34
BỘ GIAO THÔNG VẬN TẢI TRƯỜNG ĐẠI HỌC HÀNG HẢI VIỆT NAM KHOA CÔNG NGHỆ THÔNG TIN TÀI LIỆU HỌC TẬP HỌC PHẦN: LẬP TRÌNH WINDOWS Hải Phòng, ngày 18/11/2011

Transcript of de_cuong_ltw

Page 1: de_cuong_ltw

BỘ GIAO THÔNG VẬN TẢI

TRƯỜNG ĐẠI HỌC HÀNG HẢI VIỆT NAM

KHOA CÔNG NGHỆ THÔNG TIN

TÀI LIỆU HỌC TẬP

HỌC PHẦN: LẬP TRÌNH WINDOWS

Hải Phòng, ngày 18/11/2011

Page 2: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

1

MỤC LỤC

I. HƯỚNG DẪN SỬ DỤNG VISUAL STUDIO 2010 ............................................................................... 2

1.1. Lập trình bằng giao diện (win32) hay bằng các dòng lệnh (Console) ? .................. 2

1.2. Tạo dự án mới. .......................................................................................................................................... 2

1.3. Sự khác biệt giữa VS2010 với các công cụ khác ................................................................... 6

1.4. Chạy nhiều chương trình trong cùng 1 dự án ....................................................................... 7

II. LẬP TRÌNH VỀ FILE VÀ THƯ MỤC .......................................................................................................... 8

2.1. Tạo File (CreateFileA) ............................................................................................................................ 8

Cú pháp: ....................................................................................................................................................................... 8

2.1.1. Tạo File để đọc .................................................................................................................................... 9

2.1.2. Tạo File để ghi .................................................................................................................................. 10

2.2. Sao chép File (CopyFileA) .................................................................................................................. 11

2.3. Xóa File (DeleteFileA).......................................................................................................................... 12

2.4. Di chuyển File và Thư mục (MoveFileA) ................................................................................... 12

2.5. Lấy thuộc tính tệp hoặc thư mục (GetFileAttributesA) ................................................... 13

2.6. Thiết lập thuộc tính cho tệp hoặc thư mục (SetFileAttributesA) ............................... 14

2.7. Tạo thư mục (CreateDirectoryA) ................................................................................................. 14

2.8. Xóa thư mục (RemoveDirectoryA) .............................................................................................. 15

2.9. Xác định thư mục hiện thời (GetCurrentDirectoryA) ....................................................... 15

2.10. Thiết lập đường dẫn mới cho thư mục (SetCurrentDirectoryA) .......................... 16

BÀI TẬP: .................................................................................................................................................................... 16

III. LẬP TRÌNH REGISTRY ............................................................................................................................ 23

3.1. Mở khóa (RegOpenKeyA) .................................................................................................................. 23

3.2. Tạo khóa (RegCreateKeyA) ............................................................................................................. 24

3.3. Xóa một khóa (RegDeleteKeyA) .................................................................................................... 25

3.4. Đặt giá trị mặc định cho khóa con Default (RegSetValueA) ......................................... 26

3.5. Lấy giá trị trong khóa (RegGetValueA) .................................................................................... 27

3.6. Thêm/Sửa giá trị một khóa (RegSetKeyValueA) ................................................................ 29

BÀI TẬP: .................................................................................................................................................................... 30

Page 3: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

2

I. HƯỚNG DẪN SỬ DỤNG VISUAL STUDIO 2010

Để lập trình WinDows có nhiều công cụ như Visual C++ 6.0, DevC, Cfree. Nhưng để

dễ lập trình và nhất là việc phát hiện lỗi trong quá trình làm việc thì ta nên dùng Visual

Studio các phiên bản từ năm 2003 trở lên, đặc biệt nên dùng bản 2010. Dù lập trình trên

phiên bản nào đi nữa thì cùng một mã nguồn có thể chạy trên nhiều công cụ khác nhau. Nếu

có sự khác biệt nào giữa VS2010 với các công cụ khác thì mình sẽ nêu ở phía dưới!

1.1. Lập trình bằng giao diện (win32) hay bằng các dòng lệnh (Console) ?

Nếu bạn chọn lập trình bằng giao diện thì sẽ hơi phức tạp chút, nhưng vì thời gian có

hạn nên bản đầu tiên của tài liệu sẽ không đề cập đến lập trình bằng giao diện, mà để đơn

giản chúng ta sẽ lập trình bằng dòng (Console). Làm như thế nào thì bạn hãy đọc phần tiếp

theo.

1.2. Tạo dự án mới.

- Vào File -> New -> Project…

- Chọn Visual C++ -> Win 32 -> Đặt tên dự án vào ô Name, nơi lưu trữ dự án vào

Location. Chọn Win32 Console Application -> OK.

Page 4: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

3

- Chọn Next

- Chọn Console application -> Empty project -> Finish

Page 5: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

4

- Cửa sổ mở ra, bạn chọn tab Solution Explorer ở bên phải màn hình, click đúp

chuột vào tên dự án (trong hình ảnh dưới tên dự án là “Huong_dan2”) nó sẽ xổ xuống

những thư mục có trong dự án

- Click phải vào thư mục Source Files -> Add -> New Item… để tạo file mã nguồn

- Chọn C++ File (.cpp) -> Đặt tên File mã nguồn vào Name -> Add

Page 6: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

5

- Sau khi tạo xong sẽ có 1 tệp tin ở trong thư mục Source Files và tệp đó được mở

trắng, nơi đó là nơi ta viết code.

- Bạn gõ đoạn mã như phía dưới vào cửa sổ

- Nhấn nút trên thanh công cụ để chạy chương trình

- Và đây là kết quả sau khi chạy chương trình.

Page 7: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

6

Như vậy việc gõ mã lệnh trong lập trình Win tương đương như lập trình C, do vậy

nếu bạn chưa nắm vững ngôn ngữ C sẽ rất khó để học môn này.

1.3. Sự khác biệt giữa VS2010 với các công cụ khác

Sự khác biệt mình muốn nói tới không phải về mặt giao diện hay cách gõ lệnh ra sao,

mà là sự khác biệt về kiểu dữ liệu và tên hàm.

Để sử dụng các hàm trong VS2010 giống như các công cụ khác, nếu trong hàm đó có

truyền tham số kiểu xâu thì trong VS2010 phải sử dụng kiểu TCHAR trong khi các công cụ

khác lại là kiểu char.

Ví dụ ta có hàm tạo File (CreateFile) như sau:

Tại đây LPCWSTR lpFileName là tên file đầy đủ (có chứa đường dẫn, ví dụ

C:\bai1.txt). Trong các công cụ khác thì ta gõ “C:\bai1.txt” nhưng trong VS2010 để sử dụng

được hàm này ta phải gõ L”C:\bai1.txt” hoặc TEXT(“C:\bai1.txt”).

Lỗi khi gõ xâu kiểu char trong VS2010

Sử dụng hàm CreateFileA để khắc phục

Page 8: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

7

Để khắc phục vấn đề này ta sẽ sử dụng hàm CreateFileA (có chữ A cuối cùng của tên

hàm ban đầu), khi đó bạn có thể gõ “C:\bai1.txt” mà không phát sinh lỗi!

1.4. Chạy nhiều chương trình trong cùng 1 dự án

Trong trường hợp bạn cần làm nhiều bài tập lập trình Win, thay vì bạn sẽ tạo mới dự

án và thêm 1 bài thì bạn có thể thêm bài đó vào dự án đã có sẵn. bằng cách:

- Click phải vào tên bài tập cũ -> Remove

- Chọn Remove (nếu chọn Delete thì bài tập cũ sẽ bị xóa khỏi thư mục dự án)

- Sau đó bạn thêm mới một tệp mã nguồn vào dự án

Page 9: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

8

II. LẬP TRÌNH VỀ FILE VÀ THƯ MỤC

2.1. Tạo File (CreateFileA)

Cú pháp:

HANDLE WINAPI CreateFile(

LPCTSTR lpFileName,

DWORD dwDesiredAccess,

DWORD dwShareMode,

LPSECURITY_ATTRIBUTES lpSecurityAttributes,

DWORD dwCreationDisposition,

DWORD dwFlagsAndAttributes,

HANDLE hTemplateFile

);

Trong đó:

Kiểu dữ liệu Giải thích

LPCTSTR lpFileName

Tên tệp tin cần đọc, đầy đủ cả đường dẫn

tới tệp tin đó. Ví dụ C:\\test.txt (là “\\”,

không phải “\”)

DWORD dwDesiredAccess Kiểu truy nhập – để đọc hay ghi, nếu là đọc:

GENERIC_READ, là ghi: GENERIC_WRITE

Page 10: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

9

DWORD dwShareMode

Kiểu mở file (đọc,ghi hay cả 2),có thể là:

FILE_SHARE_DELETE,

FILE_SHARE_READ,

FILE_SHARE_WRITE

LPSECURITY_ATTRIBUTES

lpSecurityAttributes Thường là NULL

DWORD dwCreationDisposition

Thuộc tính khi mở để đọc hay ghi:

CREATE_ALWAYS (Luôn luôn tạo – dùng

cho tạo file,nếu đã có file thì file mới sẽ ghi

đè file cũ),

CREATE_NEW (Tạo file mới, nếu file cần

tạo đã tồn tại thì hàm này không thực hiện

được)

OPEN_ALWAYS (Đọc file, nếu file cần đọc

chưa có thì nó sẽ được tạo bằng hàm

CREATE_NEW như trên)

OPEN_EXISTING (Chỉ mở nếu file cần đọc

đã tồn tại)

DWORD dwFlagsAndAttributes

Thuộc tính file được mở, chúng có thể là:

FILE_ATTRIBUTE_ARCHIVE,

FILE_ATTRIBUTE_HIDDEN,

FILE_ATTRIBUTE_READONLY

HANDLE hTemplateFile Thường là NULL

2.1.1. Tạo File để đọc

Ví dụ: Mở File test.txt trong ổ C (C:\test.txt) và đọc nội dung đó, in nội dung ra ngoài màn

hình:

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { HANDLE hFile=CreateFileA( "C:\\test.txt", //Tên file (kèm cả đường dẫn) GENERIC_READ, //Mở để đọc FILE_SHARE_READ, //Chế độ chia sẻ NULL, //Chế độ bảo mật OPEN_EXISTING, //Mở nếu file đã tồn tại FILE_ATTRIBUTE_NORMAL, //File bình thường NULL); //no attr.template if(hFile==INVALID_HANDLE_VALUE) { printf("Khong the mo file!"); getch(); return 1;

Page 11: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

10

} //Khai báo xâu để lưu nội dung file và 1 biến lưu kích thước nội dung đó DWORD kich_thuoc_noi_dung; char noi_dung[500]; //1 xâu tối đa 500 ký tự if(!ReadFile(hFile,noi_dung,500,&kich_thuoc_noi_dung,NULL)) { printf("Khong the doc file nay"); getch(); return 1; } noi_dung[kich_thuoc_noi_dung]='\0'; printf("Noi dung: \n%s",noi_dung); getch(); return 0; }

Kết quả chương trình:

Như vậy để đọc 1 file ta cần các bước:

- Khai báo 1 biến HANDLE,

- Kiểm tra xem tệp tin đó tồn tại hay không.

- Nếu tồn tại thì đọc file, thực hiện bằng ReadFile, trong hàm phải có nội dung được

lưu trữ và kích thước nội dung đó.

- In nội dung ra màn hình.

2.1.2. Tạo File để ghi

Ví dụ: Tạo 1 File tên là test.txt trong ổ D:\hoc tap và ghi nội dung vào file đó.

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { HANDLE hFile=CreateFileA( "D:\\hoc tap\\test.txt", //Tên file (kèm cả đường dẫn) GENERIC_WRITE, //Mở để đọc 0, //Chế độ chia sẻ NULL, //Chế độ bảo mật CREATE_ALWAYS, //Mở nếu file đã tồn tại FILE_ATTRIBUTE_NORMAL, //File bình thường NULL); //no attr.template if(hFile==INVALID_HANDLE_VALUE) { printf("Khong the tao file!"); getch(); return 1; } //Khai báo xâu để lưu nội dung file và 1 biến lưu kích thước nội dung đó DWORD kich_thuoc_noi_dung; char noi_dung[500]; //1 xâu tối đa 500 ký tự strcpy(noi_dung,"Chao bao\nXin chao ban\nBan co thay toi viet gi ko?\tthay kho?"); if(!WriteFile(hFile,noi_dung,strlen(noi_dung),&kich_thuoc_noi_dung,NULL))

Page 12: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

11

{ printf("Khong the ghi file nay"); getch(); return 1; } printf("Thanh cong!"); getch(); return 0; }

2.2. Sao chép File (CopyFileA)

Cú pháp:

BOOL WINAPI CopyFile(

LPCTSTR lpExistingFileName,

LPCTSTR lpNewFileName,

BOOL bFailIfExists

);

Trong đó:

Tên hàm Giải thích

LPCTSTR lpExistingFileName Đường dẫn tới file nguồn . VD: C:\\bai1\\test.txt

LPCTSTR lpNewFileName Đường dẫn tới file đích. VD: D:\\bai1\\test.txt

BOOL bFailIfExists Mặc định FALSE

Ví dụ: sao chép 1 file từ ổ C sang ổ D, đồng thời đổi tên file đó

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { if(CopyFileA("C:\\1.txt","D:\\2.txt",FALSE)) printf("Thanh cong!"); else printf("That bai!"); getch(); return 0; }

Sau khi sao chép, tệp tin 1.txt sẽ được đổi thành 2.txt

Page 13: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

12

2.3. Xóa File (DeleteFileA)

Cú pháp:

BOOL WINAPI DeleteFile(

__in LPCTSTR lpFileName

);

Trong đó LPCTSTR lpFileName là tên File có đường dẫn đầy đủ

Ví dụ: Xóa File 1.txt trong ổ C:\

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { DeleteFileA("C:\\1.txt"); getch(); return 0; }

2.4. Di chuyển File và Thư mục (MoveFileA)

Cú pháp:

BOOL WINAPI MoveFile(

__in LPCTSTR lpExistingFileName,

__in LPCTSTR lpNewFileName

);

Trong đó:

LPCTSTR lpExistingFileName Tên File (hoặc thư mục) cũ có đường dẫn đầy đủ

LPCTSTR lpNewFileName Tên File (hoặc thư mục) mới có đường dẫn đầy đủ

Ví dụ: Di chuyển File 2.txt trong ổ D:\ sang C:\Program Files, đồng thời đổi tên file thành

3.txt

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { MoveFileA("D:\\2.txt","C:\\Program Files\\3.txt"); getch(); return 0; }

Page 14: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

13

Lưu ý: Nếu di chuyển thư mục thì hàm này chỉ có tác dụng với các thư mục cùng phân vùng,

ví dụ MoveFileA("C:\\Program Files","D:\\Program Files"); sẽ không thực hiện được,

MoveFileA("C:\\Program Files","C:\\thu muc\\Program Files"); sẽ thực hiện được

2.5. Lấy thuộc tính tệp hoặc thư mục (GetFileAttributesA)

Cú pháp:

DWORD WINAPI GetFileAttributes(

__in LPCTSTR lpFileName

);

Trong đó LPCTSTR lpFileName là tên tệp tin (hoặc thư mục) có đường dẫn đầy đủ.

Kiểu trả về của hàm là DWORD, đó là thuộc tính của File, gồm:

FILE_ATTRIBUTE_ARCHIVE (tệp tin và thư mục).

FILE_ATTRIBUTE_COMPRESSED (tệp tin nén).

FILE_ATTRIBUTE_DEVICE.

FILE_ATTRIBUTE_DIRECTORY (kiểu thư mục).

FILE_ATTRIBUTE_ENCRYPTED.

FILE_ATTRIBUTE_HIDDEN (tệp tin ẩn).

FILE_ATTRIBUTE_NORMAL (tệp tin bình thường).

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED.

FILE_ATTRIBUTE_OFFLINE.

FILE_ATTRIBUTE_READONLY (tệp tin chỉ đọc).

FILE_ATTRIBUTE_REPARSE_POINT.

FILE_ATTRIBUTE_SPARSE_FILE.

FILE_ATTRIBUTE_SYSTEM.

FILE_ATTRIBUTE_TEMPORARY.

FILE_ATTRIBUTE_VIRTUAL.

Ví dụ: lấy thuộc tính của tệp 1.txt trong ổ C:\

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { DWORD Att=GetFileAttributesA("C:\\1.txt"); if(Att & FILE_ATTRIBUTE_READONLY) printf("Readonly\n"); if(Att & FILE_ATTRIBUTE_HIDDEN)

Page 15: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

14

printf("Hidden\n"); if(Att & FILE_ATTRIBUTE_NORMAL) printf("Normal\n"); /* Các trường hợp còn lại tương tự */ getch(); return 0; }

2.6. Thiết lập thuộc tính cho tệp hoặc thư mục (SetFileAttributesA)

Cú pháp:

BOOL WINAPI SetFileAttributes(

__in LPCTSTR lpFileName,

__in DWORD dwFileAttributes

);

Trong đó:

LPCTSTR lpFileName Tên File hoặc thư mục (gồm đầy đủ đường dẫn)

DWORD dwFileAttributes Thuộc tính mới (gồm các thuộc tính đã liệt kê ở hàm trên)

Ví dụ: Thiết lập thuộc tính ẩn cho tệp 1.txt trong ổ C:\.

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { SetFileAttributesA("C:\\1.txt",FILE_ATTRIBUTE_HIDDEN); getch(); return 0; }

2.7. Tạo thư mục (CreateDirectoryA)

Cú pháp:

BOOL WINAPI CreateDirectory(

__in LPCTSTR lpPathName,

__in LPSECURITY_ATTRIBUTES lpSecurityAttributes

);

Trong đó:

LPCTSTR lpPathName Tên thư mục đầy đủ đường dẫn

LPSECURITY_ATTRIBUTES lpSecurityAttributes Thuộc tính bảo mật, thường đặt NULL

Ví dụ: Tạo thư mục bai1 trong C:\

Page 16: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

15

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { CreateDirectoryA("C:\Bai1",NULL); getch(); return 0; }

2.8. Xóa thư mục (RemoveDirectoryA)

Cú pháp:

BOOL WINAPI RemoveDirectory(

__in LPCTSTR lpPathName

);

Trong đó: LPCTSTR lpPathName là tên thư mục cần xóa, gồm đường dẫn đầy đủ.

Ví dụ: Xóa thư mục bai1 trong ổ C:\

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { RemoveDirectoryA("C:\\Bai1"); getch(); return 0; }

2.9. Xác định thư mục hiện thời (GetCurrentDirectoryA)

Cú pháp:

DWORD WINAPI GetCurrentDirectory(

__in DWORD nBufferLength,

__out LPTSTR lpBuffer

);

Trong đó:

DWORD nBufferLength Kích thước tên thư mục hiện thời

LPTSTR lpBuffer Tên thư mục hiện thời

Ví dụ: Xác định thư mục hiện thời

#include <Windows.h> #include <conio.h> #include <stdio.h>

Page 17: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

16

int main() { char ten_thu_muc[255]; //biến lưu tên thư mục GetCurrentDirectoryA(255,ten_thu_muc); printf("Thu muc hien tai: \n%s",ten_thu_muc); getch(); return 0; }

2.10. Thiết lập đường dẫn mới cho thư mục (SetCurrentDirectoryA)

Cú pháp:

BOOL WINAPI SetCurrentDirectory(

__in LPCTSTR lpPathName

);

Trong đó:

LPCTSTR lpPathName là đường dẫn mới thiết đặt cho thư mục hiện thời

Ví dụ: Thiết đặt đường dẫn mới cho thư mục hiện thời ở C:\

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { char ten_thu_muc[255]; DWORD tc=GetCurrentDirectoryA(255,ten_thu_muc); printf("Hien tai: %s\n",ten_thu_muc); if(!SetCurrentDirectoryA("C:\\Bai1\\")) printf("False"); else { printf("Thanh cong!\nDuong dan moi: "); GetCurrentDirectoryA(255,ten_thu_muc); printf("%s\n",ten_thu_muc); } getch(); return 0; }

BÀI TẬP:

1. Liệt kê c|c File(s) v{ thư mục trong thư mục gốc (liệt kê cả những Files v{ thư

mục bên trong thư mục con)

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. void hien_thi(char tmA[200],int cap) 6. { 7. //Copy toan bo file trong thu muc nguon vao thu muc dich 8. WIN32_FIND_DATAA FindFileData;

Page 18: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

17

9. char tmAcopied[200]; 10. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 11. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData); 12. if(hFind!=INVALID_HANDLE_VALUE) 13. do 14. { 15. strcpy(tmAcopied,tmA); 16. if(FindFileData.cFileName[0]!='.') 17. { 18. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 19. { 20. for(int i=0;i<cap;i++) 21. printf("_"); 22. printf("%s <DIR>\n",FindFileData.cFileName); 23. strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName); 24. hien_thi(tmAcopied,cap+1); 25. } 26. else 27. { 28. for(int i=0;i<cap;i++) 29. printf(" "); 30. printf("%s\n",FindFileData.cFileName); 31. } 32. } 33. } 34. while(FindNextFileA(hFind,&FindFileData)); 35. else 36. printf("Loi duong dan!"); 37. FindClose(hFind); 38. } 39. int main() 40. { 41. char a[200]; 42. printf("Nhap duong dan thu muc A: "); 43. gets(a); 44. 45. int cap=0; //Cấp thư mục 46. hien_thi(a,cap); 47. //Tao thu muc tai B truoc khi sao chep 48. printf("\nKet thuc!"); 49. getch(); 50. return 0; 51. } 52.

2. Copy tất cả c{ Files trong thư mục A sang thư mục B3

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. void Sao_Chep(char tmA[200],char tmB[200]) 6. { 7. //Copy toan bo file trong thu muc nguon vao thu muc dich 8. WIN32_FIND_DATAA FindFileData; 9. char tmAcopied[200]; 10. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 11. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData); 12. do 13. { 14. strcpy(tmAcopied,tmA); 15. char tmB2[200]; 16. strcpy(tmB2,tmB); 17. if(FindFileData.cFileName[0]!='.') 18. { 19. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

Page 19: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

18

20. { 21.

CreateDirectoryA(strcat(strcat(tmB2,"\\"),FindFileData.cFileName),NULL); 22. 23.

Sao_Chep(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName),tmB2); 24. 25. } 26. else 27.

CopyFileA(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName), 28. strcat(strcat(tmB2,"\\"),FindFileData.cFileName),FALSE); 29. printf("%s -> %s\n",tmAcopied,tmB2); 30. } 31. } 32. while(FindNextFileA(hFind,&FindFileData)); 33. } 34. int main() 35. { 36. char a[200],b[200]; 37. printf("Nhap duong dan thu muc A: "); 38. gets(a); 39. printf("Nhap duong dan thu muc B: "); 40. gets(b); 41. 42. Sao_Chep(a,b); 43. getch(); 44. return 0; 45. } 46.

3. Di chuyển thư mục A sang thư mục B

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. void lay_ten_thu_muc(char ten_duong_dan[200], char ten_thu_muc[200]) 6. { 7. int i=strlen(ten_duong_dan)-1; 8. int j=0; 9. while(ten_duong_dan[i]!='\\') 10. { 11. ten_thu_muc[j]=ten_duong_dan[i]; 12. i--; 13. j++; 14. } 15. ten_thu_muc[j]=NULL; 16. for(j=0;j<strlen(ten_thu_muc)/2;j++) 17. { 18. char a=ten_thu_muc[j]; 19. ten_thu_muc[j]=ten_thu_muc[strlen(ten_thu_muc)-1-j]; 20. ten_thu_muc[strlen(ten_thu_muc)-1-j]=a; 21. } 22. } 23. 24. void Cut(char tmA[200],char tmB[200]) 25. { 26. //Copy toan bo file trong thu muc nguon vao thu muc dich 27. WIN32_FIND_DATAA FindFileData; 28. char tmAcopied[200]; 29. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 30. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData); 31. do 32. {

Page 20: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

19

33. strcpy(tmAcopied,tmA); 34. char tmB2[200]; 35. strcpy(tmB2,tmB); 36. if(FindFileData.cFileName[0]!='.') 37. { 38. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 39. { 40.

CreateDirectoryA(strcat(strcat(tmB2,"\\"),FindFileData.cFileName),NULL);

41. Cut(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName),tmB2);

42. RemoveDirectoryA(tmAcopied); 43. } 44. else 45.

MoveFileA(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName),strcat(strcat(tmB2,"\\"),FindFileData.cFileName));

46. printf("%s -> %s\n",tmAcopied,tmB2); 47. } 48. } 49. while(FindNextFileA(hFind,&FindFileData)); 50. FindClose(hFind); 51. } 52. int main() 53. { 54. char a[200],b[200]; 55. printf("Nhap duong dan thu muc A: "); 56. gets(a); 57. printf("Nhap duong dan thu muc B: "); 58. gets(b); 59. 60. //Tao thu muc tai B truoc khi sao chep 61. char ten_thu_muc[200],b2[200]; 62. lay_ten_thu_muc(a,ten_thu_muc); 63. strcat(strcat(b,"\\"),ten_thu_muc); 64. CreateDirectoryA(b,NULL); 65. Cut(a,b); 66. RemoveDirectoryA(a); 67. getch(); 68. return 0; 69. }

70.

4. Đếm số Files trong thư mục

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. 6. void dem(char tmA[200],int &a) 7. { 8. //Copy toan bo file trong thu muc nguon vao thu muc dich 9. WIN32_FIND_DATAA FindFileData; 10. char tmAcopied[200]; 11. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 12. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData); 13. do 14. { 15. strcpy(tmAcopied,tmA); 16. if(FindFileData.cFileName[0]!='.') 17. { 18. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 19.

Page 21: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

20

dem(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName),a); 20. else 21. a++; 22. } 23. } 24. while(FindNextFileA(hFind,&FindFileData)); 25. FindClose(hFind); 26. } 27. int main() 28. { 29. char a[200]; 30. printf("Nhap duong dan thu muc A: "); 31. gets(a); 32. 33. int so_file=0; 34. 35. dem(a,so_file); 36. printf("So file: %d",so_file); 37. //Tao thu muc tai B truoc khi sao chep 38. 39. getch(); 40. return 0; 41. } 42.

5. Liệt kê các Files đuôi .dat

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. int ktra(char file[200]) 6. { 7. if(file[strlen(file)-1]=='t' && file[strlen(file)-2]=='a' &&

file[strlen(file)-3]=='d' && file[strlen(file)-4]=='.') 8. return 1; 9. else return 0; 10. } 11. void hien_thi(char tmA[200]) 12. { 13. //Copy toan bo file trong thu muc nguon vao thu muc dich 14. WIN32_FIND_DATAA FindFileData; 15. char tmAcopied[200]; 16. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 17. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData); 18. do 19. { 20. strcpy(tmAcopied,tmA); 21. if(FindFileData.cFileName[0]!='.') 22. { 23. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 24.

hien_thi(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName));

25. else if(ktra(FindFileData.cFileName)) 26. { 27. printf("%s\\%s\n",tmAcopied,FindFileData.cFileName); 28. } 29. } 30. } 31. while(FindNextFileA(hFind,&FindFileData)); 32. FindClose(hFind); 33. } 34. int main() 35. { 36. char a[200];

Page 22: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

21

37. printf("Nhap duong dan thu muc A: "); 38. gets(a); 39. 40. int cap=0; //Cấp thư mục 41. hien_thi(a); 42. //Tao thu muc tai B truoc khi sao chep 43. printf("\nKet thuc tim kiem"); 44. getch(); 45. return 0; 46. } 47.

6. Hiển thị kích thước một File abc.xyz

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. DWORD kich_thuoc(char duong_dan[200]) 5. { 6. DWORD size; 7. WIN32_FIND_DATAA FindFileData; 8. HANDLE hFind=FindFirstFileA(duong_dan,&FindFileData); 9. if(hFind==INVALID_HANDLE_VALUE) 10. return -1; 11. else 12. { 13. size=FindFileData.nFileSizeLow; 14. return size; 15. } 16. } 17. int main() 18. { 19. char duong_dan[200]; 20. printf("Nhap duong dan: "); 21. gets(duong_dan); 22. if(kich_thuoc(duong_dan)!=-1) 23. printf("Kich thuoc file: %d <bytes>",kich_thuoc(duong_dan)); 24. else 25. printf("Sai duong dan"); 26. getch(); 27. return 0; 28. }

29.

7. Tạo một file abc.xyz trong thư mục A v{ ghi v{o đó đoạn văn bản “Cong hoa xa

hoi chu nghia Viet nam”. Sau đó, copy file trên sang thư mục B

1. #include <conio.h> 2. #include <Windows.h> 3. #include <stdio.h> 4. #include <string.h> 5. void tao_ghi_file(char duong_dan[200],char ten_file[100]) 6. { 7. HANDLE hFile=CreateFileA(strcat(strcat(duong_dan,"\\"),ten_file), // tên file 8. GENERIC_WRITE,

// mở để ghi 9. 0,

// không chia sẻ 10. NULL,

// bảo mật mặc định 11. CREATE_ALWAYS,

// ghi đè nếu đã tồn tại

Page 23: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

22

12. FILE_ATTRIBUTE_NORMAL, // tệp tin bình thường

13. NULL); // không thuộc tính, template

14. if(hFile==INVALID_HANDLE_VALUE) 15. { 16. printf("Sai duong dan"); 17. return ; 18. } 19. else 20. { 21. DWORD dwResult; 22. char noi_dung[200]="Cong hoa xa hoi chu nghia Viet Nam"; 23. WriteFile(hFile,noi_dung,strlen(noi_dung),&dwResult,NULL); 24. CloseHandle(hFile); 25. } 26. } 27. int main() 28. { 29. char duong_dan[200]; 30. printf("Nhap duong dan: "); 31. gets(duong_dan); 32. char ten_file[100]; 33. printf("Nhap ten file: "); 34. gets(ten_file); 35. char duong_dan_dich[200]; 36. printf("Nhap duong dan dich: "); 37. gets(duong_dan_dich); 38. 39. tao_ghi_file(duong_dan,ten_file); 40. 41. strcat(strcat(duong_dan_dich,"\\"),ten_file); 42. printf("%s\n",duong_dan); 43. printf("%s\n",duong_dan_dich); 44. if(CopyFileA(duong_dan,duong_dan_dich,FALSE)) 45. printf("Sao chep file thanh cong!"); 46. else 47. printf("Sao chep file that bai!"); 48. 49. getch(); 50. return 0; 51. }

52.

8. Xóa những Files có đuôi .inp

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. #include <tchar.h> 5. int ktra(char file[200]) 6. { 7. if(file[strlen(file)-1]=='p' && file[strlen(file)-2]=='n' &&

file[strlen(file)-3]=='i' && file[strlen(file)-4]=='.') 8. return 1; 9. else return 0; 10. } 11. void hien_thi(char tmA[200]) 12. { 13. //Copy toan bo file trong thu muc nguon vao thu muc dich 14. WIN32_FIND_DATAA FindFileData; 15. char tmAcopied[200]; 16. strcpy(tmAcopied,tmA); //sao chep de thu muc ban dau ko thay doi 17. HANDLE hFind=FindFirstFileA(strcat(tmAcopied,"\\*"),&FindFileData);

Page 24: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

23

18. do 19. { 20. strcpy(tmAcopied,tmA); 21. if(FindFileData.cFileName[0]!='.') 22. { 23. if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 24.

hien_thi(strcat(strcat(tmAcopied,"\\"),FindFileData.cFileName));

25. else if(ktra(FindFileData.cFileName)) 26. { 27. char copied_tmA[200]; 28. strcpy(copied_tmA,tmAcopied); 29.

DeleteFileA(strcat(strcat(copied_tmA,"\\"),FindFileData.cFileName)); 30. printf("Deleted file

%s\\%s\n",tmAcopied,FindFileData.cFileName); 31. } 32. } 33. } 34. while(FindNextFileA(hFind,&FindFileData)); 35. FindClose(hFind); 36. } 37. int main() 38. { 39. char a[200]; 40. printf("Nhap duong dan thu muc A: "); 41. gets(a); 42. 43. int cap=0; //Cấp thư mục 44. hien_thi(a); 45. //Tao thu muc tai B truoc khi sao chep 46. printf("\nXoa xong!"); 47. getch(); 48. return 0; 49. }

III. LẬP TRÌNH REGISTRY

3.1. Mở khóa (RegOpenKeyA)

Cú pháp:

LONG WINAPI RegOpenKey(

__in HKEY hKey,

__in LPCTSTR lpSubKey,

__out PHKEY phkResult

);

Trong đó:

HKEY hKey Kiểu khóa, có thể là một trong số các khóa:

HKEY_CLASSES_ROOT

HKEY_CURRENT_CONFIG

HKEY_CURRENT_USER

HKEY_LOCAL_MACHINE

Page 25: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

24

HKEY_USERS

LPCTSTR lpSubKey Khóa con

PHKEY phkResult Con trỏ lưu khóa được mở

Sau khi thực hiện hàm này thì biến phKResult sẽ lưu lại khóa được mở để sử dụng cho các

hàm tiếp theo cần dùng tới khóa này

Ví dụ: Có cây khóa registry như sau:

Hình 3.1. Liệt kê Registry

Chúng ta sẽ mở khóa Console như sau:

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { HKEY hKey; RegOpenKeyA(HKEY_CURRENT_USER,"Console",&hKey); getch(); return 0; }

Kết quả sau khi thực hiện hàm này là hKey=HKEY_CURRENT_USER\Console

3.2. Tạo khóa (RegCreateKeyA)

Cú pháp:

LONG WINAPI RegCreateKey(

__in HKEY hKey,

Page 26: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

25

__in LPCTSTR lpSubKey,

__out PHKEY phkResult

);

Các kiểu dữ liệu như hàm RegOpenKey

Để tạo 1 khóa, trước hết ta phải mở khóa cha của khóa cần tạo bằng hàm

RegOpenKeyA. Khóa con cần tạo có thể là 1 cấp hoặc nhiều cấp (“key1\key2\key3”).

Ví dụ: Tạo cây registry như hình sau:

Hình 3.2. Cây Registry

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { HKEY hKey; RegOpenKeyA(HKEY_CURRENT_USER,"Console",&hKey); RegCreateKeyA(hKey,"key1",&hKey); getch(); return 0; }

3.3. Xóa một khóa (RegDeleteKeyA)

Cú pháp:

LONG WINAPI RegDeleteKey(

__in HKEY hKey,

__in LPCTSTR lpSubKey

);

Trong đó:

HKEY hKey Khóa gốc, là một trong số các khóa đã liệt kê trong hàm RegOpenKey

LPCTSTR lpSubKey Khóa con cần xóa

Ví dụ: Xóa khóa key3 trong cây registry ở Hình 3.2.

Page 27: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

26

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { RegDeleteKeyA(HKEY_CURRENT_USER,"Console\\key1\\key2\\key3"); getch(); return 0; }

3.4. Đặt giá trị mặc định cho khóa con Default (RegSetValueA)

Cú pháp:

LONG WINAPI RegSetValue(

__in HKEY hKey,

__in LPCTSTR lpSubKey,

__in DWORD dwType,

__in LPCTSTR lpData,

__in DWORD cbData

);

Trong đó:

HKEY hKey Dữ liệu HKEY được liệt kê trong hàm RegOpenKey hoặc

giá trị biến hKey lấy ra từ hàm RegOpenKey.

LPCTSTR lpSubKey Khóa con

DWORD dwType

LPCTSTR lpData

DWORD cbData

Ví dụ: Thiết lập giá trị mặc định cho khóa Default trong key1 là “Gia tri mac dinh”

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { DWORD data; DWORD dwType=REG_SZ; RegSetValueA(HKEY_CURRENT_USER,"Console\\key1",dwType,"Gia tri mac dinh",strlen("Gia tri mac dinh")); getch(); return 0; }

Hoặc

#include <Windows.h> #include <conio.h>

Page 28: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

27

#include <stdio.h> int main() { HKEY hKey; RegOpenKeyA(HKEY_CURRENT_USER,"Console\\key1\\",&hKey); DWORD data; DWORD dwType=REG_SZ; RegSetValueA(hKey,NULL,dwType,"Gia tri mac dinh2",strlen("Gia tri mac dinh")); getch(); return 0; }

Sau khi thực hiện một trong 2 đoạn code trên thì ta sẽ nhận được kết quả như sau:

Khóa Default trong key1 có giá trị Data=”Gia tri mac dinh”

3.5. Lấy giá trị trong khóa (RegGetValueA)

Cú pháp:

LONG WINAPI RegGetValue(

__in HKEY hkey,

__in_opt LPCTSTR lpSubKey,

__in_opt LPCTSTR lpValue,

__in_opt DWORD dwFlags,

__out_opt LPDWORD pdwType,

__out_opt PVOID pvData,

__in_out_opt LPDWORD pcbData

);

Trong đó:

HKEY hkey, Khóa gốc, nhận một trong số các khóa trong hàm RegOpenKey

LPCTSTR lpSubKey Khóa con trong hkey

LPCTSTR lpValue Khóa cần lấy dữ liệu

DWORD dwFlags Có thể nhận một trong số các dữ liệu:

RRF_RT_REG_DWORD

RRF_RT_REG_EXPAND_SZ

RRF_RT_REG_MULTI_SZ

Page 29: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

28

RRF_RT_REG_SZ

RRF_RT_REG_BINARY

LPDWORD pdwType Kiểu dữ liệu

PVOID pvData Biến lưu giá trị cần lấy trong khóa

LPDWORD pcbData Kích thước dữ liệu

Ví dụ 1: Khóa Scheme Source nằm trong HKEY_CURRENT_USER\Control Panel\Cursors, kiểu

REG_DWORD, giá trị là 2, ta sẽ đọc dữ liệu này ra màn hình.

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { DWORD data; DWORD size=200; DWORD dwType=REG_DWORD; RegGetValueA(HKEY_CURRENT_USER, //Khóa gốc "Control Panel\\Cursors", //Khóa con "Scheme Source", //Khóa cần lấy giá trị RRF_RT_REG_DWORD, //Giá trị cần lấy kiểu DWORD &dwType, //Khóa này kiểu DWORD &data, //Biến lưu dữ liệu &size); //Kích thước dữ liệu hay kích thước biến data printf("%d",data); getch(); return 0; }

Ví dụ 2: Khóa Last Username nằm trong HKEY_CURRENT_USER\Identities, kiểu REG_SZ, giá

trị Main Identity, ta sẽ đọc giá trị này ra màn hình

#include <Windows.h> #include <conio.h> #include <stdio.h>

Page 30: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

29

int main() { char data[20]; DWORD size=20; DWORD dwType=REG_SZ; RegGetValueA(HKEY_CURRENT_USER, //Khóa gốc "Identities", //Khóa con "Last Username", //Khóa cần lấy giá trị RRF_RT_REG_SZ, //Giá trị cần lấy kiểu DWORD &dwType, //Khóa này kiểu DWORD &data, //Biến lưu dữ liệu &size); //Kích thước dữ liệu hay kích thước biến data printf("%s",data); getch(); return 0; }

Các kiểu dữ liệu khác bạn làm tương tự.

3.6. Thêm/Sửa giá trị một khóa (RegSetKeyValueA)

Cú pháp:

LONG WINAPI RegSetKeyValue(

__in HKEY hKey,

__in_opt LPCTSTR lpSubKey,

__in_opt LPCTSTR lpValueName,

__in DWORD dwType,

__in_opt LPCVOID lpData,

__in DWORD cbData

);

Trong đó: kiểu dữ liệu và các biến tương tự hàm RegSetValueA như trên

Ví dụ 1: Thêm một khóa tên là “key”, kiểu REG_DWORD, giá trị cho vào là 10.

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { DWORD data=10; DWORD size=sizeof(DWORD); DWORD dwType=REG_DWORD;

Page 31: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

30

RegSetKeyValueA(HKEY_CURRENT_USER, //Khóa gốc "Console\\key1", //Khóa con "key", //Khóa cần thêm dwType, //Kiểu khóa - REG_DWORD &data, //Biến lưu dữ liệu vào size); //Kích thước biến data getch(); return 0; }

Ví dụ 2: Thêm một khóa tên là “key_SZ”, kiểu REG_SZ, giá trị cho vào là “Gi| trị cần thêm”.

Vì “Gi| trị cần thêm” là kiểu UNICOE (chữ có dấu) nên ta sử dụng biến kiểu TCHAR, các

hàm của kiểu char như strcpy (sao chép chuỗi) được đổi thành lstrcpy (có chữ “l” đằng

trước), và dữ liệu lúc này thêm vào là L”Gi| trị cần thêm”

#include <Windows.h> #include <conio.h> #include <stdio.h> int main() { TCHAR data[20]; lstrcpy(data,L"Giá trị cần thêm"); DWORD size=lstrlen(data)*sizeof(TCHAR); DWORD dwType=REG_SZ; RegSetKeyValue(HKEY_CURRENT_USER, //Khóa gốc L"Console\\key1", //Khóa con L"key_SZ", //Khóa cần thêm dwType, //Kiểu khóa - REG_DWORD &data, //Biến lưu dữ liệu vào size); //Kích thước biến data getch(); return 0; }

BÀI TẬP:

1. Tạo chương trình cho phép (không cho phép) hệ thống chạy chương trình

bằng c|ch v{o Start \ Run “HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \

CurrentVersion \ Policies \ Explorer \ NoRun” (type: DWORD, value:1)

1. #include <windows.h> 2. #include <stdio.h> 3. #include <conio.h> 4. int main() 5. { 6. char tra_loi; 7. DWORD value; 8. printf("Ban muon vo hieu hoa cua so run? (y/n): "); 9. scanf("%c",&tra_loi);

Page 32: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

31

10. if(tra_loi=='y' || tra_loi=='Y') 11. value=1; 12. else 13. value=0; 14. if(RegSetKeyValueA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVe

rsion\\Policies\\Explorer", 15. "NoRun",REG_DWORD,&value,sizeof(DWORD)) == ERROR_SUCCESS) 16. printf("Thuc hien thanh cong!"); 17. else 18. printf("Da that bai"); 19. getch(); 20. return 0; 21. }

2. Thiết lập “HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Memory Management \ ClearPageFileAtShutdown” gi| trị của khóa = 1 nếu nó đang bằng 0 v{ ngược lại

#include <windows.h> #include <stdio.h> #include <conio.h> int main() { HKEY hKey; DWORD dwType=REG_DWORD; DWORD data; DWORD size=sizeof(DWORD); if(!(RegOpenKeyA(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Session Manager\\Memory Management", &hKey)==ERROR_SUCCESS) ) return 1; RegGetValueA(hKey,NULL,"ClearPageFileAtShutdown",RRF_RT_REG_DWORD,&dwType,&data,&size); if(data==0) data=1; else if(data==1) data=0; if(RegSetKeyValueA(hKey,NULL,"ClearPageFileAtShutdown",dwType,&data,size)==ERROR_SUCCESS) printf("Da doi thanh cong!"); else printf("Khong the doi duoc!"); RegCloseKey(hKey); getch(); return 0; }

3. “HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ WaitToKil

lServiceTimeout” (type: DWORD) cho biết thời gian hệ thống chờ c|c dịch vụ, … Hãy

hiển thị gi| trị của khóa n{y của hệ thống. Sau đó, thiết lập gi| trị khóa này =450.

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. int main() 5. { 6. HKEY hKey; 7. if(RegOpenKeyA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control",&hKey)

==ERROR_SUCCESS)

Page 33: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

32

8. { 9. char value[10]; 10. DWORD size=sizeof(char)*10; 11. DWORD dwType=REG_SZ; 12.

if(RegGetValueA(hKey,NULL,"WaitToKillServiceTimeout",RRF_RT_REG_SZ,&dwType,&value,&size)==ERROR_SUCCESS)

13. { 14. printf("Time: %s",value); 15.

RegSetKeyValueA(hKey,NULL,"WaitToKillServiceTimeout",REG_SZ,"450",size); 16. } 17. else return 1; 18. } 19. getch(); 20. return 0; 21. }

4. Khóa HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \

CurrentVersion \ Policies \ Explorer \ NoSetFolders” (type: DWORD, value: 1) sẽ

không cho hiển thị mục menu Settings của Windows Explorer. Hãy thiết lập gi| trị

cho khóa =0 nếu nó đang bằng 1 hoặc chưa có v{ ngược lại.

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. int main() 5. { 6. HKEY hKey; 7. if(RegOpenKeyA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersio

n\\Policies\\Explorer",&hKey)==ERROR_SUCCESS) 8. { 9. DWORD dta, size=sizeof(DWORD), dwType=REG_DWORD; 10.

RegGetValueA(hKey,NULL,"NoSetFolders",RRF_RT_REG_DWORD,&dwType,&dta,&size); 11. if(dta==0) 12. dta=1; 13. else if(dta==1) 14. dta=0; 15. RegSetKeyValueA(hKey,NULL,"NoSetFolders",REG_DWORD,&dta,size); 16. printf("Thuc hien thanh cong!"); 17. } 18. else 19. printf("Loi!"); 20. getch(); 21. return 0; 22. } 23.

5. L{m ẩn/hiện chức năng Task Manager

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. int main() 5. { 6. DWORD value=1, dwType=REG_DWORD, size=sizeof(DWORD);//disable; 7. HKEY hKey; 8. 9. RegOpenKeyA(HKEY_CURRENT_USER,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\

Policies\\System",&hKey); 10. 11. RegGetValueA(hKey,NULL,"DisableTaskMgr",RRF_RT_REG_DWORD,&dwType,&value,&size

Page 34: de_cuong_ltw

Tài liệu lập trình Windows – Đỗ Đức Hùng

Liên hệ tác giả: G!M: [email protected], Web: http://cnt50dh1.net

33

); 12. if(value==0) 13. value=1; 14. else if(value==1) 15. value=0; 16. if(RegSetKeyValueA(HKEY_CURRENT_USER, 17.

"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System","DisableTaskMgr",

18. REG_DWORD, 19. &value, 20. size)==ERROR_SUCCESS) 21. printf("Thanh cong!"); 22. else 23. printf("That bai!"); 24. getch(); 25. return 0; 26. }

6. L{m ẩn/hiện biểu tượng My Computer

1. #include <Windows.h> 2. #include <conio.h> 3. #include <stdio.h> 4. int main() 5. { 6. DWORD value=1, dwType=REG_DWORD, size=sizeof(DWORD);//disable; 7. HKEY hKey; 8. RegOpenKeyA(HKEY_CURRENT_USER,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\

Policies\\NonEnum",&hKey); 9. RegGetValueA(hKey,NULL,"{20D04FE0-3AEA-1069-A2D8-

08002B30309D}",RRF_RT_REG_DWORD,&dwType,&value,&size); 10. if(value==0) 11. value=1; 12. else if(value==1) 13. value=0; 14. if(RegSetKeyValueA(hKey,NULL, 15. "{20D04FE0-3AEA-1069-A2D8-08002B30309D}", 16. dwType, 17. &value, 18. size) ==ERROR_SUCCESS) 19. printf("Thanh cong!"); 20. else 21. printf("That bai!"); 22. getch(); 23. return 0; 24. } 25.

Chúc các bạn thi đạt kết quả cao!