Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL & ADO & C++: unhandled exception

Status
Not open for further replies.

ramass

Programmer
Jul 10, 2001
19
FR
Hello !

I want to use ADO in a shared dynamic library. So, I create some methods to open and close a connection. Here is the code of these methods:
Code:
_RecordsetPtr	RS_seat;		
_ConnectionPtr     CN_seat;      	
_CommandPtr      CM_seat;			
CCustomRs	champBD;			
IADORecordBinding *piAdoRecordBinding ;

MYDCLSPEC short _stdcall connecterBD()
{
	HRESULT hr = NOERROR;
	piAdoRecordBinding = NULL;
    
	CString			strConnection;		
              CString			strCmdText;				
	
	strConnection   = _T("Provider=MSDAORA.1;Password=seat;User ID=seat;Data
                                              Source=seatecole;Persist Security Info=True");
	strCmdText  = _T("select *  from tbl_cible");

	RS_seat = NULL;
	 ::CoInitialize(NULL);
	try
	{ 
                 CN_seat.CreateInstance(__uuidof( Connection ) );
                 CN_seat->Open( (LPCSTR)strConnection,"","",adConnectUnspecified);
    	}
	catch (_com_error )
	{  return 1;
	}

	return 0;
}

MYDCLSPEC short _stdcall deconnecterBD()
{
	try
	{
                 CN_seat->Close();
	}
	catch ( _com_error )
	{
	   return 1;
	}
	return 0;
}
and here is the program which uses these methods :
Code:
#include <malloc.h>
#include &quot;d:\Recup\mumuzdll\ProgrammesInitiationC\test\test.h&quot;
#include &quot;d:\Recup\manipOracleC\dllBD\dllBD.h&quot;

int main(int argc, char* argv[])
{             // Connection
	if ( (resultat = connecterBD(&CN_seat)) == 1) {
                            printf(&quot;Bweurk!\n&quot;);
                            return 0;
	}
	else 
	{
		// Déconnection
                    if ( (resultat = deconnecterBD(&CN_seat)) == 1 ) {
                         printf(&quot;Bouh!!&quot;);
                         return 0;
	       }
              }
	return 2;
}

The connection and the disconnection are OK but I have a &quot;unHandled exception&quot; after the execution of the program ( ie after the instruction 'return 2'). My platform is a WindowsNT one.
Is is possible to avoid it ?

Thanks for reply

Matthieu, a french programmer


 
I can't understand what are you doing.
1. The CoInitialize is not in the right place. Put it in the first line in main function.
2. If I write your main in pseudocode
main
if(connect here)
{
...
}else
{
disconnect here
}
1. you mustn't close a program before disconnecting what you connected
2. you can't disconnect what you didn't connect. Once run your program do only a connection or only a disconnection. John Fill
1c.bmp


ivfmd@mail.md
 
Sorry, my first message wasn't enough clear. I explain me: There are TWO programs
- The first program is a DLL. For the first point, I agree with you: I should put ::CoInitialize at the right place.
- The second program uses the first one. Its pseudo code is or is equivalent to:
Code:
    connect here and take the result
    if result = OK then
       disconnect here
    end if
So, there is a disconnection if the connection was sucessfull. The problem is the disconnection which isn't made and an error which occured at the end of the program ('unhandler execption'). I think that I can't use ADO component in a DLL but I'd like to export it from a C++ program to a VB program ( even if I know that it is possible to use it directly in VB )
 
I think is because disconnection is failed. Try to see if you closed all queries opened in this connection. John Fill
1c.bmp


ivfmd@mail.md
 
There are no queries opened in this connection. The program just opens and closes it, that's all. Moreover, no exception are detected with try/catch or with the control of the HRESULT code returned by Close ; I use the code:
Code:
   hr = CN_seat->close;
   if ( FAILED(hr) ){
     return 2;
   }
and no error occured.
 
Ok, I guess. The first program use some ptr objects. In a program, if you have open a connection, in its destructor it closes the connection what you had closed in an other program. As a result memory fault. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top