-1
ServerInfo = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(BufferPtr, GetType(SERVER_INFO_100)), SERVER_INFO_100)

I need to convert this CType to C#. I am not aware on VB.net. Please suggest how to do this and also forloop

For index = 0 To iEntriesRead - 1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sandy1234
  • 9
  • 2
  • 2
    You also have `SERVER_INFO_100 serverInfo = Marshal.PtrToStructure(bufferPtr);` No cast needed. – Jimi Jul 03 '21 at 12:01
  • this works perfectly 'BufferPtr = CType(CType(BufferPtr, Long) + CType(Marshal.SizeOf(ServerInfo), Long), IntPtr)' pleas convert this line too – Sandy1234 Jul 03 '21 at 12:24
  • I *think* the most direct equivalent of `CType` in VB is `Convert.ChangeType` in the .NET framework. Note that this returns `object` so it would require a cast. – Craig Jul 06 '21 at 14:35

2 Answers2

2

CType is a cast operator/function, so comparable to (SERVER_INFO_100) object in C#.

ServerInfo = (SERVER_INFO_100) System.Runtime.InteropServices.Marshal.PtrToStructure(BufferPtr, GetType(SERVER_INFO_100));

The closest you get to the C# cast operator is DirectCast in VB.NET. Read: Difference between DirectCast() and CType() in VB.NET

In C# you could also use the as cast operator which is the same as the VB.NET TryCast.

ServerInfo = System.Runtime.InteropServices.Marshal.PtrToStructure(BufferPtr, GetType(SERVER_INFO_100)) as SERVER_INFO_100;

This has the advantage that you don't get an exception if the type is not SERVER_INFO_100.

Please suggest how to do this and also forloop

The for-loop is explained in the docs.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • While it's true that you don't get an `InvalidCastException` if the type is wrong with `as` (or `TryCast` in VB), you do then have to consider the possibility that you get `null`. (This is a bit of a pet peeve of mine as I deal with a fair amount of code that uses `as` instead of a direct cast but then always treats it as if it succeeded... with this usage, it should be a direct cast so that you get the `InvalidCastException` on failure instead of a vague `NullReferenceException` later!) – Craig Jul 06 '21 at 14:32
0

you can use "as" in C# for casting

For eg:

ServerInfo = Marshal.PtrToStructure(BufferPtr, typeof(SERVER_INFO_100)) as SERVER_INFO_100;

For the For loop in C# you can follow below code

for(int index=0;index< iEntriesRead - 1;index++)
{
  //your code
}
Amit Verma
  • 2,450
  • 2
  • 8
  • 21
  • Thank u for u r share...for For loop its ok...but where to give "as" for ctype – Sandy1234 Jul 03 '21 at 11:35
  • I don't have editor so I can not compile changes. I have modified answer. Let me know if there is any issue. – Amit Verma Jul 03 '21 at 11:40
  • You may need to continue that for-loop until `index – Hans Kesting Jul 03 '21 at 13:41
  • `as` corresponds to VB `TryCast`. You should never use `as` unless you are explicitly dealing with the potential `null`; in particular, if you pretend it's a plain cast, you are likely to convert an `InvalidCastException` at the point of conversion (very clear about what's wrong) to a `NullReferenceException` at some future access (much less clear about what's wrong). – Craig Jul 06 '21 at 14:30