从Vb.net 2010将C参数传递给dll

时间:2011-08-12 10:35:02

标签: c vb.net

我试图从Vb.net调用一个C ++ dll.Iam调用的函数在dll中声明,如下所示

extern "C" DWORD PASCAL EXPORT STDFUPRT_CreateMappingFromDevice(PCHAR szDevLink, PMAPPING *ppMapping, PDWORD pNbAlternates)

其中pchar是指向字节数组的指针(字符串) pdword是指向单词的指针

Pmapping是一种结构,如下所示

**typedef struct {

DWORD       dwStartAddress;
DWORD       dwAliasedAddress;
DWORD       dwSectorIndex;
DWORD       dwSectorSize;
BYTE        bSectorType;
BOOL        UseForOperation;
} MAPPINGSECTOR, *PMAPPINGSECTOR;

typedef struct {

BYTE            nAlternate;
char            Name[MAX_PATH];
DWORD           NbSectors;
PMAPPINGSECTOR  pSectors;   
} MAPPING, *PMAPPING;**

我已经尝试过发送连续的283个字节,因为这是总的结构大小但它不会工作。我尝试过编组,但它也可以工作。

Structure PMAPPING
    Dim nAlternate As Byte

    Dim name As IntPtr
    Dim nbsector As Int32
    Dim psector As IntPtr

End Structure
    Dim m_pMapping As PMAPPING
    m_pMapping.name = Marshal.AllocHGlobal(259)
    m_pMapping.psector = Marshal.AllocHGlobal(17)

    Dim tmpbuffer() As Byte
    'load symbolic link
    tmpbuffer = System.Text.Encoding.ASCII.GetBytes(devpathstr)

    'Context.szdevlink = devpathstr
    dwRet = STDFUPRT_CreateMappingFromDevice(tmpbuffer, m_pMapping, alt2)
    'check return string value        
    Dim teststr As String
    teststr = Marshal.PtrToStringAuto(m_pMapping.name)
    'teststr returns garbage value 

感谢期待您的建议

0 个答案:

没有答案