C# C++ 字符串傳遞

能夠說新手使用P-INVOKE最開始的頭疼就是C#和C++的字符串傳遞,由於這裏涉及到兩個問題。 第一:C#的string和C++的字符串首指針如何對應。 第二:字符串還有ANSI和UNICODE(寬字符串)之分。 本文分三部分闡述: 第一:字符串指針當輸入參數, 第二:字符串指針做爲返回值, 第三:字符串指針做爲輸入輸出參數。 C++部分的測試代碼很簡單這裏就所有貼出來了: 1#include "stdafx.h" 2#include "TestDll.h" 3#include 4#include 5#include 6 7 8 staticchar* _hello ="Hello,World!!"; 9 static TCHAR * _helloW = TEXT("Hello,World!!");1011 void __stdcall PrintString(char* hello)12{13 printf("%s\n",hello);14}1516 void __stdcall PrintStringW(TCHAR * hello)17{18 _tprintf(TEXT("%s\n"),hello);19}202122 char* __stdcall GetStringReturn()23{24return _hello;25}2627TCHAR * __stdcall GetStringReturnW()28{29return _helloW;30}313233 void __stdcall GetStringParam(char* outHello,int len)34{ //output "aaaaaaaa"35 for(int i=0; i< len -1 ;i++) outHello[i] ='a';36 outHello[len -1] ='\0';37}3839 void __stdcall GetStringParamW(TCHAR * outHello,int len)40{ //output "aaaaaaaa" unicode version.41 for(int i=0; i< len -1 ;i++) outHello[i] = TEXT('a');42 outHello[len -1] = TEXT('\0');43} 下面看C#如何調用。 第一:字符串指針做爲輸入參數,能夠使用byte[] 和MarshalAs來解決。(注意四個P-INVOKE,兩個ANSI版本,和兩個UNICODE版本),推薦使用MarshalAs方法簡單明瞭。 1 [DllImport("TestDll", EntryPoint ="PrintString")] 2publicstaticexternvoid PrintStringByBytes(byte[] hello); 3 4 [DllImport("TestDll", EntryPoint ="PrintString")] 5publicstaticexternvoid PrintStringByMarshal([MarshalAs(UnmanagedType.LPStr)]string hello); 6 7 [DllImport("TestDll", EntryPoint ="PrintStringW")] 8publicstaticexternvoid PrintStringByBytesW(byte[] hello); 910 [DllImport("TestDll", EntryPoint ="PrintStringW")]11publicstaticexternvoid PrintStringByMarshalW([MarshalAs(UnmanagedType.LPWStr)]string hello);121314publicvoid Run()15 {16 PrintStringByBytes(Encoding.ASCII.GetBytes("use byte[]"));17 PrintStringByMarshal("use MarshalAs");18 PrintStringByBytesW(Encoding.Unicode.GetBytes("use byte[]"));19 PrintStringByMarshalW("use MarshalAs");20} 第二:字符串指針做爲返回值,和上面同樣也有兩種聲明方法,一樣也包含兩個版本。注意:Marshal.PtrToStringAnsi()函數的使用,把字符串指針轉變爲C#的string.推薦使用MarshalAs方法簡單明瞭。 1 [DllImport("TestDll", EntryPoint ="GetStringReturn")] 2publicstaticextern IntPtr GetStringReturnByBytes(); 3 4 [DllImport("TestDll", EntryPoint ="GetStringReturn")] 5 [return:MarshalAs(UnmanagedType.LPStr)] 6publicstaticexternstring GetStringReturnByMarshal(); 7 8 [DllImport("TestDll", EntryPoint ="GetStringReturnW")] 9publicstaticextern IntPtr GetStringReturnByBytesW();1011 [DllImport("TestDll", EntryPoint ="GetStringReturnW")]12 [return: MarshalAs(UnmanagedType.LPWStr)]13publicstaticexternstring GetStringReturnByMarshalW();141516publicvoid Run()17 { //Marshal.PtrToStringAuto(GetStringReturnByBytes()); 自動判斷類型不錯。18 Console.WriteLine(Marshal.PtrToStringAnsi(GetStringReturnByBytes()));19 Console.WriteLine(GetStringReturnByMarshal());20 Console.WriteLine(Marshal.PtrToStringUni(GetStringReturnByBytesW()));21 Console.WriteLine(GetStringReturnByMarshalW());22} 第三:字符串指針做爲輸入輸出參數時,由於要求有固定的容量,因此這裏使用的是StringBuilder,你們仔細看了,固然也有byte[]版本。這個看你們喜歡那個版本就是用那個. 1 [DllImport("TestDll", EntryPoint ="GetStringParam")] 2publicstaticexternvoid GetStringParamByBytes(byte[] outHello, int len); 3 4 [DllImport("TestDll", EntryPoint ="GetStringParam")] 5publicstaticexternvoid GetStringParamByMarshal([Out, MarshalAs(UnmanagedType.LPStr)]StringBuilder outHello, int len); 6 7 [DllImport("TestDll", EntryPoint ="GetStringParamW")] 8publicstaticexternvoid GetStringParamByBytesW(byte[] outHello, int len); 910 [DllImport("TestDll", EntryPoint ="GetStringParamW")]11publicstaticexternvoid GetStringParamByMarshalW([Out, MarshalAs(UnmanagedType.LPWStr)]StringBuilder outHello, int len);121314publicbyte[] _outHello =newbyte[10];15publicbyte[] _outHelloW =newbyte[20];16public StringBuilder _builder =new StringBuilder(10); //很重要設定string的容量。17 18publicvoid Run()19 {20//21 GetStringParamByBytes(_outHello, _outHello.Length);22 GetStringParamByMarshal(_builder, _builder.Capacity);23 GetStringParamByBytesW(_outHelloW, _outHelloW.Length /2);24 GetStringParamByMarshalW(_builder, _builder.Capacity);2526//27 Console.WriteLine(Encoding.ASCII.GetString(_outHello));28 Console.WriteLine(_builder.ToString());29 Console.WriteLine(Encoding.Unicode.GetString(_outHelloW));30 Console.WriteLine(_builder.ToString());31}函數

相關文章
相關標籤/搜索