wstring不是.NET内部支持的string类型,下面是MSDN解释:下表列出了在 Win32 API(在 Wtypes.h 中列出)和 C 样式函数中使用的数据类型。许多非托管库包含将这些数据类型作为参数传递并返回值的函数。第三列列出了在托管代码中使用的相应的 .NET Framework 内置值类型或类。某些情况下,您可以用大小相同的类型替换此表中列出的类型。Wtypes.h 中的非托管类型 非托管 C 语言类型 托管类名 说明 HANDLE void* System.IntPtr 在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。 BYTE unsigned char System.Byte 8 位 SHORT short System.Int16 16 位 WORD unsigned short System.UInt16 16 位 INT int System.Int32 32 位 UINT unsigned int System.UInt32 32 位 LONG long System.Int32 32 位 BOOL long System.Int32 32 位 DWORD unsigned long System.UInt32 32 位 ULONG unsigned long System.UInt32 32 位 CHAR char System.Char 用 ANSI 修饰。 LPSTR char* System.String 或 System.Text.StringBuilder 用 ANSI 修饰。 LPCSTR Const char* System.String 或 System.Text.StringBuilder 用 ANSI 修饰。 LPWSTR wchar_t* System.String 或 System.Text.StringBuilder 用 Unicode 修饰。 LPCWSTR Const wchar_t* System.String 或 System.Text.StringBuilder 用 Unicode 修饰。 FLOAT Float System.Single 32 位 DOUBLE Double System.Double 64 位wstring的实质是: typedef basic_string<wchar_t> wstring;The sequences controlled by an object of template class basic_string are the Standard C++ string class and are usually referred to as strings, but they should not be confused with the null-terminated C-strings used throughout the Standard C++ Library. The string class is a container that enables the use of strings as normal types, such as using comparison and concatenation operations, iterators, and STL algorithms and copying and assigning with class allocator managed memory.所以wstring是不可能像lz那样封装,lz可以在.NET这边把string写到非托管内存(Marshal操作就可以了),声明PInvoke市传入传出都使用IntPtr(指针)操作!
贴点代码看看。
c#是这么写的:
[DllImport("dll.dll", EntryPoint = "getFolderPath", CharSet = CharSet.Unicode)]
static extern string getFolderPath(StringBuilder type);
HANDLE void* System.IntPtr 在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。
BYTE unsigned char System.Byte 8 位
SHORT short System.Int16 16 位
WORD unsigned short System.UInt16 16 位
INT int System.Int32 32 位
UINT unsigned int System.UInt32 32 位
LONG long System.Int32 32 位
BOOL long System.Int32 32 位
DWORD unsigned long System.UInt32 32 位
ULONG unsigned long System.UInt32 32 位
CHAR char System.Char 用 ANSI 修饰。
LPSTR char* System.String 或 System.Text.StringBuilder 用 ANSI 修饰。
LPCSTR Const char* System.String 或 System.Text.StringBuilder 用 ANSI 修饰。
LPWSTR wchar_t* System.String 或 System.Text.StringBuilder 用 Unicode 修饰。
LPCWSTR Const wchar_t* System.String 或 System.Text.StringBuilder 用 Unicode 修饰。
FLOAT Float System.Single 32 位
DOUBLE Double System.Double 64 位wstring的实质是:
typedef basic_string<wchar_t> wstring;The sequences controlled by an object of template class basic_string are the Standard C++ string class and are usually referred to as strings, but they should not be confused with the null-terminated C-strings used throughout the Standard C++ Library. The string class is a container that enables the use of strings as normal types, such as using comparison and concatenation operations, iterators, and STL algorithms and copying and assigning with class allocator managed memory.所以wstring是不可能像lz那样封装,lz可以在.NET这边把string写到非托管内存(Marshal操作就可以了),声明PInvoke市传入传出都使用IntPtr(指针)操作!
[DllImport]
static extern string getDefaultFolderPath1([MarshalAs(UnmanagedType.LPWStr)]StringBuilder type);
但是效果还是一样,传入后得到的是乱码
static extern IntPtr getFolderPath(IntPtr type);
现在我这么写,
[DllImport("dll.dll", EntryPoint = "getFolderPath", CharSet = CharSet.Unicode)]
static extern IntPtr getDefaultFolderPath(IntPtr type);
...
IntPtr hGlobal = Marshal.StringToHGlobalUni(str);
string path=Marshal.PtrToStringUni(getDefaultFolderPath(hGlobal));调用以后,出现“尝试读取或写入受保护的内存”,查了好久也没查出什么
这是什么原因 呢?
http://www.hb427kys.com