2011-01-26 114 views
2

我試圖在windows ce 6.0環境中.net cf 3.5中的非託管C++ dll中調用一個函數。P /調用函數通過mangled名稱

的結構被定義爲:

typedef struct TagOperatorInfo 
{ 
    DWORD dwMode;  
    DWORD dwFormat;  //Operator name format 
    DWORD dwAct;  //Network type(Available in 3G module£ºGSM or 3G), 
    TCHAR szOper[32]; 
}OperatorInfo,*LPOperatorInfo; 

和函數調用是:

BOOL GetCurOperatorInfo(LPOperatorInfo pinfo); 

我在.NET的TagOperatorInfo定義如下:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto)] 
    public struct TagOperatorInfo 
    { 

     /// DWORD->unsigned int 
     public uint dwMode; 

     /// DWORD->unsigned int 
     public uint dwFormat; 

     /// DWORD->unsigned int 
     public uint dwAct; 

     /// TCHAR[32] 
     [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 32)] 
     public string szOper; 
    } 

,看到後一些文章和MSDN文檔我稱爲本地功能爲:

[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "#30", CallingConvention = CallingConvention.Winapi)] 
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] 
    internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)] ref NativeHelper.TagOperatorInfo operatorInfo); 

注:我調用的函數的入口點使用序號定義,因爲C++的mangled名稱。

我的問題是我總是得到notSupportedException拋出。我不明白,因爲ref參數應該給它指向結構的指針。

調用它是我的.NET功能:

/// <summary> 
    /// Gets the current operator information. 
    /// </summary> 
    /// <param name="operatorInfo">The operator info.</param> 
    /// <returns></returns> 
    public static bool GetOperatorInformation(out NativeHelper.TagOperatorInfo operatorInfo) 
    { 
     operatorInfo = new NativeHelper.TagOperatorInfo(); 

     if (NativeImports.GetCurOperatorInfo(ref operatorInfo) == true) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 

    } 

什麼我缺少這個工作。

UPDATE

.NET Compact Framework受方法調用

[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "[email protected]@@[email protected]@@Z", CallingConvention = CallingConvention.Winapi)] 
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] 
    internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)]ref NativeHelper.TagOperatorInfo operatorInfo); 
+0

你確定你需要序號?試試它的名字。幾乎所有的C++ API都將使用`extern「C」`作爲未加密的名稱。 – 2011-01-26 12:35:45

+0

查看評論下載 – Sorcerer86pt 2011-01-26 12:42:13

回答

7

只能調用已導出爲C風格的函數功能。據我所知,你無法通過P/Invoke調用直C++功能,例如:

How to set up a C++ function so that it can be used by p/invoke?

更新

事實上,它似乎調用通過功能時,你可以使用錯位的名稱的P/Invoke。這是我過去永遠無法工作的東西,所以我立即糾正。使用名稱,而不是序應該更有彈性太:

參考:Entry Point Not Found Exception

因此,像:

[DllImport(gsmaAdapterDLLName, 
    EntryPoint="[email protected]@@[email protected]@@Z", 
    ExactSpelling = true, 
    CallingConvention = CallingConvention.Cdecl)] 
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info); 

而對於.NET CF:

DllImport(gsmaAdapterDLLName, 
    EntryPoint="[email protected]@@[email protected]@@Z", 
    CallingConvention = CallingConvention.WinApi)] 
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info);