#include <windows.h>
#include <dbt.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int readFile(const char * nodeStatus)
{
FILE *filePtr;
char filePath[256];
char line[256];
const char *directory;
const char *fileName;
directory = nodeStatus;
fileName = ":\\cfg.ini";
strcpy_s(filePath, sizeof(filePath), directory);
strcat_s(filePath + strlen(directory), sizeof(filePath) - strlen(directory), fileName);
if (fopen_s(&filePtr, filePath, "r") != 0) {
perror("Error opening file");
return 1;
}
while (fgets(line, sizeof(line), filePtr) != NULL) {
printf("%s", line);
}
fclose(filePtr);
return 0;
}
LRESULT CALLBACK WndProc(HWND h, UINT msg, WPARAM wp, LPARAM lp)
{
if (msg == WM_DEVICECHANGE)
{
if ((DWORD)wp == DBT_DEVICEARRIVAL)
{
DEV_BROADCAST_VOLUME* p = (DEV_BROADCAST_VOLUME*)lp;
if (p->dbcv_devicetype == DBT_DEVTYP_VOLUME)
{
MessageBox(NULL, TEXT("节点已接入"), NULL, NULL);
int driveLetter = 65+(int)(log(p->dbcv_unitmask) / log(2));
char filePath[100] = { (char)driveLetter,':','//','t','x','t','.','t','x','t' };
printf("%s", filePath);
FILE *filePtr;
char ch;
if (fopen_s(&filePtr, filePath, "r") != 0) {
perror("Error opening file");
return 1;
}
while ((ch = fgetc(filePtr)) != EOF) {
putchar(ch);
}
fclose(filePtr);
}
}
else if ((DWORD)wp == DBT_DEVICEREMOVECOMPLETE)
{
DEV_BROADCAST_VOLUME* p = (DEV_BROADCAST_VOLUME*)lp;
if (p->dbcv_devicetype == DBT_DEVTYP_VOLUME)
{
int l = 65 + (int)(log(p->dbcv_unitmask) / log(2));
MessageBox(NULL, TEXT("节点已拔出"), NULL, NULL);
}
}
return TRUE;
}
else
return DefWindowProc(h, msg, wp, lp);
}
int main()
{
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.lpszClassName = TEXT("myusbmsg");
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
HWND h = CreateWindow(TEXT("myusbmsg"), TEXT(""), 0, 0, 0, 0, 0, 0, 0, GetModuleHandle(0), 0);
MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}