Service Application Exception

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
wjm4
Posts: 72
Joined: Mon 12 Feb 2007 21:31

Service Application Exception

Post by wjm4 » Sun 25 Apr 2010 02:14

I have a simple service application that will connect to a database server on start and then launch a thread that sleeps every second. I am setting the TUniConnection Parameters at run time and calling the connect statement. I have narrowed the problem down to the connect statement.

1)With the connect() or connected = true statement commented out, the application runs fine.
2) With the connection enabled in the designer and then I compile (Thus an app that starts with the connection enabled) everything works fine

The only time it fails is when I call connect from code. Any ideas as to why this is happening? Thanks for the help.

I have a sample application but do not see anywhere to attach it.

Code: Select all

//---------------------------------------------------------------------------
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBAccess"
#pragma link "Uni"
#pragma link "dxmdaset"
#pragma resource "*.dfm"

TService1 *Service1;
//---------------------------------------------------------------------------
__fastcall TService1::TService1(TComponent* Owner)
	: TService(Owner)
{
}

TServiceController __fastcall TService1::GetServiceController(void)
{
	return (TServiceController) ServiceController;
}

void __stdcall ServiceController(unsigned CtrlCode)
{
	Service1->Controller(CtrlCode);
}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceStart(TService *Sender, bool &Started)
{
  /********Database Connection*********/
  ses->Username = "wcs";
  ses->Password = "password";
  ses->Server = "10.20.1.7\SQLEXPRESS";
  ses->LoginPrompt = false;
  ses->ConnectDialog = NULL;
  ses->Connected = true; //Causes Service to Crash
  //ses->Connect();  //Causes Service to Crash
  /************************************/
  myThread = new TestThread(false);
  Started = true;
}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceStop(TService *Sender, bool &Stopped)
{
  if(myThread)  {
	myThread->Terminate();
	myThread->WaitFor();
	delete myThread;
	myThread = NULL;
  }
  Stopped = true;
}
//---------------------------------------------------------------------------

Code: Select all

//---------------------------------------------------------------------------

#include 
#pragma hdrstop

#include "TestThread.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(&UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TestThread::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

__fastcall TestThread::TestThread(bool CreateSuspended)
	: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TestThread::Execute()
{
  while(!Terminated) {
    ::Sleep(1000);
  }
}
//---------------------------------------------------------------------------

wjm4
Posts: 72
Joined: Mon 12 Feb 2007 21:31

Post by wjm4 » Sun 25 Apr 2010 02:36

I forgot to mention that I have the provider already declared on the design form. I have it set to SQL Server.

I know the connection parameters are correct because I can connect from within the designer with the same connection parameters.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 26 Apr 2010 10:02

To solve the problem try to execute the CoInitialize function in the method, where connection to SQL Server is established, like this:

Code: Select all

  CoInitialize(nil);

Post Reply