日期:2014-05-18 浏览次数:20997 次
#include<string.h> #include<stdio.h> typedef struct _TAG_Info { char tagMac[64]; char tagCoordinate[32]; }TAG_INFO; typedef struct _RSSI_Info { short nRSSI; char strCoordinate[32]; }RSSI_INFO; TAG_INFO result; extern"C" _declspec(dllexport) TAG_INFO _stdcall GetCoordinateByTagsRssi(char tagMac[64],short inAPCount,RSSI_INFO*pRssiInfo) { sprintf(result.tagCoordinate,"x:%f,y:%f",10.0,10.0); strcpy(result.tagMac,tagMac); return result; }//C++的动态链接库代码 省略了算法 如果调用成功 就能显示出x:10.0000,y:10.0000 在VC6.0下调用正常
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Cstest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [StructLayout(LayoutKind.Sequential)] public struct TAG_INFO { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string tagMac; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string tagCoordinate; }; public struct RSSI_INFO { public Int16 nRSSI; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string strCoordinate; };//对应上面两个结构体 但是char数组和string对应总觉得不太对,又不知道该用什么 [DllImport(@"E:\dll\Debug\dlltest.dll", EntryPoint = "_GetCoordinateByTagsRssi@12")] public static extern TAG_INFO GetCoordinateByTagsRssi(string tagMac, Int16 inAPCount, ref RSSI_INFO pRssiInfo); private void button1_Click(object sender, EventArgs e) { TAG_INFO jieguo; RSSI_INFO[] test = new RSSI_INFO[3]; test[0].nRSSI = 5; test[1].nRSSI = 5; test[2].nRSSI = 5; test[0].strCoordinate = "x:0,y:5"; test[1].strCoordinate = "x:5,y:0"; test[2].strCoordinate = "x:10,y:5"; string tagmac ="AAAA"; Int16 m = 3; jieguo = GetCoordinateByTagsRssi(tagmac, m, ref test[0]); } } }
class Program { public const int tagMac_BytesCount = 64; public const int tagCoordinate_BytesCount = 32; public const int tagAll_BytesCount = tagMac_BytesCount + tagCoordinate_BytesCount; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct TAG_INFO { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = tagMac_BytesCount)] public String tagMac; [MarshalAs(Un