WEB开发网
开发学院软件开发C语言 c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串... 阅读

c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串和指针

 2010-09-30 22:46:12 来源:WEB开发网   
核心提示:可以说新手使用P-INVOKE最开始的头疼就是C#和C++的字符串传递,因为这里涉及到两个问题,c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串和指针,第一:C#的string和C++的字符串首指针如何对应,第二:字符串还有ANSI和UNICODE(宽字符串)之分,本文分三部分阐述:第一:字符串指针当输入

可以说新手使用P-INVOKE最开始的头疼就是C#和C++的字符串传递,因为这里涉及到两个问题。

第一:C#的string和C++的字符串首指针如何对应。

第二:字符串还有ANSI和UNICODE(宽字符串)之分。

本文分三部分阐述:

第一:字符串指针当输入参数,

第二:字符串指针作为返回值,

第三:字符串指针作为输入输出参数。

C++部分的测试代码很简单这里就全部贴出来了:

 1 #include "stdafx.h"
 2 #include "TestDll.h"
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <tchar.h>
 6 
 7 
 8  static char * _hello = "Hello,World!!";
 9  static TCHAR * _helloW = TEXT("Hello,World!!");
10 
11  void __stdcall PrintString(char * hello)
12 {
13   printf("%s\n",hello);
14 }
15 
16  void __stdcall PrintStringW(TCHAR * hello)
17 {
18   _tprintf(TEXT("%s\n"),hello);
19 }
20 
21 
22  char * __stdcall GetStringReturn()
23 {
24   return _hello;
25 }
26 
27 TCHAR * __stdcall GetStringReturnW()
28 {
29   return _helloW;
30 }
31 
32 
33  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 }
38 
39  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 }

1 2 3 4  下一页

Tags:编程 指南 平台

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接