VB.NET implementation of DllImport from C#?

  • Thread starter Vagabond Software
  • Start date
V

Vagabond Software

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("cabinet.dll",
CallingConvention=CallingConvention.Cdecl,
EntryPoint="FDICreate")]
private static extern IntPtr FdiCreate(
FdiMemAllocDelegate fnMemAlloc,
FdiMemFreeDelegate fnMemFree,
FdiFileOpenDelegate fnFileOpen,
FdiFileReadDelegate fnFileRead,
FdiFileWriteDelegate fnFileWrite,
FdiFileCloseDelegate fnFileClose,
FdiFileSeekDelegate fnFileSeek,
int cpuType, // ignored by 32-bit FDI
[In, Out][MarshalAs(UnmanagedType.LPStruct)] 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(
FdiMemAllocDelegate fnMemAlloc,
FdiMemFreeDelegate fnMemFree,
FdiFileOpenDelegate fnFileOpen,
FdiFileReadDelegate fnFileRead,
FdiFileWriteDelegate fnFileWrite,
FdiFileCloseDelegate fnFileClose,
FdiFileSeekDelegate fnFileSeek,
CabError erf)
{
return FdiCreate(fnMemAlloc, fnMemFree, fnFileOpen, fnFileRead,
fnFileWrite,
fnFileClose, fnFileSeek, cpuTypeUnknown, erf);
}

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

<DllImport("cabinet.dll", CallingConvention:=CallingConvention.Cdecl, _
EntryPoint:="FDICreate")> _
Private Shared Function FDICreate(ByVal pfnAlloc As FdiMemAllocDelegate, _
ByVal pfnFree As FdiMemFreeDelegate, _
ByVal pfnOpen As FdiFileOpenDelegate, _
ByVal pfnRead As FdiFileReadDelegate, _
ByVal pfnWrite As FdiFileWriteDelegate, _
ByVal pfnClose As FdiFileCloseDelegate, _
ByVal pfnSeek As FdiFileSeekDelegate, _
ByVal cpuType As Integer, <MarshalAs(UnmanagedType.LPStruct)> _
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
 
T

Tom Shelton

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("cabinet.dll",
CallingConvention=CallingConvention.Cdecl,
EntryPoint="FDICreate")]
private static extern IntPtr FdiCreate(
FdiMemAllocDelegate fnMemAlloc,
FdiMemFreeDelegate fnMemFree,
FdiFileOpenDelegate fnFileOpen,
FdiFileReadDelegate fnFileRead,
FdiFileWriteDelegate fnFileWrite,
FdiFileCloseDelegate fnFileClose,
FdiFileSeekDelegate fnFileSeek,
int cpuType, // ignored by 32-bit FDI
[In, Out][MarshalAs(UnmanagedType.LPStruct)] 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(
FdiMemAllocDelegate fnMemAlloc,
FdiMemFreeDelegate fnMemFree,
FdiFileOpenDelegate fnFileOpen,
FdiFileReadDelegate fnFileRead,
FdiFileWriteDelegate fnFileWrite,
FdiFileCloseDelegate fnFileClose,
FdiFileSeekDelegate fnFileSeek,
CabError erf)
{
return FdiCreate(fnMemAlloc, fnMemFree, fnFileOpen, fnFileRead,
fnFileWrite,
fnFileClose, fnFileSeek, cpuTypeUnknown, erf);
}

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

<DllImport("cabinet.dll", CallingConvention:=CallingConvention.Cdecl, _
EntryPoint:="FDICreate")> _
Private Shared Function FDICreate(ByVal pfnAlloc As FdiMemAllocDelegate, _
ByVal pfnFree As FdiMemFreeDelegate, _
ByVal pfnOpen As FdiFileOpenDelegate, _
ByVal pfnRead As FdiFileReadDelegate, _
ByVal pfnWrite As FdiFileWriteDelegate, _
ByVal pfnClose As FdiFileCloseDelegate, _
ByVal pfnSeek As FdiFileSeekDelegate, _
ByVal cpuType As Integer, <MarshalAs(UnmanagedType.LPStruct)> _
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

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