C# 通过引用 C++ 传递 int 和 string ActiveX 控件:类型不匹配

发布于 2024-08-12 12:43:29 字数 1690 浏览 7 评论 0原文

我在将引用 int 或字符串变量传递给 C++ ActiveX 控件时遇到问题。 我还通过引用 C++ DLL 传递这些变量,一切正常。

C++ DLL:

__declspec (dllexport) void
Execute (LPCTSTR cmd, int& resultCode, LPCTSTR& message, long& receiptNumber)
{
    message = _T("ReplyProblem");
    resultCode = 100;
    receiptNumber = -1;
}

C#:

[DllImport("MyCOM.dll", CharSet = CharSet.Unicode)]
public static extern void Execute (string cmd, out int resultCode, out string message, out int receiptNumber);
...
int resultCode = 0;
string message = "";
int receiptNumber = 0;
Execute ("cmd", out resultCode, out message, out receiptNumber); // OK

如何在 ActiveX 控件中完成此操作?我尝试使用 & 定义方法引用符号,但 MIDL 编译器不允许这样做。

MyCOM.idl:

[id(1025315)] void Execute (LPCTSTR cmd, [out]long& returnCode); // MIDL2025: syntax error

我修改了使用指针的方法*。

MyCOM.idl:

[id(1025315)] void Execute (LPCTSTR cmd, [out]long* returnCode);

MyCOMCtrl.h:

// Dispatch maps
afx_msg void Execute (LPCTSTR cmd, long* resultCode);

MyCOMCtrl.cpp

// Dispatch map
...
DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_PI4)
...

void MyCOMCtrl::Execute (LPCTSTR cmd, long* resultCode)
{
    *resultCode = 111;
}

C#:

using MyCOMLib;
...
MyCOM client = new MyCOM();
int resultCode = 0;

// COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
client.Execute ("Test command", out resultCode);

使用 C# 中的字符串类型和 C++ ActiveX 中的 LPCTSTR* 会出现相同的异常。 任何提示或建议将不胜感激。

I have a problem passing by reference int or string variables to C++ ActiveX Control.
Also I pass these variables by reference to C++ DLL and everything works fine.

C++ DLL:

__declspec (dllexport) void
Execute (LPCTSTR cmd, int& resultCode, LPCTSTR& message, long& receiptNumber)
{
    message = _T("ReplyProblem");
    resultCode = 100;
    receiptNumber = -1;
}

C#:

[DllImport("MyCOM.dll", CharSet = CharSet.Unicode)]
public static extern void Execute (string cmd, out int resultCode, out string message, out int receiptNumber);
...
int resultCode = 0;
string message = "";
int receiptNumber = 0;
Execute ("cmd", out resultCode, out message, out receiptNumber); // OK

How to get this done in ActiveX Control? I tried to define methods using & reference symbol, but MIDL compiler did not allow that.

MyCOM.idl:

[id(1025315)] void Execute (LPCTSTR cmd, [out]long& returnCode); // MIDL2025: syntax error

I modified the methods to use pointers *.

MyCOM.idl:

[id(1025315)] void Execute (LPCTSTR cmd, [out]long* returnCode);

MyCOMCtrl.h:

// Dispatch maps
afx_msg void Execute (LPCTSTR cmd, long* resultCode);

MyCOMCtrl.cpp

// Dispatch map
...
DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_PI4)
...

void MyCOMCtrl::Execute (LPCTSTR cmd, long* resultCode)
{
    *resultCode = 111;
}

C#:

using MyCOMLib;
...
MyCOM client = new MyCOM();
int resultCode = 0;

// COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
client.Execute ("Test command", out resultCode);

The same exception occurs using string type in C# and LPCTSTR* in C++ ActiveX instead.
Any tips or suggestions will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-08-19 12:43:29

已解决:

在 MyCOMCtrl.cpp 中:

// Dispatch map
...
DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_PI4)
...

必须是:

DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_BSTR VTS_PI4) // two VTS arguments

SOLVED:

In MyCOMCtrl.cpp:

// Dispatch map
...
DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_PI4)
...

Must be:

DISP_FUNCTION_ID(MyCOMCtrl, "Execute", DISPID_EXECUTE_METHOD, Execute, VT_EMPTY, VTS_BSTR VTS_PI4) // two VTS arguments
迎风吟唱 2024-08-19 12:43:29

这只是一个远景(没有双关语),但请尝试对 .net resultCode 变量使用“long”数据类型。

This is just a long shot (no pun intended), but try using the "long" datatype for you .net resultCode variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文