开发者

Passing C parameters to a dll from Vb.net 2010

Im trying to call a C++ dll from Vb.net.The function which Iam calling is declared in the dll as giv开发者_开发技巧en below

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

where pchar is a pointer to an array of bytes(string) pdword is a pointer to a word

Pmapping is a structure as given below

**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;**

I have tried sending sequential 283 bytes since thats the total structre size but it wont work.Ive tried marshalling as given bellow but it work as well.

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 

Thanks looking forward for your suggestion

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜