Passing by reference an ADO Connection from Excel VBA to C++

I want to pass by reference an ADO Connection which has been already opened in Excel VBA to a C++ DLL, but I get the following error:

"Unhandled exception at 0x4dd5230f in EXCEL.EXE: 0xC0000005: Access violation writing location 0x1775238d."

What am I doing wrong

The code I am using is:

- VBA:

Declare Function Retrieve_C Lib "xxx.dll" (ByRef conn As ADODB.Connection) As Double
Function Test() As Double
Dim c As ADODB.Connection
Set c = New ADODB.Connection
c.Open "Provider=MSDASQL; Data Source=xxx"
Test = Retrieve_C(c)
End Function

- C++:

#import "xxx\msado15.dll" rename("EOF","ADOEOF")
double __stdcall Retrieve_C(ADODB::_ConnectionPtr conn)
{
CoInitialize(NULL);
ADODB::_RecordsetPtr recordset(__uuidof(ADODB::Recordset));
recordset->Open("SELECT xxx",
conn.GetInterfacePtr(),
ADODB::adOpenForwardOnly,
ADODB::adLockReadOnly,
ADODB::adCmdText);
return recordset->Fields->GetItem("xxx")->GetValue();
recordset->Close();
}


Answer this question

Passing by reference an ADO Connection from Excel VBA to C++

  • Philip Carmichael

    I have moved this thread to the native data access forum. You are more likely to get a response there, since this is native ADO and not ADO.NET.

    http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=87&SiteID=1

    Thanks,

    Sarah



  • prithvi4u

    Try using "xxx.dll" (ByVal conn As ADODB.Connection) instead.

    (Passing conn ByVal instead of ByRef)

    Also as a side note, you've placed call to recordset close method after the return statement...

  • Passing by reference an ADO Connection from Excel VBA to C++