问题
I have compiled the Sample OleDb Provider code which comes from running the VS2007 ATL OLEDB Provider wizard. I have given fuller details on that blog post. The code crashes Excel.
Sub TestOleDbProvider()
On Error GoTo ErrHand
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "Provider=FindFiles;Server=foo;Database=bar" '* this works
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cn '* this works
cmd.CommandText = "*.*" '* this works
Stop
Dim rs As ADODB.Recordset
Set rs = cmd.Execute '* crashes here
Exit Sub
ErrHand:
Debug.Print Err.Description & " (" & Err.Number & ")"
'Stop
End Sub
The above code crashes Excel on the line Set rs = cmd.Execute
. If I debug from VS2017 I get unhandled exception
C++ call stack for uncaught exception is here
msado15.dll!CQuery::SetSQL(unsigned short *) Unknown Non-user code. Symbols loaded.
msado15.dll!CQuery::SetCommandText(long,unsigned long,unsigned char,unsigned char) Unknown Non-user code. Symbols loaded.
msado15.dll!CQuery::Execute(enum ExecuteTypeEnum,char,unsigned long,bool,unsigned long,unsigned long,long,struct tagVARIANT *,unsigned long,void *,long *,struct _ADORecordset * *) Unknown Non-user code. Symbols loaded.
msado15.dll!CCommand::_Execute(enum ExecuteTypeEnum,char,unsigned long,bool,unsigned long,unsigned long,long,long,struct tagVARIANT *,unsigned long,void *,long *,struct _ADORecordset * *) Unknown Non-user code. Symbols loaded.
msado15.dll!CCommand::ExecuteWithModeFlag(struct tagVARIANT *,struct tagVARIANT *,long,struct _ADORecordset * *,int) Unknown Non-user code. Symbols loaded.
msado15.dll!CCommand::Execute(struct tagVARIANT *,struct tagVARIANT *,long,struct _ADORecordset * *) Unknown Non-user code. Symbols loaded.
VBE7.DLL!1e813579() Unknown No symbols loaded.
[Frames below may be incorrect and/or missing, no symbols loaded for VBE7.DLL] Annotated Frame
VBE7.DLL!1e7cff4b() Unknown No symbols loaded.
VBE7.DLL!1e829d13() Unknown No symbols loaded.
VBE7.DLL!1e82fea2() Unknown No symbols loaded.
VBE7.DLL!1e82bcb5() Unknown No symbols loaded.
[External Code] Annotated Frame
The call stack shows the exception being raised clearly inside of msado15.dll
I should imagine this use case is not regularly tested but I'd be interested in any suggestions.
How to stop crashing please?
回答1:
ADO, the COM automation wrapper over OLEDB, queries for the OLEDB's ICommandText interface. Instead of complaining the interface is not implemented, it just continues with a NULL command text and crashes.
I supposed all this used to work, but doesn't anymore as for some reason (OLEDB coupled with ADO makes a quite complex technology), the ATL template that supports the command implements the interface but does not answer when queried for.
So to fix the issue, just add an ATL entry to the serviced interfaces for the command object:
BEGIN_COM_MAP(CFindFilesCommand)
...
COM_INTERFACE_ENTRY(ICommandText)
...
END_COM_MAP()
来源:https://stackoverflow.com/questions/51217627/oledb-c-atls-oledb-provider-sample-crashes-excel-uncaught-exception-from