C# conversion to VB .NET ?

K

Katie

Hi,
Im converting some C# RAPI code to VB .NET. The C# version works. However,
the VB version returns E_INVALIDARG

Does anyone see the conversion problem? i noted the lines that may be the
problem.

I know there are libraries available for RAPI but i can't use them

Thanks
Katie ;)


C#
version --------------------------------------------------------------------
-----
[StructLayout(LayoutKind.Sequential)]
internal struct RAPIINIT
{
public int cbSize;
public IntPtr heRapiInit;
public int hrRapiInit;
}

public boolean TryRapiConnect()
{
// non-blocking init rapi call

private IntPtr hInitEvent = IntPtr.Zero;
private int InitResult = 0;
private RAPIINIT ri;
private int retVal = 0;

ri = new RAPIINIT();

ri.cbSize = Marshal.SizeOf(ri);
ri.hrRapiInit = InitResult;

hInitEvent = CreateEvent(IntPtr.Zero, 0, 0, "RapiInitEvent");

ri.heRapiInit = hInitEvent;

retVal = CeRapiInitEx(ref ri);

}

VB
version --------------------------------------------------------------------
--
<StructLayout(LayoutKind.Sequential)> _
Public Structure RAPIINIT
Dim cbSize As Integer
Dim heRapiInit As Integer
Dim hrRapiInit As Integer
End Structure

public Function TryRapiConnect() As Boolean
'non-blocking init rapi call

Dim hInitEvent As Integer = 0
Dim InitResult As Integer = 0
Dim ri As RAPIINIT
Dim retVal as integer = 0

ri = New RAPIINIT
ri.cbSize = Marshal.SizeOf(ri)
ri.hrRapiInit = 0

hInitEvent = CreateEvent(0, 0, 0, "RapiInitEvent") '<<< maybe problem
here with first arg

ri.heRapiInit = hInitEvent

retVal = CeRapiInitEx(ri) '<<< maybe problem here with arg. C# version
uses (ref ri). what should it be in VB?

End Function
 
K

Katie

here is the dllimport for C# and VB .NET
C#----
[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
internal static extern int CeRapiInitEx ([MarshalAs(UnmanagedType.Struct)]
ref RAPIINIT pRapiInit);

VB---
<DllImport("rapi.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function CeRapiInitEx(<MarshalAs(UnmanagedType.Struct)>
ByVal prapiinit As RAPIINIT) As Integer
End Function

Katie said:
Hi,
Im converting some C# RAPI code to VB .NET. The C# version works. However,
the VB version returns E_INVALIDARG

Does anyone see the conversion problem? i noted the lines that may be the
problem.

I know there are libraries available for RAPI but i can't use them

Thanks
Katie ;)


C#
version --------------------------------------------------------------------
-----
[StructLayout(LayoutKind.Sequential)]
internal struct RAPIINIT
{
public int cbSize;
public IntPtr heRapiInit;
public int hrRapiInit;
}

public boolean TryRapiConnect()
{
// non-blocking init rapi call

private IntPtr hInitEvent = IntPtr.Zero;
private int InitResult = 0;
private RAPIINIT ri;
private int retVal = 0;

ri = new RAPIINIT();

ri.cbSize = Marshal.SizeOf(ri);
ri.hrRapiInit = InitResult;

hInitEvent = CreateEvent(IntPtr.Zero, 0, 0, "RapiInitEvent");

ri.heRapiInit = hInitEvent;

retVal = CeRapiInitEx(ref ri);

}

VB
version --------------------------------------------------------------------
 
P

Peter Foot [MVP]

Your C# declaration is passing by reference (ref), and your VB version is
passing by value (ByVal), changing this in your VB code to ByRef should fix
it.

Peter

--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com

Katie said:
here is the dllimport for C# and VB .NET
C#----
[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
internal static extern int CeRapiInitEx ([MarshalAs(UnmanagedType.Struct)]
ref RAPIINIT pRapiInit);

VB---
<DllImport("rapi.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function CeRapiInitEx(<MarshalAs(UnmanagedType.Struct)>
ByVal prapiinit As RAPIINIT) As Integer
End Function

Katie said:
Hi,
Im converting some C# RAPI code to VB .NET. The C# version works. However,
the VB version returns E_INVALIDARG

Does anyone see the conversion problem? i noted the lines that may be the
problem.

I know there are libraries available for RAPI but i can't use them

Thanks
Katie ;)


C#
version --------------------------------------------------------------------
-----
[StructLayout(LayoutKind.Sequential)]
internal struct RAPIINIT
{
public int cbSize;
public IntPtr heRapiInit;
public int hrRapiInit;
}

public boolean TryRapiConnect()
{
// non-blocking init rapi call

private IntPtr hInitEvent = IntPtr.Zero;
private int InitResult = 0;
private RAPIINIT ri;
private int retVal = 0;

ri = new RAPIINIT();

ri.cbSize = Marshal.SizeOf(ri);
ri.hrRapiInit = InitResult;

hInitEvent = CreateEvent(IntPtr.Zero, 0, 0, "RapiInitEvent");

ri.heRapiInit = hInitEvent;

retVal = CeRapiInitEx(ref ri);

}

VB
version --------------------------------------------------------------------
 
C

Chris Tacke, eMVP

Out of curiosity, why not just use the OpenNETCF RAPI dll as it is?

--
Chris Tacke, eMVP
Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Katie said:
Hi,
Im converting some C# RAPI code to VB .NET. The C# version works. However,
the VB version returns E_INVALIDARG

Does anyone see the conversion problem? i noted the lines that may be the
problem.

I know there are libraries available for RAPI but i can't use them

Thanks
Katie ;)


C#
version --------------------------------------------------------------------
-----
[StructLayout(LayoutKind.Sequential)]
internal struct RAPIINIT
{
public int cbSize;
public IntPtr heRapiInit;
public int hrRapiInit;
}

public boolean TryRapiConnect()
{
// non-blocking init rapi call

private IntPtr hInitEvent = IntPtr.Zero;
private int InitResult = 0;
private RAPIINIT ri;
private int retVal = 0;

ri = new RAPIINIT();

ri.cbSize = Marshal.SizeOf(ri);
ri.hrRapiInit = InitResult;

hInitEvent = CreateEvent(IntPtr.Zero, 0, 0, "RapiInitEvent");

ri.heRapiInit = hInitEvent;

retVal = CeRapiInitEx(ref ri);

}

VB
version --------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top