我有一个 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中工作。
如果您需要更多信息,请询问。
谢谢。