C# 通过引用 C++ 传递 int 和 string ActiveX 控件:类型不匹配
我在将引用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已解决:
在 MyCOMCtrl.cpp 中:
必须是:
SOLVED:
In MyCOMCtrl.cpp:
Must be:
这只是一个远景(没有双关语),但请尝试对 .net resultCode 变量使用“long”数据类型。
This is just a long shot (no pun intended), but try using the "long" datatype for you .net resultCode variable.