2011-04-14 5 views
0

C++ tête de la fonction DLL cette fonction deux pour obtenir des informations sur les stations wifi autour de moi en utilisant gagner appareil mobile 6.5 et je dois les appeler pour les utiliser dans le code C#Pinvoke C# de C++ dll en utilisant DllImport en C#

// (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); 

C# échantillon

[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 
     } 
    } 
} 

et quand je mets ce dll sur le mobile et le C# app.exe la première fonction nommée Getadapters (..) retour à moi le nom de l'adaptateur dans le premier textbox1 puis l'application s'est arrêtée et ne me donne pas d'exception prise en charge lorsque le mobile tente d'exécuter Les deux autres fonctions nommées refreshBSSID() et getBSSIDs(), quel est le problème? ou existe-t-il une autre solution pour obtenir cette information (BSSID, SS ..etc)?

Répondre

0

C++ par défaut, sauf s'il est modifié, utilise une convention d'appel de l'appelant (Cdecl). Votre code C++ ne modifie pas la convention d'appel. Votre code C# par défaut (à moins que vous ne le changiez) utilisera une convention d'appel (StdCall).

Bien que cela ne soit pas exactement le problème que vous l'ayez toujours est techniquement incorrect. Même si vous deviez résoudre votre problème actuel, vous risquez d'avoir un problème à cause de la convention d'appel.

Je vais deviner que votre structure C# BSSIDInfo ne correspond pas à la structure C++. Pourquoi faire la méthode StrToByteArray quand tout ce qu'il fait est GetBytes sur la chaîne donnée ...

lorsque les essais mobiles pour exécuter l' deux autres fonctions qui a nommé comme refreshBSSID() et getBSSIDs() donc ce que est le problème ? ou est-il une autre solution pour obtenir cette information

Je pensais que je savais que la raison a pris un autre regard et je me suis trompé.

+0

et je vais dire quelque chose ... principalement la première fonction qui utilise la fonction StrToByte bien exécuté .. le problème maintenant dans la seconde de fonctionner –