2013-04-26 33 views
1

我正在寫虛擬磁盤驅動器,並有我有這樣的定義的結構:傳遞從用戶模式可變長度結構來內核模式

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

所以我有幾分動態大小的結構。

我該如何將它從用戶模式傳遞給我的驅動程序?

+2

想''foo \ 0bar \ 0 \ baz \ 0「' – 2013-04-26 00:11:55

回答

2

分配內存連續塊,足以同時容納你的結構和「重點」和「路徑」的數據 - 這樣的事情:

/* 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結構的大小。

+0

哦,這很容易......我想我明白了。謝謝。 抱歉不能投票,沒有足夠的聲望。 – externum 2013-04-26 00:22:29

+0

嗯...還有一個問題。我應該添加數組的長度結構? ** UPD:**再次抱歉。看來我需要更多的睡眠。 =) – externum 2013-04-26 00:29:51

+0

睡眠很好。不用擔心,祝你好運! – 2013-04-26 00:35:33

相關問題