C#通过引用C ++ ActiveX控件传递int和字符串:类型不匹配 [英] C# pass int and string by reference to C++ ActiveX Control: type mismatch
问题描述
我在通过引用int或字符串变量传递给C ++ ActiveX控件时遇到问题。
另外,我通过引用C ++ DLL传递这些变量,一切正常。
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
如何在ActiveX控件中完成此操作?我尝试使用&定义方法参考符号,但MIDL编译器不允许这样做。
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:
MyCOM.idl:
[id(1025315)] void Execute (LPCTSTR cmd, [out]long& returnCode); // MIDL2025: syntax error
我修改了使用指针*的方法。
I modified the methods to use pointers *.
MyCOM.idl:
MyCOM.idl:
[id(1025315)] void Execute (LPCTSTR cmd, [out]long* returnCode);
MyCOMCtrl.h:
MyCOMCtrl.h:
// Dispatch maps
afx_msg void Execute (LPCTSTR cmd, long* resultCode);
MyCOMCtrl.cpp
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 *会发生相同的异常。
任何提示或建议将不胜感激。
The same exception occurs using string type in C# and LPCTSTR* in C++ ActiveX instead. Any tips or suggestions will be appreciated.
推荐答案
已解决:
在MyCOMCtrl.cpp中:
In 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
这篇关于C#通过引用C ++ ActiveX控件传递int和字符串:类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!