好久没来了,看看我的函数为什么会崩溃
//---------------------------------------
#include <vcl.h>
#include <windows.h>
#include "Data.h"
#include <inifiles.hpp>
#include "DBAccess.hpp"
#include "MemDS.hpp"
#include "MyAccess.hpp"
#pragma hdrstop
//---------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------
#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//---------------------------------------
extern "C"
{
__declspec(dllexport) __stdcall bool insertDataToDB(const ST_DATASTORAGE DataRec);
__declspec(dllexport) __stdcall bool ConnectToDB();
}
bool bConnDB;
TMyQuery *command;
TList *list;
//TFDM *DM;
//连接到数据库
__declspec(dllexport) __stdcall bool ConnectToDB()
{
TIniFile *ini;
ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));
// ini->ReadString()
TMyConnection *connect = new TMyConnection(NULL);
connect->LoginPrompt = false;
AnsiString ans;
ans = ini->ReadString("DBServer","Server","");
connect->Server = ans;
connect->Port = ini->ReadInteger("DBServer","Port",3306);
ans = ini->ReadString("DBServer","User","");
connect->Username = ans;
ans = ini->ReadString("DBServer","Password","");
connect->Password = ans;
ans = ini->ReadString("DBServer","DataBase","");
connect->Database = ans;
delete ini;
connect->Connect();
bConnDB = connect->Connected;
if(bConnDB)
ShowMessage("yes");
else
ShowMessage("No");
if(bConnDB == true)
{
command = new TMyQuery(NULL);
command->Connection = connect;
//command->CommandTimeout = 5;
list = new TList;
//DM = new TFDM(NULL);
//ShowMessage("连接成功 ");
}
else
{
//ShowMessage("连接失败 ");
}
return bConnDB;
}
//获取节点的类型 温度 角度 应变三种 温度25 角度31 应变32
int GetNodeType(int NodeID)
{
for(int i = 0; i < list->Count; i++)
{
ST_NODE_TYPE *data = (ST_NODE_TYPE *)list->Items[i];
if(data->Node == NodeID)
{
return data->Type;
}
}
TIniFile *ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));
AnsiString ans = "A";
ans += IntToStr(NodeID);
//温度
int pos = ini->ReadInteger("Temperaturn",ans,-1);
if(pos != -1)
{
delete ini;
ST_NODE_TYPE *data = new ST_NODE_TYPE;
data->Node = NodeID;
data->Type = 25;
list->Add(data);
return 25;
}
//角度
pos = ini->ReadInteger("Angle",ans,-1);
if(pos != -1)
{
delete ini;
ST_NODE_TYPE *data = new ST_NODE_TYPE;
data->Node = NodeID;
data->Type = 31;
list->Add(data);
return 31;
}
//应变
pos = ini->ReadInteger("Strain",ans,-1);
if(pos != -1)
{
delete ini;
ST_NODE_TYPE *data = new ST_NODE_TYPE;
data->Node = NodeID;
data->Type = 32;
list->Add(data);
return 32;
}
delete ini;
return -1;
}
//转16进制字符串
void FunHexToText(char *text,const float value)
{
char buff[4];
memcpy(buff,&value,4);
BinToHex(buff,text,4);
}
//入库操作
__declspec(dllexport) __stdcall bool insertDataToDB(const ST_DATASTORAGE DataRec)
{ /*
if(bConnDB == false)
ConnectToDB();
if(bConnDB == false)
{//连接数据库失败
return false;
}*/
/*AnsiString SQL = "INSERT INTO `monitorinfo` (MonitorType,Direction,OrderCode,DeviceNumber,DataLength,TimeDot,ObjectCode,DataContent,CreateTime,SourceData) VALUES(";
int itemp = 25;//GetNodeType(DataRec.nNodeID);
//测点类型
SQL += IntToStr(itemp);
SQL += ",1,1,";
//测点编号
AnsiString ans = IntToStr(itemp);
ans += IntToStr(DataRec.nNodeID);
ans += IntToStr(DataRec.nChannelID);
SQL += ans;
SQL += ",";
//数据长度
SQL += "4,'";
//监测时间点
TDateTime datetime(DataRec.dMeasuredTime);
ans = "2013-08-08 08:12:12";//datetime.FormatString("yyyy-MM-dd hh:mm:ss");
SQL += ans;
SQL += "',";
//对象码 跟测点编号一致
ans = IntToStr(itemp);
ans += IntToStr(DataRec.nNodeID);
ans += IntToStr(DataRec.nChannelID);
SQL += ans;
SQL += ",'";
//数据内容 原始数据的16进行字符表示
//char buff[20];
//ZeroMemory(buff,20);
//FunHexToText(buff,DataRec.fMeasuredValue);
//ans.printf("%s",buff);
SQL+= "A1B1C1D1";//ans;
SQL += "','";
//系统时间
SQL += "2013-10-10 12:12:12";//Now().FormatString("yyyy-MM-dd hh:mm:ss");
SQL += "','";
//原始数据
char buff2[5];
ZeroMemory(buff2,5);
memcpy(buff2,&DataRec.fMeasuredValue,4);
//ShowMessage(buff2);
//ans.printf("%s",buff2);
//ans = "asdf";
SQL += buff2;
SQL += "')";
command->SQL->Clear();
command->SQL->Add(SQL);
//ShowMessage(SQL);
try
{
//command->ExecSQL();
}catch(...)
{
return false;
} */
return true;
}