还是c#引用c++的问题,关于传递结构的,实在是太纠结了,高手帮忙

abutwang 2011-09-05 06:07:37
c++ GLOBAL
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#ifndef int8_t
#define int8_t char
#endif

#ifndef uint8_t
#define uint8_t unsigned char
#endif

#ifndef int16_t
#define int16_t short
#endif

#ifndef uint16_t
#define uint16_t unsigned short
#endif

#ifndef int32_t
#define int32_t int
#endif

#ifndef uint32_t
#define uint32_t unsigned int
#endif

#ifndef float32_t
#define float32_t float
#endif

#ifndef float64_t
#define float64_t double
#endif

#endif
======================================================


#ifndef _MQTTC_H_
#define _MQTTC_H_

#include "global.h"

#ifdef WIN32
#ifdef _USRDLL
#define APP_API __declspec(dllexport)
#else
#define APP_API
#endif
#else
#define APP_API
#endif

typedef struct
{
uint16_t mid;
int8_t *topic;
uint8_t *payload;
uint32_t payloadlen;
int32_t qos;
int8_t retain;
}MQTTC_Message;

typedef void (*callbackSubscribe)(void *data, uint16_t id, int32_t qosCnt, const uint8_t *qosPtr);
typedef void (*callbackMessage)(void *data, const MQTTC_Message *msg);

#ifdef __cplusplus
extern "C"
{
#endif

APP_API void MQTTC_SetCallbackSubscribe(void *handle, callbackSubscribe cbFunc);
APP_API void MQTTC_SetCallbackMessage(void *handle, callbackMessage cbFunc);

APP_API int32_t MQTTC_Subscribe(void *handle, uint16_t *cbID, const int8_t *subPattern, int32_t qos);

#ifdef __cplusplus
}
#endif

#endif

========================================================

3个问题

1:MQTTC_SetCallbackSubscribe 和 MQTTC_SetCallbackMessage 如何在c#内写
2:MQTTC_Subscribe如何写
3:结构体难道这么纠结
...全文
130 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abutwang 2011-09-10
  • 打赏
  • 举报
回复
问题终于解决了,多谢各位热心的回帖,最后经过调试,函数调用如下
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackMessage(IntPtr data, ref MQTTC_Message msg);

abutwang 2011-09-05
  • 打赏
  • 举报
回复
等下试试看,谢谢各位,自己再顶下
xonln 2011-09-05
  • 打赏
  • 举报
回复
用好你的C++吧,,你得到了最好的,你却想最差的
sdl2005lyx 2011-09-05
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MQTTC_Message
{
public ushort mid;
public string topic;
public IntPtr payload;
public uint payloadlen;
public int qos;
public char retain;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackSubscribe(IntPtr data, UInt16 id, Int32 qosCnt, [In]IntPtr qosPtr);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackMessage(IntPtr data, [In]MQTTC_Message msg);

楼主,注意红色部分跟你的区别。。。
贪玩的老鼠 2011-09-05
  • 打赏
  • 举报
回复
教你一招:
用VC新建一个ALT程序,在.idl里定义结构体生成DLL后,
再把你的动态库引用到C#里,
在库里找到你要的结构体!!
把它栲出来,删掉对动态库的引用,
重新定义就可以了!!!

这招最管用!!!!

abutwang 2011-09-05
  • 打赏
  • 举报
回复
谢谢帮顶,我觉得把一定是有些地方错了,这种东西做通一次就该懂了吧
feixuyue 2011-09-05
  • 打赏
  • 举报
回复
不会,给你顶
abutwang 2011-09-05
  • 打赏
  • 举报
回复
我在c#的定义代码
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MQTTC_Message
{
public System.Int16 mid;
[MarshalAs(UnmanagedType.LPTStr, SizeConst = 32)]
public System.String topic;
[MarshalAs(UnmanagedType.LPArray]
public System.Byte[] payload;
public System.UInt32 payloadlen;
public System.Int32 qos;
public System.Char retain;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackSubscribe(IntPtr data, UInt16 id, Int32 qosCnt, StringBuilder qosPtr);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackMessage(IntPtr data, MQTTC_Message msg);

[DllImport("MQTTC.dll",CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern Int32 MQTTC_Subscribe(IntPtr handle, System.UInt16 cbID,System.String subPattern, System.Int32 qos);
sdl2005lyx 2011-09-05
  • 打赏
  • 举报
回复
去掉, Pack = 1就可以,
char* 对应C#就是string,当然任何类型的指针都可以用Intptr ,尤其是void*。
但是这里仅仅是char*,就没必要那么复杂。。。
abutwang 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sdl2005lyx 的回复:]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MQTTC_Message
{
public ushort mid;
public string topic;
public IntPtr payload;
public uint payloadlen;
public int qos;
public char……
[/Quote]


我发觉要把[StructLayout(LayoutKind.Sequential, Pack = 1)]去掉回调函数就回调成功了

但是里面的内容我用Marshal。copy又完全不对

或者保留[StructLayout(LayoutKind.Sequential, Pack = 1)]

要把public string topic改为 public Intptr topic;


110,566

社区成员

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

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

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