下面一位网友写的代码(精华区有一张贴子):
int __stdcall Open(long _overtime,char* Params,char *message)
{
int ret=0;
Application->ProcessMessages();
HRESULT coret=CoInitialize(NULL);
switch (coret)
{
case S_OK:
break;
case S_FALSE:
strcpy(message,"Indicates that the library is already initialized or that it could not release default allocator.");
break;
case E_OUTOFMEMORY:
strcpy(message,"Indicates that it was unable to initialize because the system is out of memory.");
break;
case E_INVALIDARG:
strcpy(message,"Indicates the argument is invalid.");
break;
case E_UNEXPECTED:
strcpy(message,"Indicates an unexpected error occurred.");
break;
}
if(coret!=S_OK&&coret!=S_FALSE)
{
return -1;
}
try{
DM=new TDM(NULL);
}
catch(...)
{
strcpy(message,"创建数据模块失败!");
return -1;
}
// Application->CreateForm(__classid(TDM), &DM);
try
{
DM->Connect=true;
}
catch(Exception *E)
{
strcpy(message,"连接数据库失败!");
strcat(message,E->Message.c_str());
CoUninitialize();
return -1;
}
return ret;
}
//---------------------------------------------------------------------------
int __stdcall Close(long _overtime,char* Params,char *message)
{
int ret=0;
Application->ProcessMessages();
if(!DM)
{
strcpy(message,"没有数据模块的实例!");
return -1;
}
try
{
DM->Connect=false;
}
catch(Exception *E)
{
strcpy(message,"断开数据库失败!");
strcat(message,E->Message.c_str());
ret=-8888;
}
try
{
delete DM;
}
catch(...)
{
strcpy(message,"释放数据模块失败");
ret=-9999;
}
CoUninitialize();
return ret;
}