C中char点的等效c#类型是什么

What is the equivalent c# type for char point in C

用于以下 C com 函数的等效 C# 参数类型是什么?

对于下面的签名,我收到了错误:

This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

C#

[DllImport("Sn62.dll")]
public static extern int GetLicenseNo(string lpszLicenseKey, 
                                      string lpszEncryptionKey, 
                                      string lpBuffer, 
                                      ushort wBufferSize);

C代码

#define _SAL2_Source_(Name, args, annotes) _SA_annotes3(SAL_name, #Name, "", "2") _Group_(annotes _SAL_nop_impl_)

#define _Null_terminated_                 _SAL2_Source_(_Null_terminated_, (), _Null_terminated_impl_)

typedef int                 BOOL;
typedef _Null_terminated_ CHAR *LPSTR;
typedef unsigned short      WORD;



__declspec(dllexport)   BOOL  SN_GetLicenseNo(LPSTR lpszLicenseKey,LPSTR lpszEncryptionKey,LPSTR lpBuffer,WORD wBufferSize);


BOOL  GetLicenseNo(LPSTR lpszLicenseKey, LPSTR lpszEncryptionKey, LPSTR lpszBuffer, WORD wBufferSize)
{
    struct t_license *lp;
    BOOL bRet;

    if (wBufferSize<SIZE_LICENSENO + 1)
        return(FALSE);


    ClearText(lpszLicenseKey, lpszEncryptionKey);
    lp = (struct t_license *)DecodeBuffer;
    bRet = TRUE;

    CopyString(lpszBuffer, lp->LicenseNo, SIZE_LICENSENO); //lpszBuffer is used to return value.

    return(bRet);
}

我认为你很接近,你只需要告诉编组系统将什么样的字符串编组为: [DllImport("Sn62.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int GetLicenseNo([MarshalAs(UnmanagedType.LPStr)]string lpszLicenseKey, [MarshalAs(UnmanagedType.LPStr)]string lpszEncryptionKey, [MarshalAs(UnmanagedType.LPStr)]string lpBuffer, ushort wBufferSize);

这样它就知道如何转换数据。

编辑:您可能还需要指定调用约定以匹配您正在使用的导出类型,请参阅 DllImport 属性中的 CallingConvention

根据 Hans Passant 的说法,这可能更正确:

[DllImport("Sn62.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetLicenseNo(StringBuilder lpszLicenseKey,
                                      StringBuilder lpszEncryptionKey,
                                      StringBuilder lpBuffer,
                                      ushort wBufferSize);

您将使用的电话:

ushort wBufferSize = 250;   //Set to the size you need
StringBuilder licKey = new StringBuilder(wBufferSize);
StringBuilder encKey = new StringBuilder(wBufferSize);
StringBuilder buffer = new StringBuilder(wBufferSize);

//Intialize values here...

GetLicenseNo(licKey, encKey, buffer, wBufferSize);