• Welcome to Valhalla Legends Archive.
 

[C++.Net]Passing Arrays in VC++.Net

Started by shout, April 06, 2005, 07:55 PM

Previous topic - Next topic

shout

I looked for a little bit on google but I could not find anything.

What I am trying to do is re-write my C# BnetHashing.dll library in C++, and have all the parameters and returns pointer-less.

So in C# for the decleration of the method I would have

public static byte[] HashData(byte[] Data)
{...}

and from what I have pieced together, C++ would be something like

public:
       static System::Byte __gc* HashData(System::Byte __gc* data)
       {...}


I could not find anything on returning. :'(

MyndFyre

#1
Quote from: Shout on April 06, 2005, 07:55 PM
I looked for a little bit on google but I could not find anything.

What I am trying to do is re-write my C# BnetHashing.dll library in C++, and have all the parameters and returns pointer-less.

So in C# for the decleration of the method I would have

public static byte[] HashData(byte[] Data)
{...}

and from what I have pieced together, C++ would be something like

public:
       static System::Byte __gc* HashData(System::Byte __gc* data)
       {...}


I could not find anything on returning. :'(

Well, in .NET, arrays are strongly typed.  I don't think you want to return a Byte*, either.

This is from the MSDN Guid.ToByteArray() method:

public: unsigned char ToByteArray()  __gc[];


So what do I recommend for your conversion of the C# function signature?


typedef unsigned char byte;

public:
  byte __gc[] HashData(byte __gc[] data) const;


The static modifier does something different in C/++ than it does in C#: specifically, it means that a value in a variable *inside a function* should be used across each function call.  Static in C# simply means that the function or field belongs to the type and not an instance of the type.

The HashData function should be able to be called in any of these ways:

hashedArray = MyClass::HashData(data);

MyClass obj;
hashedArray = obj::HashData(data);

MyClass* pobj = new MyClass;
hashedArray = pobj->HashData(data);
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

shout

#2
Fiddleing around, I found that this syntax works exactly how I wanted it to.

typedef unsigned char byte

public:
       System::Byte HashData(System::Byte __gc[] Data) __gc[];

Adron

Quote from: MyndFyre on April 06, 2005, 08:51 PM
The static modifier does something different in C/++ than it does in C#: specifically, it means that a value in a variable *inside a function* should be used across each function call.

The static modifier in C++ has many meanings. Your link describes them. Yes, as applied to variables defined in a function, it means that the data should be stored in the data segment instead of on the stack. That way it's instanced once for the entire program instead of once per copy of the function running.

When applied to variables defined in a class, it means the same thing - instanced once for the entire program instead of once per instance of the class. That way you don't need to create an instance of the class to be able to access it.

When applied to a function defined in a class, it means that the function doesn't access any members of the class that aren't static, i.e. that it doesn't need an instance of the class to function.

I'd say that "static" as used for class members in C++ corresponds very closely to the C# usage of static. Go ahead and use it :)