0

Structure in C++:

typedef struct _denom 
    { 
     CHAR       cCurrencyID[3]; 
     int       ulAmount; 
     short     usCount; 
     LPULONG    lpulValues; //pointer to array of ULONGS 
     int      ulCashBox;        
    } DENOMINAT, * LPDENOMINAT; 

Structure in C#:

[ StructLayout( LayoutKind.Sequential, 
CharSet=CharSet.Ansi, Pack=1 ) ] 
 public struct  DENOMINAT 
 { 
   [ MarshalAs( UnmanagedType.ByValArray, SizeConst=3 ) ] 
  public char[] cCurrencyID; 
  public int ulAmount; 
  public short usCount; 
  public IntPtr lpulValues; 
  public int ulCashBox;     
} 

lpulValues is a pointer to array of ulongs and its array size is based on the usCount for eg :if uscount is 5 then in C++ it would be lpulValues = new ULONG[usCount];.

I can easily get the array values in C++ which is not been possible in C#.I dont understand how to get the array values through IntPtr. Thanks in advance.

1 Answer 1

2

Important thing

In Windows, in C/C++, sizeof(long) == sizeof(int). So sizeof(C-long) == sizeof(C#-int). See https://stackoverflow.com/a/9689080/613130

You should be able to do:

var denominat = new DENOMINAT();

var uints = new uint[denominat.usCount];
Marshal.Copy(denominat.lpulValues, (int[])(Array)uints, 0, uints.Length);

Note that we cheat a little, casting the uint[] to a int[], but it is legal, because they differ only for the sign (see https://stackoverflow.com/a/593799/613130)

I don't think you can do it automatically with the .NET PInvoke marshaler.

To display the array:

for (int i = 0; i < uints.Length; i++)
{
    Console.WriteLine(uints[i]);
}
Sign up to request clarification or add additional context in comments.

8 Comments

@TechBrkTru See added lines
:how will i display the array values of lpulvalues for eg: if the ulongs length is 5??? In C++ i can display it as lpulValues[0] ,lpulValue[1] ...and so on how is it possible in C#
Why you don't use (ulong[]) instead of (long[])
The cast is necessary because the Marshal.Copy doesn't have an overload for ulong[], but only for long[], so I have to trick it to use the long[] overload for out ulong[] array.
@xanatos:Well i get the address values when i execute the for loop .I need to access the values stored at those address
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.