用于以下C com函数的等效C#参数类型是什么?
对于下面的签名,我收到错误:
这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
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);
}
答案 0 :(得分:1)
我认为你很接近,你只需要告诉编组系统将哪种字符串编组为: <击> [DllImport(&#34; Sn62.dll&#34;,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);
这样它就知道如何转换数据。
击>
编辑:您可能还需要指定调用约定以匹配您正在使用的导出类型,请参阅CallingConvention
属性中的DllImport
。
根据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);