我有一个C ++ DLL,我试图在我的C#项目中工作(我对C ++ / C一无所知,但确实有源,可以在VS2013中打开并构建它)
dll附带了Excel VBA中的工作示例,但我无法在C#中使用它。
我不断遇到的错误是......
附加信息:调用PInvoke函数 'RMTest!RMTest.PInvokeTest :: encode_eib_d'使堆栈失衡。 这可能是因为托管的PInvoke签名不匹配
我的C#测试代码如下......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace RMTest
{
class Program
{
static void Main(string[] args)
{
string s1 = "2";
string s2 = "1";
string s3 = "1";
string s4 = "1";
string s5 = "TESTTE";
var res = PInvokeTest.encode_eib_d(s1, s2, s3, s4, s5);
}
}
class PInvokeTest
{
[DllImport("UKRM_EncodeEIB_D.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern string encode_eib_d(string infoID, string versionID, string format, string sClass, string licNum);
}
}
作为参考,Excel中的工作示例,VBA如下......
Public Declare _
Function encode_eib_d Lib "UKRM_EncodeEIB_D.dll" (ByVal inInfoID As String, ByVal inVersionID As String, ByVal inClass As String, _
ByVal inFormat As String, ByVal inLicenseNum As String) As Variant
Function ENCODE_D(inInfoID As String, inVersionID As String, inFormat As String, inClass As String, inLicenseNum As String) As Variant
Dim error As Integer
ChDrive (ActivePath())
ChDir (ActivePath())
On Error GoTo ErrHandler:
ENCODE_D = encode_eib_d(inInfoID, inVersionID, inFormat, inClass, inLicenseNum)
ErrHandler:
If Err.Number <> 0 Then
ENCODE_D = Err.Description
End If
End Function
C ++ dll中相关的代码片段(据我所知)如下......
标题(.h):
#include "UKRM_EncodeEIB_D.h"
#include <WTypes.h>
#include <tchar.h>
#include <comutil.h>
#include <malloc.h>
#include <atlconv.h>
extern "C" {
VARIANT __stdcall CallEncode(LPCSTR sInfoID, LPCSTR sVersionId, LPCSTR sFormat, LPCSTR sClass, LPCSTR sLicenseNum);
}
的.cpp
#include "UKRM_EncodeEIB_D_DLL.h"
// ******************************************************************************
// * CallEncode
// ******************************************************************************
///
/// @brief Use of UKRM_Encode_EIB_D to provide DLL interface
///
/// @param [in] sInfoID barcode info ID
/// @param [in] sVersionId barcode version id
/// @param [in] sFormat barcode format
/// @param [in] sClass barcode class ID
/// @param [in] sLicenseNum barcode license number
///
/// @return @arg returns barcode if successful, otherwise returns error
///
/// @LM_Detailed_Description
/// Creates UKRM_Encode_EIB_D object, supplies input and returns result
//
// ******************************************************************************
VARIANT __stdcall CallEncode(LPCSTR sInfoID, LPCSTR sVersionId, LPCSTR sFormat, LPCSTR sClass, LPCSTR sLicenseNum)
{
bool bRC;
class UKRM_Encode_EIB_D stEncodeObject;
char *srtnstring = NULL;
bRC = stEncodeObject.encode((char *) sInfoID, (char *) sVersionId, (char *) sFormat, (char *) sClass, (char *) sLicenseNum);
if(bRC == true)
{
stEncodeObject.get_BarcodeString(&srtnstring);
}
else
{
stEncodeObject.get_ErrorString(&srtnstring);
}
_bstr_t bstrt(srtnstring);
return _variant_t(bstrt).Detach();
}
.DEF:
LIBRARY "UKRM_EncodeEIB_D"
EXPORTS
encode_eib_d = CallEncode
VERSION 1.0
我在输入参数上尝试了MarshalAs等的各种组合,并尝试使用StringBuilders而不是字符串,但听起来好像对方法签名不满意。
令人沮丧的是它在VBA中有效。
如果您需要更多信息,请询问。
感谢。
答案 0 :(得分:0)
在您的情况下,您需要一个pinvoke签名,如:
[DllImport("UKRM_EncodeEIB_D.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string encode_eib_d(
[MarshalAs(UnmanagedType.LPStr)]string infoID,
[MarshalAs(UnmanagedType.LPStr)]string versionID,
[MarshalAs(UnmanagedType.LPStr)]string format,
[MarshalAs(UnmanagedType.LPStr)]string sClass,
[MarshalAs(UnmanagedType.LPStr)]string licNum);