I declared a method of in C# like this:
[OperationContract]
[FaultContract(typeof(MyException))]
MyClass MyMethod(... some params ..., Int32[] myParam);
And in C++/CLI a need to write the method matching the interface:
MyClass^ MyMethod(... some params ..., array<long>^ myParam) { ...
I need to trasfer array of longs for C++ world from .Net. I know that C++ long is not the .Net long. But I don’t know how to make this.
In the C++ compiler for MSVC,
longandinthave the same size. I’m not sure if you’re thinking oflong longwhich represents a 64-bit signed integer in MSVC. If you mean justlongthough, thenInt32within .Net should be fine.To be really safe, you can use the provided macros for signed 32-bit integers:
Or better yet, using the .NET defined types as Alexandre C recommends:
The most important thing to take note of is that
int,long,int32_t, andSystem::Int32are all the same exact size in the current MSVC C++ compiler. It doesn’t matter which you use, butint32_tandSystem::Int32are the safest choices.Microsoft can change their
longat a later date to be a size larger 32-bits. If that were to happen, then you could recompile this same code with the new compiler with zero issues.With regards to what size each data type is, the standard requires that
intandlongare at least 4 bytes large. On some compilers, you may find thatsizeof(long) != sizeof(int). For that reason, if you want to make sure that you’re using integers that are exactly 4 bytes big you should use the provided headers that guarantee the required size.For more details see here: http://en.wikipedia.org/wiki/Long_integer. The article includes the relevant links to the standards.