Windows Phone 8(WP8) C# unsafe code?

我是研究僧i 提交于 2019-12-05 01:05:52

问题


EDIT: You can use unsafe code... you just have to manually edit the proj file.

Why or why does C# on WP8 not support unsafe code when I can use native C++ code on the phone(I did not expect this)? I mean rly come on, I am so disappointed with what Microsoft is trying to force C# into.

Is there any MS plans to support this in the future on WP8? I pass shader constants from C# to C++ and use unsafe code to optimize this process but on WP8 i'm going to be forced into doing this in some slow fashion on a WAY slower device compared to a PCs and it makes this very frustrating.

As an example to pass a vector4 to C++ I would do:

public unsafe void Set(Vector4 value)
{
    com.Set((int)&value, sizeof(Vector4));
}

Then in C++:

void ShaderVariableCom::Set(__int32 data, int size)
{
    void* ptr = (void*)data;
    if (vertexOffset != -1) memcpy(vertexBytes + vertexOffset, ptr, size);
    if (pixelOffset != -1) memcpy(pixelBytes + pixelOffset, ptr, size);
}

Anyone have a good fast way of doing this without unsafe code?


回答1:


I found a solution.

For simple vector types just do:

Marshal.StructureToPtr(vector, nativePtr, false);

And for arrays do:

var data = new Vector[10];
int size = Marshal.SizeOf(data[0]);
for (int i = 0; i != data.Length; ++i)
{
    Marshal.StructureToPtr(data[i], nativePtr + (size * i), false);
}

If anyone has a better answer that would be good to know too.




回答2:


This is exactly what C++/CX is there to address (while maintaining the integrity of the sandbox).

For instance for passing int array from C# to C++ use Platform::Array<int> in your C++/CX ref class or interface definition. Check out MSDN for the specifics of how to use this type.

( http://msdn.microsoft.com/en-us/library/windows/apps/hh700131.aspx )



来源:https://stackoverflow.com/questions/14432361/windows-phone-8wp8-c-sharp-unsafe-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!