您的位置 首页 > 数码极客

vc++调用bcb的dll如何调试bcb下的dll

在项目开发时,要调用C++封装的dll,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了。但是当传递的是结构体、结构体数组或者结构体指针的时候,就会发现C#上没有类型可以对应。这时怎么办,第一反应是C#也定义结构体,然后当成参数传弟。然而,当我们定义完一个结构体后想传递参数进去时,会抛异常,或者是传入了结构体,但是返回值却不是我们想要的,经过调试跟踪后发现,那些值压根没有改变过,代码如下。

[csharp] view plain copy

  1. [DllImport("workS;)]
  2. private static extern bool fetchInfos(Info[] infos);
  3. public struct Info
  4. {
  5. public int OrderNO;
  6. public byte[] UniqueCode;
  7. public float CpuPercent;
  8. };
  9. private void buttonTest_Click(object sender, EventArgs e)
  10. {
  11. try
  12. {
  13. Info[] infos=new Info[128];
  14. if (fetchInfos(infos))
  15. {
  16. me("Fail");
  17. }
  18. else
  19. {
  20. string message = "";
  21. foreach (Info info in infos)
  22. {
  23. message += ("OrderNO={0}\r\nUniqueCode={1}\r\nCpu={2}",
  24. in,
  25. Encoding.UTF8.GetString),
  26. in
  27. );
  28. }
  29. Me(message);
  30. }
  31. }
  32. catch ex)
  33. {
  34. Me);
  35. }
  36. }

后来,经过查找资料,有文提到对于C#是属于托管内存,现在要传递结构体数组,是属性非托管内存,必须要用Marsh指定空间,然后再传递。于是将结构体变更如下。[csharp] view plain copy

  1. [StructLayoutAttribute, CharSet = C, Pack = 1)]
  2. public struct Info
  3. {
  4. public int OrderNO;
  5. [MarshalA, SizeConst = 32)]
  6. public byte[] UniqueCode;
  7. public float CpuPercent;
  8. };

但是经过这样的改进后,运行结果依然不理想,值要么出错,要么没有被改变。这究竟是什么原因?不断的搜资料,终于看到了一篇,里面提到结构体的传递,有的可以如上面所做,但有的却不行,特别是当参数在C++中是结构体指针或者结构体数组指针时,在C#调用的地方也要用指针来对应,后面改进出如下代码。[csharp] view plain copy

  1. [DllImport("workS;)]
  2. private static extern bool fetchInfos(IntPtr infosIntPtr);
  3. [StructLayoutAttribute, CharSet = C, Pack = 1)]
  4. public struct Info
  5. {
  6. public int OrderNO;
  7. [MarshalA, SizeConst = 32)]
  8. public byte[] UniqueCode;
  9. public float CpuPercent;
  10. };
  11. private void buttonTest_Click(object sender, EventArgs e)
  12. {
  13. try
  14. {
  15. int workStationCount = 128;
  16. int size = Mar(typeof(Info));
  17. IntPtr infosIntptr = Mar(size * workStationCount);
  18. Info[] infos = new Info[workStationCount];
  19. if (fetchInfos(infosIntptr))
  20. {
  21. Me("Fail");
  22. return;
  23. }
  24. for (int inkIndex = 0; inkIndex < workStationCount; inkIndex++)
  25. {
  26. IntPtr ptr = (IntPtr)((UInt32)infosIntptr + inkIndex * size);
  27. infos[inkIndex] = (Info(ptr, typeof(Info));
  28. }
  29. Mar(infosIntptr);
  30. string message = "";
  31. foreach (Info info in infos)
  32. {
  33. message += ("OrderNO={0}\r\nUniqueCode={1}\r\nCpu={2}",
  34. in,
  35. Encoding.UTF8.GetString),
  36. in
  37. );
  38. }
  39. Me(message);
  40. }
  41. catch ex)
  42. {
  43. Me);
  44. }
  45. }

要注意的是,这时接口已经改成IntPtr了。通过以上方式,终于把结构体数组给传进去了。不过,这里要注意一点,不同的编译器对结构体的大小会不一定,比如上面的结构体在BCB中如果没有字节对齐的话,有时会比一般的结构体大小多出2两个字节。因为BCB默认的是2字节排序,而VC是默认1 个字节排序。要解决该问题,要么在BCB的结构体中增加字节对齐,要么在C#中多开两个字节(如果有多的话)。字节对齐代码如下。

[csharp] view plain copy

  1. #pragma pack(push,1)
  2. struct Info
  3. {
  4. int OrderNO;
  5. char UniqueCode[32];
  6. float CpuPercent;
  7. };
  8. #pragma pack(pop)

用Mar为结构体指针开辟内存空间,目的就是转变化非托管内存,那么如果不用Mar,还有没有其他的方式呢?

其实,不论C++中的是指针还是数组,最终在内存中还是一个一个字节存储的,也就是说,最终是以一维的字节数组形式展现的,所以我们如果开一个等大小的一维数组,那是否就可以了呢?答案是可以的,下面给出了实现。

[csharp] view plain copy

  1. [DllImport("workS;)]
  2. private static extern bool fetchInfos(IntPtr infosIntPtr);
  3. [DllImport("workS;)]
  4. private static extern bool fetchInfos(byte[] infos);
  5. [StructLayoutAttribute, CharSet = C, Pack = 1)]
  6. public struct Info
  7. {
  8. public int OrderNO;
  9. [MarshalA, SizeConst = 32)]
  10. public byte[] UniqueCode;
  11. public float CpuPercent;
  12. };
  13. private void buttonTest_Click(object sender, EventArgs e)
  14. {
  15. try
  16. {
  17. int count = 128;
  18. int size = Mar(typeof(Info));
  19. byte[] inkInfosbytes = new byte[count * size];
  20. if (fetchInfos(inkInfosBytes))
  21. {
  22. Me("Fail");
  23. return;
  24. }
  25. Info[] infos = new Info[count];
  26. for (int inkIndex = 0; inkIndex < count; inkIndex++)
  27. {
  28. byte[] inkInfoBytes = new byte[size];
  29. Array.Copy(inkInfosBytes, inkIndex * size, inkInfoBytes, 0, size);
  30. infos[inkIndex] = (Info)bytesToStruct(inkInfoBytes, typeof(Info));
  31. }
  32. string message = "";
  33. foreach (Info info in infos)
  34. {
  35. message += ("OrderNO={0}\r\nUniqueCode={1}\r\nCpu={2}",
  36. in,
  37. Encoding.UTF8.GetString),
  38. in
  39. );
  40. }
  41. Me(message);
  42. }
  43. catch ex)
  44. {
  45. Me);
  46. }
  47. }
  48. #region bytesToStruct
  49. /// <summary>
  50. /// Byte array to struct or classs.
  51. /// </summary>
  52. /// <param name=”bytes”>Byte array</param>
  53. /// <param name=”type”>Struct type or class type.
  54. /// Egg:class Human{...};
  55. /// Human human=new Human();
  56. /// Type type=();</param>
  57. /// <returns>Destination struct or class.</returns>
  58. public static object bytesToStruct(byte[] bytes, Type type)
  59. {
  60. int size = Mar(type);//Get size of the struct or class.
  61. if < size)
  62. {
  63. return null;
  64. }
  65. IntPtr structPtr = Mar(size);//Allocate memory space of the struct or class.
  66. Mar(bytes, 0, structPtr, size);//Copy byte array to the memory space.
  67. object obj = Mar(structPtr, type);//Convert memory space to destination struct or class.
  68. Mar(structPtr);//Release memory space.
  69. return obj;
  70. }
  71. #endregion

对于实在想不到要怎么传数据的时候,可以考虑byte数组来传(即便是整型也可以,只要是开辟4字节(在32位下)),只要开的长度对应的上,在拿到数据后,要按类型规则转换成所要的数据,一般都能达到目的。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“vc++调用bcb的dll如何调试bcb下的dll”边界阅读