vcclr.h中存在的PtrToStringChars(字符串s)函数的C#等价物 我有一个本地C++函数,希望导入到我的C项目文件中。

vcclr.h中存在的PtrToStringChars(字符串s)函数的C#等价物 我有一个本地C++函数,希望导入到我的C项目文件中。,c#,c++,visual-studio,dll,C#,C++,Visual Studio,Dll,该文件是vcclr.h,它位于Visual Studio 9.0/VC/include文件夹中。函数为PtrToStringChars(字符串s) 是否有一个等价的C++函数来代替这个本地C++函数?< /P> 此外,如果希望导入此函数,则需要指定包含此文件的dll的名称。如何获取此dll的名称 < >我可以看到包含这个函数的C++文件,从那里我可以看到它的文件夹位置(绝对路径和相对路径)。 不需要那个方法: string str = "hello".Substring(0, 2); // fo

该文件是vcclr.h,它位于Visual Studio 9.0/VC/include文件夹中。函数为
PtrToStringChars(字符串s)

是否有一个等价的C++函数来代替这个本地C++函数?< /P> 此外,如果希望导入此函数,则需要指定包含此文件的dll的名称。如何获取此dll的名称


< >我可以看到包含这个函数的C++文件,从那里我可以看到它的文件夹位置(绝对路径和相对路径)。

不需要那个方法:

string str = "hello".Substring(0, 2); // force not to inline the string
                                      // it would be the same if it was inlined

GCHandle h = GCHandle.Alloc(str, GCHandleType.Pinned);
IntPtr ptr = h.AddrOfPinnedObject(); // Your address

// ch is h
// the unchecked is necessary (technically optional because it's default)
// because Marshal.ReadInt16 reads a signed Int16, while char is more similar
// to an unsigned Int16
char ch = unchecked((char)Marshal.ReadInt16(ptr));
或者使用一些不安全的代码(请参阅):


如果您想要确切地PtrToStringChars(…),该怎么办?你不能拥有它。。。它直接在
vcclr.h
标题中定义为一个内联函数(在我的第37-39行中,注释是40-48行,代码是40-48行)

您不需要这种方法:

string str = "hello".Substring(0, 2); // force not to inline the string
                                      // it would be the same if it was inlined

GCHandle h = GCHandle.Alloc(str, GCHandleType.Pinned);
IntPtr ptr = h.AddrOfPinnedObject(); // Your address

// ch is h
// the unchecked is necessary (technically optional because it's default)
// because Marshal.ReadInt16 reads a signed Int16, while char is more similar
// to an unsigned Int16
char ch = unchecked((char)Marshal.ReadInt16(ptr));
或者使用一些不安全的代码(请参阅):


如果您想要确切地PtrToStringChars(…),该怎么办?你不能拥有它。。。它直接在
vcclr.h
标题中定义为一个内联函数(在我的第37-39行中,注释是40-48行,代码是40-48行)

也许如果您给我们函数名?函数名是vcclr.h文件中的PtrToStringChars(字符串s)?也许如果您给我们函数名?函数名是vcclr.h文件中的PtrToStringChars(字符串s)