-2

I have a function from this VB.net that needs to be imported to C#. I have tried various VB.NET to C# converters but it does not work correctly with the imported dll associated with this function. Anyone knows how to convert correctly for the following VB function to C#:

  <DllImport("E5KDAQ.dll")> _
  Public Function E5K_ReadDIStatus(ByVal id As Short,<[In](),Out()> ByRef chnval As Integer) As Short
  End Function

Using an online converter, it gives the following: Convert c# which has error

[DllImport("E5KDAQ.dll")]
public static extern short E5K_ReadDIStatus(short id, [In()] out int chnval);

enter image description here

Thomas Koh
  • 213
  • 5
  • 17
  • Try `public static extern short E5K_ReadDIStatus(short id, [In(), Out()] ref int chnval);`. – 41686d6564 stands w. Palestine Sep 05 '18 at 02:08
  • `has error` Please put the error in **text** not an image. – mjwills Sep 05 '18 at 02:14
  • 1
    In C#, `out` is used for parameters that are used for output only while `ref` is used for parameters that are used to provide input and output. In VB, `ByRef` is used for both. That's why attributes are used to qualify the `ByRef` keyword. Those attributes clearly indicate that the `chnval` parameter is to be used for both input and output so that means that `ref` must be used in C#. Because `ref` is unambiguous, you probably don't even need the attributes but, if you do include them, you obviously need both. – jmcilhinney Sep 05 '18 at 02:16
  • Thanks Ahmed Abdelhameed,your solution works – Thomas Koh Sep 05 '18 at 02:26

2 Answers2

2

From official documentation here: http://www.acceed.de/manuals/inlog/EDAM5000_Manual.pdf there is a C++ definition, plus a bit of documentation on what the second parameter is, which is really what you want to look at:

VC++: (see E5KDAQ.h)

unsigned short E5K_ReadDIStatus (int id, unsigned long *Didata);

Parameters:

id: module ID address

Didata: points to a 32-bit buffer to store DI status

So the C# definition should simply be (long and int in C++ are 32-bit)

[DllImport("E5KDAQ")]
static extern ushort E5K_ReadDIStatus(int id, ref uint Didata)
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

You can use this.

public static extern short E5K_ReadDIStatus(short id, ref int[] chnval)

Reference: Are P/Invoke [In, Out] attributes optional for marshaling arrays?

Gauravsa
  • 6,330
  • 2
  • 21
  • 30