开发者

C# pass int and string by reference to C++ ActiveX Control: type mismatch

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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜