9.16.2009

Source : Load Unload Device Driver

很對不起,這個code是base on http://www.ucancode.net/Visual_C_Codes/Loading-DLLs-LoadLibrary-GetProcAddress-FreeLibrary-VC-Example.htm
的 load dll function 寫的,竟然沒有提到,真是抱歉。

我猜原 po 應該是在 Bruce Eitman 的 WINCE Blog:

Windows CE: Loading a Driver with ActivateDeviceEx



這個code是以 load unload driver : SST
cpp

#include "stdafx.h"

#include "resource.h"

#define SST_REG_KEY (TEXT("Drivers\\Builtin\\SST"))

typedef struct {
TCHAR *ValueName;
DWORD Type;
DWORD DwordValue;
TCHAR *StringValue;
} RegEntry;

RegEntry RegEntries[]={
{TEXT("Dll"), REG_SZ, 0,TEXT("SST.DLL")},
{TEXT("Prefix"),REG_SZ, 0,TEXT("SST")},
{TEXT("Order"), REG_DWORD,4,NULL},
{TEXT("Index"), REG_DWORD,1,NULL},
{NULL,NULL,NULL,NULL}
};

void AddDriverSSTToRegistry()
{
HKEY hTargetKey;
DWORD dwDisposition;
DWORD RetVal;
DWORD Index=0;
BYTE *Value;
DWORD uSize;

RetVal = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
SST_REG_KEY,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hTargetKey,
&dwDisposition
);
if(RetVal == ERROR_SUCCESS)
{
while(RegEntries[Index].ValueName!=NULL)
{
if(RegEntries[Index].Type == REG_DWORD)
{
RegSetValueEx(hTargetKey,
RegEntries[Index].ValueName,
0,
REG_DWORD,
(const BYTE*)&RegEntries[Index].DwordValue,
sizeof(DWORD));
}
else
{
Value = (BYTE *)RegEntries[Index].StringValue;
uSize = (wcslen(RegEntries[Index].StringValue)+1)*sizeof(TCHAR);

RegSetValueEx(hTargetKey,
RegEntries[Index].ValueName,
0,
REG_SZ,
(const BYTE*)RegEntries[Index].StringValue,
(wcslen(RegEntries[Index].StringValue)+1) * sizeof(TCHAR));

}
Index++;
}
}
}

HANDLE hDriverSST=INVALID_HANDLE_VALUE;

BOOL LoadDriverSST(void)
{
BOOL RetVal = NULL;

if(hDriverSST == INVALID_HANDLE_VALUE)
{
hDriverSST = ActivateDeviceEx( SST_REG_KEY,
NULL,
0,
NULL);
if(hDriverSST != INVALID_HANDLE_VALUE && hDriverSST != 0)
RetVal = TRUE;
else
{
RETAILMSG(1,(TEXT("Fail to load Driver SST\n")));
}
}
return RetVal;
}

void UnloadDriverSST(void)
{
if(hDriverSST!= INVALID_HANDLE_VALUE)
{
DeactivateDevice(hDriverSST);
hDriverSST=INVALID_HANDLE_VALUE;
}
}

BOOL CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg) {
case WM_INITDIALOG:
return TRUE;
case WM_CLOSE:
EndDialog(hWnd,0);
return FALSE;
case WM_COMMAND:
switch(wParam){
case IDC_BTNLOAD:
LoadDriverSST();
printf("LOAD!");
return TRUE;
case IDC_BTNUNLOAD:
UnloadDriverSST();
printf("UNLOAD");
return TRUE;
case IDC_RESISTRY:
printf("Registry");
AddDriverSSTToRegistry();
return TRUE;
}
break;
default:
return FALSE;
}
}

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

return DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);

return 0;
}

5 則留言:

匿名 提到...

Could at least give me credit if you are going to use one of my functions in your blog?

Bruce Eitman

checko 提到...

Yes, Yes, sorry, This post is a sequal of my previous post - http://realchecko.blogspot.com/2009/09/getprocaddresses-one-time-dll.html ,
So I forget to give the link in this post, I've modified this post.

Sorry for my mistake.

checko 提到...

Beside that, I've write http://realchecko.blogspot.com/2009/10/shame-on-me.html on my blog to apology to you.

匿名 提到...

Checko:

I too am sorry. I did not mean to sound harsh. I should have written "please" with that. I am very pleased by what you have done with my code.

You have a very nice blog - I only wish that I could read Chinese. I must rely on a friend to assist me.

Please use my code in the future in this way.

Thank you very much.

Bruce Eitman

checko 提到...

You are a kind person, I got lots of benefits from your codes and blogs.

And thank you for forgiving my ignorance and my poor english.