-1

伙计们,我遇到了不平衡的堆栈问题,请参见下文

C API 中的 DNParse 函数:

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
{
    ....
}
4

1 回答 1

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.
于 2013-11-09T09:18:52.090 回答