WINCE流驱动的自动加载

********************************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_DWORDSpecifies 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

posted on 2010-11-13 20:47  王亮1  阅读(150)  评论(0编辑  收藏  举报

导航