您的位置:首页 > 其它

WinCE下读写注册表的又一种方法

2011-12-01 22:01 316 查看



上一篇介绍了VB.NET读写WinCE注册表的方法,用着确实方便。在驱动或者应用程序开发的过程中,其实也有一种简便的方法读写注册表,使用微软提供的CReg类(Registry helper class)。用法也很简单,只要包括creg.hxx即可,SDK中一般会包含该头文件。

闲话少说,直接附上代码,供参考。

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 // Preloader.cpp : 定义控制台应用程序的入口点。

2 //

3

4 #include "stdafx.h"

5 #include <windows.h>

6 #include "creg.hxx"

7

8 #define GET_PATH_FROM_REG

9

10 #define REG_ROOT HKEY_CURRENT_USER

11 #define REG_PATH _T("Software")

12 #define REG_NAME _T("AutoRunApp")

13 #define INI_PATH _T("\\Config.ini")

14 #define WINCE_STANDARD_SHELL _T("\\windows\\explorer.exe")

15

16 static TCHAR g_szFilePath[MAX_PATH];

17

18 #ifdef GET_PATH_FROM_REG     // 从注册表中读取需开机时自启动程序的完整路径

19 static BOOL GetFilePath(TCHAR *pFileName)

20 {

21 if (pFileName)

22 {

23 CReg Reg;

24 DWORD dwLen = MAX_PATH;

25 Reg.Create(REG_ROOT,REG_PATH);

26 if (Reg.ValueSZ(REG_NAME,pFileName,dwLen))

27 {

28 return TRUE;

29 }

30 }

31

32 return FALSE;

33 }

34 #else               // 从文件中读取需开机时自启动程序的完整路径

35 static BOOL GetFilePath(TCHAR *pFileName)

36 {

37 int nIndex;

38

39 if(pFileName)

40 {

41 HANDLE hFile = INVALID_HANDLE_VALUE;

42 char szPath[MAX_PATH];

43 DWORD dwRead;

44 hFile = CreateFile(INI_PATH,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);

45 if(hFile != INVALID_HANDLE_VALUE)

46 {

47 ReadFile(hFile,szPath,MAX_PATH,&dwRead,NULL);

48 for(nIndex = 0; nIndex < dwRead; nIndex++)

49 {

50 if(szPath[nIndex] == '\r' || szPath[nIndex] == '\n')

51 {

52 break;

53 }

54 *pFileName++ = szPath[nIndex];

55 }

56

57 *pFileName = _T('\0');

58

59 CloseHandle(hFile);

60 return TRUE;

61 }

62 }

63

64 return FALSE;

65 }

66 #endif

67

68 static BOOL IsFileExist(TCHAR *pFileName)

69 {

70 if (pFileName)

71 {

72 HANDLE hFind = INVALID_HANDLE_VALUE;

73 WIN32_FIND_DATA fd;

74

75 RETAILMSG(1,(TEXT("\r\nIsFileExist(%s)"),pFileName));

76

77 hFind = FindFirstFile(pFileName,&fd);

78

79 if (INVALID_HANDLE_VALUE != hFind)

80 {

81 FindClose(hFind);

82 return TRUE;

83 }

84 }

85

86 return FALSE;

87 }

88

89 static BOOL RunAppFile(TCHAR *pFilePath)

90 {

91 if (pFilePath)

92 {

93 PROCESS_INFORMATION pi;

94 BOOL bRet = FALSE;

95 bRet = CreateProcess(pFilePath,NULL,NULL,NULL,FALSE,0,NULL,

96 NULL,NULL,&pi);

97 if (bRet)

98 {

99 CloseHandle(pi.hThread);

100 CloseHandle(pi.hProcess);

101 return TRUE;

102 }

103 }

104

105 return FALSE;

106 }

107

108 int _tmain(int argc, _TCHAR* argv[])

109 {

110 // 获取开机时需自启动程序的完整路径

111 // 判断该程序否存在

112 // 如果程序存在,则启动该程序,如果不存在,则启动WinCE默认的Shell

113 if (GetFilePath(g_szFilePath))

114 {

115 if (IsFileExist(g_szFilePath))

116 {

117 if(RunAppFile(g_szFilePath))

118 {

119 return 0;

120 }

121 }

122 }

123

124 RunAppFile(WINCE_STANDARD_SHELL);

125

126 return 0;

127 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: