Pinvoke C#来自C++;在C中使用DLLimport的动态链接库#
DLL中的C++函数头这两个函数用于获取有关我周围使用win mobile 6.5设备的wifi站点的信息,我需要调用它们以在C#代码中使用它们 C#样品Pinvoke C#来自C++;在C中使用DLLimport的动态链接库#,c#,c++,wifi,windows-mobile-6.5,C#,C++,Wifi,Windows Mobile 6.5,DLL中的C++函数头这两个函数用于获取有关我周围使用win mobile 6.5设备的wifi站点的信息,我需要调用它们以在C#代码中使用它们 C#样品 [DllImport(@“\Storage Card\Work\Beaad.dll”,EntryPoint=“GetAdapters”,SetLastError=true)] 公共静态外部bool getAdapters([marshallas(UnmanagedType.LPWStr)]字符串buf,ref UInt32 dwBufSize
[DllImport(@“\Storage Card\Work\Beaad.dll”,EntryPoint=“GetAdapters”,SetLastError=true)]
公共静态外部bool getAdapters([marshallas(UnmanagedType.LPWStr)]字符串buf,ref UInt32 dwBufSizeBytes);
[DllImport(@“\Storage Card\Work\bead.dll”,EntryPoint=“RefreshBSSIDs”,SetLastError=true)]
公共静态外部bool refreshBSSIDs([MarshalAs(UnmanagedType.LPWStr)]字符串buf);
[DllImport(@“\Storage Card\Work\bead.dll”,EntryPoint=“GetBBSIDs”,SetLastError=true)]
公共静态外部bool getBBSIDs([marshallas(UnmanagedType.LPWStr)]字符串buf,BSSIDInfo[]nfo,ref UInt32 dwBufSizeBytes,ref UInt32 dwReturnedItems);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
公共结构BSSIDInfo
{
公共字节[]BSSID;//mac
公共字符[]SSID;
公共BSSIDInfo(字节[]bs,字符[]ss)
{
这是RSSI=0;
这个.Infastructure=0;
这个通道=0;
this.Auth=0;
bs=新字节[6];
ss=新字符[32];
BSSID=bs;
SSID=ss;
}
公共国际RSSI;
公共int频道;
公共结构;
公共int认证;
}
公共静态字节[]StrToByteArray(字符串str)
{
System.Text.UTF8Encoding encoding=新的System.Text.UTF8Encoding();
返回编码.GetBytes(str);
}
公共静态字符[]c=新字符[1024];
字符串buf=新字符串(c);
公共无效按钮1\u单击(对象发送者,事件参数e)
{
BSSIDInfo[]nfo=新的BSSIDInfo[128];
字节[]bytee=StrToByteArray(buf);
UInt32 dwsize=新UInt32();
UInt32 dwTmp=新的UInt32();
UInt32 dwCount=新的UInt32();
dwTmp=Convert.ToUInt32(Marshal.SizeOf(typeof(BSSIDInfo))*nfo.Length;
dwCount=0;
dwsize=Convert.ToUInt32(bytee.Length);
if(false==getAdapters(buf,ref-dwsize)| | dwsize==0)
{
标签1.Text=“无适配器”;
}
其他的
{
字符串[]strList=新字符串[15];
if(buf.Contains(',')==false)//一个适配器
{
textBox1.Text=buf;
}
其他的
{
strList=buf.Split(',');
for(int i=0;i0)
{
//刷新BSSID(buf)&&
对于(int i=0;i
当我把这个dll放在手机上,C#app.exe第一个名为Getadapters(..)的函数返回给我第一个文本框中适配器的名称时,手机尝试执行另外两个名为refreshBSSID()和getBSSIDs()的函数时,应用程序停止并给出不受支持的异常那么问题是什么呢?或者是否有其他解决方案来获取此信息(BSSID、SS..等)?C++默认情况下,除非更改使用调用方(Cdecl)调用约定。您的C++代码不会更改调用约定。默认情况下,C#代码(除非更改)将使用被调用方约定(StdCall) 虽然这可能不是问题所在,但您拥有它在技术上仍然是不正确的。即使您要解决当前的问题,您也可能会因为呼叫约定而最终遇到问题 <>我猜你的C bssiDeNoFi结构与C++结构不匹配。为什么方法StrToByteArray所做的只是获取给定字符串上的字节 当手机试图执行 其他两个名为 refreshBSSID()和GetBSSID()那又怎样 问题出在哪里?还是还有别的 获取此信息的解决方案
我以为我知道原因,但又看了一眼,我错了。我要说点什么。。。主要是第一个使用StrToByte函数的函数执行得很好。。现在的问题是第二个问题
// (adapter names , pointer to destination buffer ,and the size , returned structs)
bool __declspec(dllexport) GetBBSIDs(LPWSTR pAdapter, struct BSSIDInfo *pDest, DWORD &dwBufSizeBytes, DWORD &dwReturnedItems);
bool __declspec(dllexport) RefreshBSSIDs(LPWSTR pAdapter);
bool __declspec(dllexport) GetAdapters(LPWSTR pDest, DWORD &dwBufSizeBytes);
[DllImport(@"\Storage Card\Work\Beaad.dll", EntryPoint = "GetAdapters", SetLastError = true)]
public static extern bool getAdapters([MarshalAs(UnmanagedType.LPWStr)] String buf, ref UInt32 dwBufSizeBytes);
[DllImport(@"\Storage Card\Work\Beaad.dll", EntryPoint = "RefreshBSSIDs", SetLastError = true)]
public static extern bool refreshBSSIDs([MarshalAs(UnmanagedType.LPWStr)]String buf);
[DllImport(@"\Storage Card\Work\Beaad.dll", EntryPoint = "GetBBSIDs", SetLastError = true)]
public static extern bool getBBSIDs([MarshalAs(UnmanagedType.LPWStr)]String buf,BSSIDInfo [] nfo, ref UInt32 dwBufSizeBytes, ref UInt32 dwReturnedItems);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct BSSIDInfo
{
public byte[] BSSID; //mac
public char[] SSID;
public BSSIDInfo(byte[]bs,char[] ss)
{
this.RSSI = 0;
this.Infastructure = 0;
this.Channel = 0;
this.Auth = 0;
bs = new byte[6];
ss = new char[32];
BSSID = bs;
SSID = ss;
}
public int RSSI;
public int Channel;
public int Infastructure;
public int Auth;
}
public static byte[] StrToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
public static char[] c = new char[1024];
string buf = new string(c);
public void button1_Click(object sender, EventArgs e)
{
BSSIDInfo[] nfo = new BSSIDInfo[128];
byte[] bytee=StrToByteArray(buf);
UInt32 dwsize= new UInt32();
UInt32 dwTmp = new UInt32();
UInt32 dwCount = new UInt32();
dwTmp = Convert.ToUInt32(Marshal.SizeOf(typeof(BSSIDInfo)) * nfo.Length);
dwCount =0;
dwsize=Convert.ToUInt32(bytee.Length);
if (false == getAdapters(buf,ref dwsize) || dwsize == 0)
{
label1.Text = "no adabters";
}
else
{
String [] strList=new String[15];
if (buf.Contains(',') == false)// one adapter
{
textBox1.Text = buf;
}
else
{
strList = buf.Split(',');
for (int i = 0; i < strList.Length; i++)
{
textBox1.Text+= strList[i]+Environment.NewLine;
}
}
if (refreshBSSIDs(buf) && getBBSIDs(buf, nfo, ref dwTmp, ref dwCount) && dwCount > 0)
{
//refreshBSSIDs(buf) &&
for (int i = 0; i < dwCount; i++)
{
textBox2.Text += nfo.GetValue(i).ToString() + Environment.NewLine;
}
}
else
{
//make another thing
}
}
}