2013-10-05 92 views
-1

夥計們,我有一個不平衡的堆棧問題見下文的Lotus Notes C API的問題

DNParse功能的C API中:

STATUS LNPUBLIC DNParse(DWORD Flags, const char far *TemplateName, 
         const char far *InName, DN_COMPONENTS far *Comp, 
         WORD CompSize); 

using FORMULAHANDLE = System.UInt32; 
using NullHandle = System.Nullable; 
using Status = System.UInt16; 
using DBHandle = System.IntPtr; 
using DHANDLE = System.IntPtr; 
using NoteID = System.UInt32; 
using ColHandle = System.UInt32; 
using WORD = System.UInt32; 
using DWORD = System.UInt32; 
using NoteHandle = System.IntPtr; 
using FontID = System.UInt32; 

public static unsafe string GetCurrentUserCommonName() 
    { 
     string str = ""; 
     Status sts = 0; 
     DWORD xDWORD = 0; 
     dname.DN_COMPONENTS DNComp = new dname.DN_COMPONENTS();    
     StringBuilder szServer = new StringBuilder(0x400, 0x400); 
     StringBuilder InName = new StringBuilder(0x400, 0x400); 

     Initialize(); 
     if (m_isInitialized) 
     { 
      sts = nnotesDLL.SECKFMGetUserName(szServer);     
      sts = nnotesDLL.DNParse(xDWORD, null, szServer, 
            DNComp, (Int16)Marshal.SizeOf(DNComp)); 
      // return CanonName.ToString(); 
     } 
     return str; 
    } 

和C#版本:

[DllImport("nnotes.dll")] 
public unsafe static extern Status DNParse(DWORD Flags, string TemplateName, 
              StringBuilder szServer, 
              dname.DN_COMPONENTS DNComp, 
              short CompSize); 
DN_COMPONENTS STRUCTURE 
public struct DN_COMPONENTS 
{ 
    .... 
} 
+0

請解釋一下「不平衡堆棧問題」的含義。這段代碼運行時會發生什麼?你期望發生什麼? –

回答

0

的錯誤在於本機代碼需要傳遞結構的地址。但是你的C#代碼是按值傳遞結構的。的的P/Invoke應該是這樣的:

[DllImport("nnotes.dll")] 
public static extern Status DNParse(
    DWORD Flags, 
    string TemplateName, 
    string szServer, 
    ref dname.DN_COMPONENTS DNComp, 
    short CompSize 
); 

的其他一些觀點:

  1. 你不需要unsafe這裏。去掉它。
  2. 這兩個字符串參數都被傳遞給該函數,並且緩衝區不被修改。這暗示了const char*。所以編組爲string