VB.NET implementation of DllImport from C#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vagabond Software

    VB.NET implementation of DllImport from C#?

    I am trying to convert a C# .NET wrapper for Microsoft's unmanaged
    'cabinet.dll' to Visual Basic .NET and have hit a stumbling block. Here is
    the C# code that I am trying to convert:

    [DllImport("cabi net.dll",
    CallingConventi on=CallingConve ntion.Cdecl,
    EntryPoint="FDI Create")]
    private static extern IntPtr FdiCreate(
    FdiMemAllocDele gate fnMemAlloc,
    FdiMemFreeDeleg ate fnMemFree,
    FdiFileOpenDele gate fnFileOpen,
    FdiFileReadDele gate fnFileRead,
    FdiFileWriteDel egate fnFileWrite,
    FdiFileCloseDel egate fnFileClose,
    FdiFileSeekDele gate fnFileSeek,
    int cpuType, // ignored by 32-bit FDI
    [In, Out][MarshalAs(Unman agedType.LPStru ct)] CabError erf);

    As you can see, this method is defined but without any '{}' brackets. It
    also uses the C# 'extern' keyword. A method of the same name is then
    declared publicly as follows:

    public static IntPtr FdiCreate(
    FdiMemAllocDele gate fnMemAlloc,
    FdiMemFreeDeleg ate fnMemFree,
    FdiFileOpenDele gate fnFileOpen,
    FdiFileReadDele gate fnFileRead,
    FdiFileWriteDel egate fnFileWrite,
    FdiFileCloseDel egate fnFileClose,
    FdiFileSeekDele gate fnFileSeek,
    CabError erf)
    {
    return FdiCreate(fnMem Alloc, fnMemFree, fnFileOpen, fnFileRead,
    fnFileWrite,
    fnFileClose, fnFileSeek, cpuTypeUnknown, erf);
    }

    My attempt at the converting the first code snippet to VB.NET looks like
    this:

    <DllImport("cab inet.dll", CallingConventi on:=CallingConv ention.Cdecl, _
    EntryPoint:="FD ICreate")> _
    Private Shared Function FDICreate(ByVal pfnAlloc As FdiMemAllocDele gate, _
    ByVal pfnFree As FdiMemFreeDeleg ate, _
    ByVal pfnOpen As FdiFileOpenDele gate, _
    ByVal pfnRead As FdiFileReadDele gate, _
    ByVal pfnWrite As FdiFileWriteDel egate, _
    ByVal pfnClose As FdiFileCloseDel egate, _
    ByVal pfnSeek As FdiFileSeekDele gate, _
    ByVal cpuType As Integer, <MarshalAs(Unma nagedType.LPStr uct)> _
    ByRef perf As CabError) As IntPtr
    End Function

    Basically, I'm a little lost on the translation. Visual Studio seems to be
    allowing me to create another 'Public' funciton named FDICreate, just like
    in the C# version, but if there is a problem in my initial attempt at
    conversion, I'd like a heads-up now.

    Thanks in advance for any help.

    Carl


  • Tom Shelton

    #2
    Re: VB.NET implementation of DllImport from C#?

    In article <#tNTckDyFHA.37 56@tk2msftngp13 .phx.gbl>, Vagabond Software wrote:[color=blue]
    > I am trying to convert a C# .NET wrapper for Microsoft's unmanaged
    > 'cabinet.dll' to Visual Basic .NET and have hit a stumbling block. Here is
    > the C# code that I am trying to convert:
    >
    > [DllImport("cabi net.dll",
    > CallingConventi on=CallingConve ntion.Cdecl,
    > EntryPoint="FDI Create")]
    > private static extern IntPtr FdiCreate(
    > FdiMemAllocDele gate fnMemAlloc,
    > FdiMemFreeDeleg ate fnMemFree,
    > FdiFileOpenDele gate fnFileOpen,
    > FdiFileReadDele gate fnFileRead,
    > FdiFileWriteDel egate fnFileWrite,
    > FdiFileCloseDel egate fnFileClose,
    > FdiFileSeekDele gate fnFileSeek,
    > int cpuType, // ignored by 32-bit FDI
    > [In, Out][MarshalAs(Unman agedType.LPStru ct)] CabError erf);
    >
    > As you can see, this method is defined but without any '{}' brackets. It
    > also uses the C# 'extern' keyword. A method of the same name is then
    > declared publicly as follows:
    >
    > public static IntPtr FdiCreate(
    > FdiMemAllocDele gate fnMemAlloc,
    > FdiMemFreeDeleg ate fnMemFree,
    > FdiFileOpenDele gate fnFileOpen,
    > FdiFileReadDele gate fnFileRead,
    > FdiFileWriteDel egate fnFileWrite,
    > FdiFileCloseDel egate fnFileClose,
    > FdiFileSeekDele gate fnFileSeek,
    > CabError erf)
    > {
    > return FdiCreate(fnMem Alloc, fnMemFree, fnFileOpen, fnFileRead,
    > fnFileWrite,
    > fnFileClose, fnFileSeek, cpuTypeUnknown, erf);
    > }
    >
    > My attempt at the converting the first code snippet to VB.NET looks like
    > this:
    >
    ><DllImport("ca binet.dll", CallingConventi on:=CallingConv ention.Cdecl, _
    > EntryPoint:="FD ICreate")> _
    > Private Shared Function FDICreate(ByVal pfnAlloc As FdiMemAllocDele gate, _
    > ByVal pfnFree As FdiMemFreeDeleg ate, _
    > ByVal pfnOpen As FdiFileOpenDele gate, _
    > ByVal pfnRead As FdiFileReadDele gate, _
    > ByVal pfnWrite As FdiFileWriteDel egate, _
    > ByVal pfnClose As FdiFileCloseDel egate, _
    > ByVal pfnSeek As FdiFileSeekDele gate, _
    > ByVal cpuType As Integer, <MarshalAs(Unma nagedType.LPStr uct)> _
    > ByRef perf As CabError) As IntPtr
    > End Function
    >
    > Basically, I'm a little lost on the translation. Visual Studio seems to be
    > allowing me to create another 'Public' funciton named FDICreate, just like
    > in the C# version, but if there is a problem in my initial attempt at
    > conversion, I'd like a heads-up now.
    >
    > Thanks in advance for any help.
    >
    > Carl
    >
    >[/color]

    Quick glance, and with out the definitions of the delegates/structs - it
    looks ok. The only real comment I have is that the
    MashalAs(Unmana gedType.LPStruc t) is probably unnecessary (in both
    versions actually).

    --
    Tom Shelton

    Comment

    Working...