2

I'm trying to link the C++ defined libraries , so that they be used in C#. I have an CString PastUsed that holds a variable in C++, which I'm trying to access in C#.

In C++, I stored the value of the variable PastUsed in the '.odl' file as BSTR PastUsed. I'm using DISP_PROPERTY_EX as a dispatch map.

DISP_PROPERTY_EX(Cpctr, "PastUsed", GetPastUsed, SetPastUsed, VT_BSTR). 

I return the value of the String variable from GetPastUsed. But, I donot think this value is being saved in PastUsed (as defined in .odl). This I'm assuming is happening because when I try to access this variable (PastUsed from .odl) in C#, I get a com exception.

The code I have in c# to retrieve the C++ defined variable is:

private void GetPastUsedDes_Click(Object eventSender, EventArgs eventArgs) { if (CheckInvalidButtonClick()) { return; } PrintLine("Dumping past Used"); AxPBSPRINTLib.AxPBSprint sPrint = mPBSPrintCtrl; String str = mPBSPrintCtrl.PastUsed; String str = Print.PastUsed; } 

The PastUsed (here above one in c#) is the same string which is supposed to be containing the variable held in Pastused of c++ (as described in my first post). As soon as it hits the breakpoint of String str in c#, I get the exception:

Print.PastUsed = 'Print.PastUsed' threw an exception of type 'System.Runtime.InteropServices.COMException' 

1 Answer 1

2

It looks like COM code to me.

You'd use COM interop (by referencing the typelibrary and using the generated interop classes for the COM interfaces).

See

Sign up to request clarification or add additional context in comments.

4 Comments

Along side I also realized that the "PastUsed" defined in .odl along with the "GetPastUsed" and "SetPastUsed" also needs to be automated in MFC. Is there a way to automate on the existing MFC??
@user1515739 Please, I honestly don't know what you mean by 'automated in MFC'. I presume you need to consume the same COM interface from MFC. Yes, CComPtr is the way to do that. I think you need to post a small sample, just the interface in idl/odl and a minimal implementation in C++. We can't see enough to say anything more
within the '.odl' file I have added the : [id(41)] BSTR PastUsed; the dispatch map associated with it is: DISP_PROPERTY_EX(CPBSprintCtrl,"PastUsed", GetPastUsed, SetPastUsed, VT_BSTR). The definitions for get, set are: void CPBSprintCtrl::SetPastUsed(BSTR nNewValue) { RefreshControl(); } BSTR CPBSprintCtrl::GetPastUsed() { return m_pastUsed.AllocSysString(); } What needs to be done for this "PastUsed" to be used across the platform?? (in C# to be used in COM library)?
@user1515739 erm... see earlier comment? Also, this doesn't make a minimal example, see SSCCE

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.