小智 45
那么你当然可以这样做:
string str = "my string";
unsafe
{
fixed (char* p = str)
{
// do some work
}
}
Run Code Online (Sandbox Code Playgroud)
其中有一个运算符(char*)绑定到字符串对象.但是,输出格式可能与底层C或其他格式不兼容......但这是解析字符串的一个很好的解决方案.希望对阅读这篇文章的人有所帮助.
Geo*_*off 13
你可以传递StringBuilder
一个char*
.
看看http://pinvoke.net,看看该函数的签名是否已经存在.
以下代码段不需要不安全的上下文(是的,它显示了有关string
的GetHashCode
方法实现的内部细节,这表明它与Java中的代码有所不同,因为在C#中,哈希码的值未缓存,通常它显示C#字符串最终不会像您可能已经教过的那样是不变的,无法与Fon Neiman交往):
using System;
using System.Runtime.InteropServices;
namespace Guess
{
class Program
{
static void Main(string[] args)
{
const string str = "ABC";
Console.WriteLine(str);
Console.WriteLine(str.GetHashCode());
var handle = GCHandle.Alloc(str, GCHandleType.Pinned);
try
{
Marshal.WriteInt16(handle.AddrOfPinnedObject(), 4, 'Z');
Console.WriteLine(str);
Console.WriteLine(str.GetHashCode());
}
finally
{
handle.Free();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这取决于你想做什么。当您通过 PInvoke 调用 Win32 函数时,您应该能够仅传递 String 变量;该框架为您整理了一切。如果您需要更复杂的东西,请查看Marshal.StringToHGlobalAnsi和该类的其他方法Marshal
。
归档时间: |
|
查看次数: |
67801 次 |
最近记录: |