将可变长度结构从用户模式传递到内核模式

我正在写虚拟磁盘驱动程序,在那里我定义了这样的结构:

typedef struct _MOUNT_NEW_QUERY { PWCHAR imagePath; WCHAR letter; PCHAR key; } MOUNT_NEW_QUERY, *PMOUNT_NEW_QUERY; 

所以我有一些dynamic大小的结构。

我如何将它从用户模式传递给我的驱动程序?

分配一个连续的内存块,足以保存你的结构和“键”和“路径”的数据 – 如下所示:

 /* we add + 1 for terminating NULLs to make life easy */ size_t keyLen = (strlen(key) + 1); size_t imgLen = (wcslen(imagePath) + 1) * sizeof(WCHAR); PMOUNT_NEW_QUERY pMNQ = malloc(sizeof(MOUNT_NEW_QUERY) + keyLen + imgLen); if(pMNQ != NULL) { /* make imagePath point to the allocated buffer immediately after * the MOUNT_NEW_QUERY portion */ pMNQ->imagePath = (PWCHAR)((PBYTE)pMNQ + sizeof(MOUNT_NEW_QUERY)); /* make the key point to the allocated buffer immediately after * the imagePath portion (including a NULL WCHAR terminator) */ pMNQ->key = (PCHAR)((PBYTE)pMNQ + sizeof(MOUNT_NEW_QUERY) + imgLen); /* populate the data here appropriately, typically with strcpy * and wcscpy, and then send the IOCTL */ fun(pMNQ); } 

当为驱动程序调用IOCTL时,传递缓冲区的大小,而不仅仅是MOUNT_NEW_QUERY结构的大小。