简体   繁体   English

Windows Phone 8(WP8)C#不安全代码?

[英]Windows Phone 8(WP8) C# unsafe code?

EDIT: You can use unsafe code... you just have to manually edit the proj file. 编辑:您可以使用不安全的代码...您只需手动编辑proj文件。

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)? 当我可以在手机上使用本机C ++代码时,为什么或为什么WP8上的C#不支持不安全的代码(我没想到这一点)? I mean rly come on, I am so disappointed with what Microsoft is trying to force C# into. 我的意思是狡猾的,我对微软试图迫使C#进入的方式感到失望。

Is there any MS plans to support this in the future on WP8? 有没有MS计划在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. 我将着色器常量从C#传递给C ++,并使用不安全的代码来优化这个过程,但是在WP8上,我会被迫在速度较慢的设备上以较慢的方式执行此操作而不是PC,这使得这非常令人沮丧。

As an example to pass a vector4 to C++ I would do: 作为将vector4传递给C ++的示例,我会这样做:

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

Then in C++: 然后在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? 没有不安全的代码,任何人都有一个很好的快速方法吗?

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. 如果有人有更好的答案,那也很好。

This is exactly what C++/CX is there to address (while maintaining the integrity of the sandbox). 这正是C ++ / CX要解决的问题(同时保持沙箱的完整性)。

For instance for passing int array from C# to C++ use Platform::Array<int> in your C++/CX ref class or interface definition. 例如,要将C#中的int数组传递给C ++,请在C ++ / CX ref类或接口定义中使用Platform::Array<int> Check out MSDN for the specifics of how to use this type. 查看MSDN以了解如何使用此类型的具体信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM