C++中unsigned char* 对应C#的是什么?

oniman123 2013-09-24 01:52:15

C++中函数声明如下:

MethodA(unsigned char* pInputData, 
unsigned int nSize,
unsigned char* pOutputData,
unsigned int* nOutSize);


前面两个是输入变量,一个是string,一个是int nsize

后面两个是输出变量,也应该是string跟size

问题是我在C#中如何调用这个函数,声明要怎么写,如果有用到指针,指针怎么初始化?

谢谢!
...全文
12970 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
2013-10-21
  • 打赏
  • 举报
回复
楼主解决了吗,碰到同样的问题 返回参数getResult(unsigned char *code)
宝_爸 2013-09-25
  • 打赏
  • 举报
回复
找C++的人要代码,要PDB文件, 你自己单步跟踪一下,看看哪里出问题了。 你的C#的项目的属性中Debug页中的Enable native code debugging 选中就可以了。
宝_爸 2013-09-25
  • 打赏
  • 举报
回复
引用 8 楼 oniman123 的回复:
C++函数内部没问题呢,因为他用C++也调用正确了
pOutputData如果不再外面分配好空间,是传不出东西的。 看我7楼的解释。
  • 打赏
  • 举报
回复
其实也可以说“最不靠谱”的是唯一“靠谱的”——这足以说明c++是多么混沌的东西啊。 实际上你可以这样表示指针,然后c++那边则把指针换成另外一种解释。因此 神马“unsigned char”前缀其实也是浮云,根本不可靠。 因此根本不知道,指针面前一切都是苍白的、根本没有规范。
  • 打赏
  • 举报
回复
最“正确”但是最不靠谱的就是“这是指针”。
threenewbee 2013-09-24
  • 打赏
  • 举报
回复
没有直接对应的类型。你可以用byte[]接收,然后再转字符串。 理论上,任何C++类型都可以用byte[]和int接收,看你这边怎么解析了。
jshi123 2013-09-24
  • 打赏
  • 举报
回复
即可以是byte数组,也可以是string,看MethodA怎么解释传进去的这个指针。
oniman123 2013-09-24
  • 打赏
  • 举报
回复
引用 11 楼 jshi123 的回复:
[quote=引用 3 楼 oniman123 的回复:]
unsigned char* outputData = NULL; 
unsigned int nOutSize = 0;
MethodA((unsigned char*)inputData.c_str(),inputData.length(),outputData,&nOutSize);
对照翻译成c#是:

	[DllImport("xxxx.dll", CharSet=CharSet.Ansi)]
	static extern void MethodA(string pInputData, uint nSize, StringBuilder pOutputData, out uint nOutSize);

	string inputData = "xxxxx";
	uint nOutSize = 0;
	MethodA(inputData, (uint)inputData.Length, null, out nOutSize);

[/quote] 不对呢~~~现在的问题就是搞不懂 unsigned char* 对应C#是什么类型,还有怎么初始化这个类型!
宝_爸 2013-09-24
  • 打赏
  • 举报
回复
usigned char对应的是System.Byte 见 http://msdn.microsoft.com/en-us/library/ac7ay120.aspx array的话使用byte[]就可以,看C的函数声明, 我的理解应该在外面分配足够到的空间,函数内部对空间进行填充,然后返回size. 因此声明应该是这样的。 static extern void MethodAFunction(byte[] inputData, int nSize, byte[] outputdata, ref int nOutSize); 还是要咨询函数实现者,他是怎么实现函数的。
jshi123 2013-09-24
  • 打赏
  • 举报
回复
引用 3 楼 oniman123 的回复:
unsigned char* outputData = NULL; 
unsigned int nOutSize = 0;
MethodA((unsigned char*)inputData.c_str(),inputData.length(),outputData,&nOutSize);
对照翻译成c#是:

	[DllImport("xxxx.dll", CharSet=CharSet.Ansi)]
	static extern void MethodA(string pInputData, uint nSize, StringBuilder pOutputData, out uint nOutSize);

	string inputData = "xxxxx";
	uint nOutSize = 0;
	MethodA(inputData, (uint)inputData.Length, null, out nOutSize);

mrjia 2013-09-24
  • 打赏
  • 举报
回复
在c#里面是class byte 每一个byte是一个 unsigned 8-bit int, 所以如果需要一个char的指针需要 var a = new byte[];
jiaoshiyao 2013-09-24
  • 打赏
  • 举报
回复
无对应 C++是C++ C#是C#
Corner_hzd 2013-09-24
  • 打赏
  • 举报
回复
那你用StringBuilder 去接受试试
oniman123 2013-09-24
  • 打赏
  • 举报
回复
C++函数内部没问题呢,因为他用C++也调用正确了
宝_爸 2013-09-24
  • 打赏
  • 举报
回复
引用 6 楼 oniman123 的回复:
现在是前面两个参数是输入参数,后面两个参数是输出参数 inputData跟ouputData都应该是字符串。 我现在用:
 byte[] inputData, int nSize,byte[] outputData, ref int nOutSize


string inputStr = "....";
byte[] inputData = Encoding.ANSCII.GetBytes(inputStr);
int nSize = inputData.Length;

int nOutSize = 2024;
byte[] outputData = new byte[nOutSize];

MethodA(inputData, nSize, outputData, ref nOutSize);

这样调用,outputData设置结果没变。哪里出了问题?
我觉得函数内部有问题。
oniman123 2013-09-24
  • 打赏
  • 举报
回复
现在是前面两个参数是输入参数,后面两个参数是输出参数 inputData跟ouputData都应该是字符串。 我现在用:
 byte[] inputData, int nSize,byte[] outputData, ref int nOutSize


string inputStr = "....";
byte[] inputData = Encoding.ANSCII.GetBytes(inputStr);
int nSize = inputData.Length;

int nOutSize = 2024;
byte[] outputData = new byte[nOutSize];

MethodA(inputData, nSize, outputData, ref nOutSize);

这样调用,outputData设置结果没变。哪里出了问题?
宝_爸 2013-09-24
  • 打赏
  • 举报
回复
我觉得C++有点问题呢。。很久不用C++了。。。。。 unsigned char* outputData = NULL; //outputData是个空指针 unsigned int nOutSize = 0; MethodA((unsigned char*)inputData.c_str(),inputData.length(),outputData,&nOutSize);//这里将空指针传进去,没有用的,C++值传递,函数中修改pOutputData指向其他地方,不会影响outputData的。 如果想传出指针,需要这样 MethodA(unsigned char* pInputData, unsigned int nSize, unsigned char** pOutputData, unsigned int* nOutSize); unsigned char* outputData = NULL; unsigned int nOutSize = 0; MethodA((unsigned char*)inputData.c_str(),inputData.length(),&outputData,&nOutSize); 这样可以在函数中为outputData分配空间。
宝_爸 2013-09-24
  • 打赏
  • 举报
回复
outputData需要在外面释放吗?
oniman123 2013-09-24
  • 打赏
  • 举报
回复
引用 2 楼 findcaiyzh 的回复:
usigned char对应的是System.Byte 见 http://msdn.microsoft.com/en-us/library/ac7ay120.aspx array的话使用byte[]就可以,看C的函数声明, 我的理解应该在外面分配足够到的空间,函数内部对空间进行填充,然后返回size. 因此声明应该是这样的。 static extern void MethodAFunction(byte[] inputData, int nSize, byte[] outputdata, ref int nOutSize); 还是要咨询函数实现者,他是怎么实现函数的。
C++是下面的方法调用,那转为C# 要怎么调用呢?谢谢你的回答!
unsigned char* outputData = NULL; 
unsigned int nOutSize = 0;
MethodA((unsigned char*)inputData.c_str(),inputData.length(),outputData,&nOutSize);

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧