c语言中的设备通知程序
我用c编写了这段代码,用于在WinXP32中检测闪存 但它什么都不做 有人能帮忙吗 (我只想了解闪存插入何时运行另一个程序,但我需要wndmain和wndproc,我无法正确编写它们) 最后一个问题是它是否可能不工作,因为我在Win7 64位运行它c语言中的设备通知程序,c,winapi,C,Winapi,我用c编写了这段代码,用于在WinXP32中检测闪存 但它什么都不做 有人能帮忙吗 (我只想了解闪存插入何时运行另一个程序,但我需要wndmain和wndproc,我无法正确编写它们) 最后一个问题是它是否可能不工作,因为我在Win7 64位运行它 #include <stdio.h> #include <tchar.h> #include <windows.h> #include <dbt.h> LRESULT CALLBACK WndProc(
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <dbt.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInstance =(HINSTANCE)(GetModuleHandle(NULL));
if(WndProc)
{
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
printf("%s" , msg);
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
if (uiMsg == WM_DEVICECHANGE)
{
MessageBox(NULL, TEXT("WM_DEVICECHANGE"), TEXT("WndProc"), MB_OK);
return 0;
}
return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
#包括
#包括
#包括
#包括
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
hInstance=(hInstance)(GetModuleHandle(NULL));
如果(WndProc)
{
味精;
while(GetMessage(&msg,NULL,0,0)>0)
{
翻译信息(&msg);
发送消息(&msg);
printf(“%s”,msg);
}
}
返回0;
}
LRESULT回调WndProc(HWND HWND、UINT uiMsg、WPARAM WPARAM、LPARAM LPARAM)
{
如果(uiMsg==WM_设备更改)
{
消息框(空、文本(“WM_DEVICECHANGE”)、文本(“WndProc”)、MB_OK);
返回0;
}
返回DefWindowProc(hWnd、uiMsg、wParam、lParam);
}
您缺少很多代码,例如创建一个新的窗口类来注册WndProc函数以处理已发送的消息等。不幸的是,为了能够处理WM_uuu消息,您需要做大量的工作
作为一个很好的起点,我想让你看看陈雷蒙的。它很简单,但包含一个简单的框架,可以让您开始使用
如果你特别需要C而不是C++,那么就足够了。你可能会创建一个预定义的窗口类型的实例,然后将子类分类(设置它的窗口过程是你编写的一个),它会给出比我现在所用的代码短的代码。您应该注意,链接到的MSDN代码只提供了一个代码片段,也就是说,它是一个不完整的程序。您仍然需要有一种从windows接收消息的机制 下面是一个程序,它将捕获win7下使用minGW编译的插入和删除消息。在code::blocks中,只需向Win32 Gui(框架应用程序)模板添加13行代码
#include <windows.h>
#include <dbt.h>
// Declare Windows procedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
// Make the class name into a global variable
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; // This is the handle for our window
MSG messages; // Here messages to the application are saved
WNDCLASSEX wincl; // Data structure for the windowclass
// The Window structure
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
UINT event;
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_DEVICECHANGE:
event = (UINT)wParam;
switch (event)
{
case DBT_DEVICEARRIVAL:
MessageBox(NULL, "Device arrival", "Notice", MB_ICONASTERISK);
break;
case DBT_DEVICEREMOVECOMPLETE:
MessageBox(NULL, "Device removal", "Notice", MB_ICONASTERISK);
break;
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
实际上,我并不需要打开窗口,我只需要理解当一个flash插入时,我找到了这个页面:但是它没有主函数,我们需要一个很大的帮助来编写它的winmain()设备通知是由一个WM_DEVICECHANGE消息执行的,需要转到窗口(甚至是虚拟的、不可见的窗口)-有关信息,请参阅。为此,您需要了解windows消息泵的工作原理,但是暂时你可以看我抓取的抓取程序。OK TUNX,但是你给的页面是C++的并且有类,我不能理解SRY,但是你还有C代码吗?已经有一个链接用于C程序的抓取程序。+ 1:这是一个很好的简单例子(我在回答中使用了链接:))。一个问题-尽管没有调用
RegisterDeviceNotification
,但这是否有效?谢谢我复制了你的代码并运行了它,但它打开了一个空窗口,它的标题是中文的,我在页面中附加了代码实际上也要返回驱动程序名,我应该在代码中的什么地方使用该部分?@icabod-谢谢。是的,在我的系统上它可以工作-win7 home pro.@mellodi-我将更新代码示例。您可以看到,当我收到WM_DEVICECHANGED消息时,我只需调用您之前链接的代码。注意:我只添加了一个更新的WindowProcedure,您仍然需要删除现有的WindowProcedure并添加链接到的msdn中的代码。:)
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
UINT event;
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_DEVICECHANGE:
Main_OnDeviceChange(hwnd, wParam, lParam);
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}