注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

男儿当自强的博客

每天进步一点

 
 
 

日志

 
 
 
 

WINCE流驱动的自动加载  

2010-11-14 08:09:53|  分类: wince应用程序开 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

********************************LoongEmbedded********************************

作者:LoongEmbedded(kandi)

时间:2010.11.13

类别:WINCE应用开发

********************************LoongEmbedded********************************

 

软件环境:WINCE6.0+VS2005

 

动态加载在系统启动之后根据需要来加载,而不是在系统启动过程中加载,就动态加载CMC.dll驱动为例来说明大概需要的工作:

 

1.在platform.bib中通过下面语句把CMC.dl放在windows文件夹下面

CMC.dll         $(_FLATRELEASEDIR)\CMC.dll                  NK  SHK

 

2.创建注册表子键并在此驱动注册表子键中写键值,键值是由键值名称和键值的值组成的。

下面介绍需要用的的注册表相关函数。

⑴RegCreateKeyEx函数

 

RegCreateKeyEx函数头如下:

LONG RegCreateKeyEx(

  HKEY hKey,

  LPCWSTR lpSubKey,

  DWORD Reserved,

  LPWSTR lpClass,

  DWORD dwOptions,

  REGSAM samDesired,

  LPSECURITY_ATTRIBUTES lpSecurityAttributes,

  PHKEY phkResult,

  LPDWORD lpdwDisposition

);

下面来看这些参数的描述

Parameters

 

hKey

[in] Handle to a currently open key or one of the following predefined reserved handle values:

HKEY_CLASSES_ROOT

HKEY_CURRENT_USER

HKEY_LOCAL_MACHINE

HKEY_USERS

Windows CE does not support the HKEY_CURRENT_CONFIG, HKEY_PERFORMANCE_DATA, or HKEY_DYN_DATA predefined reserved handle values.

 

The key opened or created by the RegCreateKeyEx function is a subkey of the key identified by the hKey parameter.

 

 

lpSubKey

 

[in] Pointer to a null-terminated string specifying the name of a subkey that this function opens or creates. The subkey specified must be a subkey of the key identified by the hKey parameter. This subkey must not begin with the backslash character (\). If the parameter is NULL, then RegCreateKeyEx behaves like RegOpenKey where it opens the key specified by hKey. In Windows CE, the maximum length of a key name is 255 characters, not including the terminating NULL character. You can also only nest 16 levels of sub-keys in Windows CE.

 

Reserved

[in] Reserved; set to 0.

 

lpClass

[in] Pointer to a null-terminated string that specifies the class (object type) of this key. This parameter is ignored if the key already exists. In Windows CE, the maximum length of a class string is 255 characters, not including the terminating NULL character.

 

dwOptions

[in] Registry key options.

REG_OPTION_NON_VOLATILE :Default setting. All registry keys are created as non-volatile and the information stored in memory is preserved when the OS is restarted. The RegSaveKey function saves keys that are non-volatile.

REG_OPTION_VOLATILE :All registry keys are created as volatile, and the information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the OS is shut down. The RegSaveKey function does not save volatile registry keys. This flag is ignored for keys that already exist.

 

samDesired

[in] Ignored; set to 0 to ensure compatibility with future versions of Windows CE.

 

lpSecurityAttributes

[in] Set to NULL. Windows CE automatically assigns the key a default security descriptor.

 

phkResult

[out] Pointer to a variable that receives a handle to the opened or created key. When you no longer need the returned handle, call the RegCloseKey function to close it.

lpdwDisposition

 

[out] Pointer to a variable that receives disposition values. The following table shows possible values for this parameter. Value Description

REG_CREATED_NEW_KEY The key did not exist and was created.

REG_OPENED_EXISTING_KEY The key existed and was simply opened without being changed.

 

Return Values

ERROR_SUCCESS indicates success. A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error, call FormatMessage with the FORMAT_MESSAGE_FROM_SYSTEM flag set. The message resource is optional; therefore, if you call FormatMessage it could fail.

 

⑵ RegSetValueEx函数

LONG RegSetValueEx(

  HKEY hKey,

  LPCWSTR lpValueName,

  DWORD Reserved,

  DWORD dwType,

  const BYTE* lpData,

  DWORD cbData

);

 

Parameters

 

hKey

[in] Handle to a currently open key or any of the following predefined reserved handle values:

HKEY_CLASSES_ROOT

HKEY_CURRENT_USER

HKEY_LOCAL_MACHINE

HKEY_USERS

 

lpValueName

[in] Pointer to a string containing the name of the value to set. If a value with this name is not already present in the key, the function adds it to the key. If this parameter is NULL or an empty string, the function sets the type and data for the key's unnamed value. Registry keys do not have default values, but they can have one unnamed value, which can be of any type. The maximum length of a value name is 255, not including the terminating NULL character.

 

Reserved

[in] Reserved; must be zero.

 

dwType

[in] Type of information to be stored as the value's data.

REG_BINARY: Specifies binary data in any form.

REG_DWORD:Specifies a 32-bit number.

REG_DWORD_LITTLE_ENDIAN: Specifies a 32-bit number in little-endian format. This is equivalent to REG_DWORD. In little-endian format, a multi-byte value is stored in memory from the lowest byte (the little end) to the highest byte. For example, the value 0x12345678 is stored as (0x78 0x56 0x34 0x12) in little-endian format.

 

REG_DWORD_BIG_ENDIAN :Specifies a 32-bit number in big-endian format. In big-endian format, a multi-byte value is stored in memory from the highest byte (the big end) to the lowest byte. For example, the value 0x12345678 is stored as (0x12 0x34 0x56 0x78) in big-endian format.

 

REG_EXPAND_SZ: Specifies a null-terminated string that contains unexpanded references to environment variables (for example, %PATH%).

REG_LINK: Specifies a Unicode symbolic link. Used internally; applications should not use this type.

REG_MULTI_SZ: Specifies an array of null-terminated strings, terminated by two null characters.

REG_NONE :No defined value type.

REG_RESOURCE_LIST: Specifies a device-driver resource list.

REG_SZ: Specifies a null-terminated Unicode string. Do not use the REG_SZ type to store hard-coded paths to the system root.

 

lpData

[in] Pointer to a buffer containing the data to be stored with the specified value name.

 

cbData

[in] Size, in bytes, of the information pointed to by the lpData parameter. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character. The maximum size of data allowed in Windows CE is 4 KB.

 

Return Values

ERROR_SUCCESS indicates success. A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error, call FormatMessage with the FORMAT_MESSAGE_FROM_SYSTEM flag set. The message resource is optional; therefore, if you call FormatMessage it could fail.

 

 

3.调用函数ActivateDeviceEx来加载CMC驱动。

ActivateDeviceEx函数

 

HANDLE ActivateDeviceEx(

  LPCWSTR lpszDevKey,

  LPCVOID lpRegEnts,

  DWORD cRegEnts,

  LPVOID lpvParam

);

 

Parameters

 

lpszDevKey

[in] Pointer to a string containing the registry path of the device driver's registry key. This key contains the driver's DLL name, prefix, and other data. For information about the subkeys of the HKEY_LOCAL_MACHINE\Drivers\Active registry key, see Device Manager Registry Keys.

 

lpRegEnts

[in] Pointer to an array of REGINI structures, each of which defines a registry value to add to the device's Active registry key before its driver is loaded. Set to NULL unless it is for a bus driver.

 

cRegEnts

[in] Count of the number of REGINI structures to which lpRegEnts points. This affects a generalization of the ActivateDevice function.

 

lpvParam

[in] Opaque pointer to a driver-specific data structure. You can use it to pass parameters to the loaded driver without having to write them through the registry. lpvParam is passed to the XXX_Init (Device Manager) function as a second parameter. lpvParam can be used by bus drivers to pass bus-specific information, entry points, or both to loaded drivers. Using this parameter prevents clients of that bus from becoming bus-agnostic. This is acceptable for some busses, especially those whose drivers cannot become bus-agnostic.

 

Return Values

Returns a handle that you can use later as the parameter to the DeactivateDevice function, if successful. If the function is not successful, it returns INVALID_HANDLE_VALUE. To obtain extended error information, call GetLastError.

 

4.调用函数DeactivateDevice来卸载CMC驱动。

DeactivateDevice函数

BOOL DeactivateDevice(

  HANDLE hDevice

);

 

Parameters

hDevice

[in] Handle to an active device. The ActivateDevice and ActivateDeviceEx functions return this value.

Return Values

Nonzero indicates success. Zero indicates failure.

 

5.本文是基于对话框的方式来实现的,加载和卸载的对话框对应的函数如下

 

加载CMC.dll的函数

void CloaddriverDlg::OnBnClickedLoadDriver()

{

             LONG    lResult = 0;

         DWORD dwParam = 88; 

 

         HKEY hOpenKey;

         DWORD OpenStyle;

         DWORD ErrorCode=0;

         CString strTmp;

 

         TCHAR CmcRootKey[] = TEXT("Drivers\\BuiltIn\\CMC");

 

         TCHAR OrderKeyName[] = TEXT("Order");

         DWORD   OrderKeyValue = 0;

 

         TCHAR InderKeyName[] = TEXT("Inder");

         DWORD   InderKeyValue = 1;

 

         TCHAR PrefixKeyName[] = TEXT("Prefix");

         TCHAR PrefixKeyValue[] = TEXT("CMC");

 

         TCHAR DllKeyName[] = TEXT("Dll");

         TCHAR DllKeyValue[] = TEXT("CMC.dll");

 

         lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE,CmcRootKey,0,L"",0,0,NULL,&hOpenKey,&OpenStyle);

        

        

             if(ERROR_SUCCESS == lResult)

         {

                   RegSetValueEx(hOpenKey,OrderKeyName,0,REG_DWORD,(BYTE *)&OrderKeyValue,sizeof(DWORD));

                   RegSetValueEx(hOpenKey,InderKeyName,0,REG_DWORD,(BYTE *)&InderKeyValue,sizeof(DWORD));

 

                   RegSetValueEx(hOpenKey,PrefixKeyName,0,REG_SZ,(BYTE *)PrefixKeyValue,sizeof(PrefixKeyValue));

                   RegSetValueEx(hOpenKey,DllKeyName,0,REG_SZ,(BYTE *)DllKeyValue,sizeof(DllKeyValue));

 

                   m_handle = ActivateDeviceEx(CmcRootKey,NULL,0,&dwParam);  

                   ErrorCode = GetLastError();

        

                   strTmp.Format(_T("%x"),&ErrorCode);

                   CEdit *pDispEdit=(CEdit *)GetDlgItem(IDC_EDIT1);

                   pDispEdit->SetWindowText(strTmp);

                   UpdateData(FALSE);

 

                if(m_handle == NULL)

                   {

                            MessageBox(_T("avtivate driver fail!"));

                   }

                   else

                   {

                            MessageBox(_T("avtivate driver success!"));

                   }

 

         }

         else

         {

                   MessageBox(_T("create key fail!"));

         }

 

}

 

卸载CMC.dll驱动的函数

void CloaddriverDlg::OnBnClickedUnloadDriver()

{

         // TODO: Add your control notification handler code here

         BOOL TempValue = 0;

                  

         TempValue = DeactivateDevice(m_handle);

 

         if(TempValue == 0)

         {

                   MessageBox(_T("deactivate driver fail!"));

         }

         else

         {

                   MessageBox(_T("deactivate driver success!"));

         }

}

 

参考链接

http://blog.csdn.net/norains/archive/2010/02/22/5316923.aspx

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LoongEmbedded/archive/2010/11/13/6007433.aspx

  评论这张
 
阅读(1288)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017