📄 fstrcv.cpp
字号:
#include "stdafx.h"
#include <afxdllx.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static AFX_EXTENSION_MODULE FstrcvDLL = { NULL, NULL };
static int count=0;
static float vol;
static float amount;
static float price=10;
static float max;
static float min;
static float open;
static float close; //计数器
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("FSTRCV.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(FstrcvDLL, hInstance))
return 0;
new CDynLinkLibrary(FstrcvDLL);
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThreadProc,&DataPacketStruct,CREATE_SUSPENDED,&ThreadID);
SetThreadPriority(hThread,THREAD_PRIORITY_BELOW_NORMAL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
//TRACE0("FSTRCV.DLL Terminating!\n");
TerminateThread(hThread,1);
// Terminate the library before destructor are called
AfxTermExtensionModule(FstrcvDLL);
}
return 1; // ok
}
_declspec(dllexport) DWORD WINAPI InitCard(HWND hWnd,UINT Msg)
{//得到主程序的句柄和数据处理消息
hMainWnd=hWnd;
uMessage=Msg;
Sleep(1000);
if (hWnd!=NULL)
{
bRestart=TRUE;
ResumeThread(hThread);
}
return (DWORD)hWnd;
}
_declspec(dllexport) void WINAPI EnableMessage(BOOL flag)
{ //数据生成线程开始和结束
bRestart=flag;
if (flag) //启动生成数据线程
{
ResumeThread(hThread);
}
else
SuspendThread(hThread); //数据生成线程挂起
}
_declspec(dllexport) void WINAPI GetCardInfo(DWORD * nCardNumber, DWORD * CardVer)
{
*nCardNumber=(DWORD)hMainWnd;
*CardVer=0x0001;
}
_declspec(dllexport) int WINAPI ReceiveInfo(void)
{ //得到生成的数据包的个数
return DataPacketNum;
}
_declspec(dllexport) int WINAPI ReadMyCount(void)
{
//return 100;
return ReadMyCountFile();
//return count;
}
_declspec(dllexport) void WINAPI DataAcquisition(MAINSTRUCT& Packet, int i)
{ //得到数据缓冲区中的数据
Packet=DataPacketStruct[i];
}
BYTE CreateDataType()
{
return (BYTE)0x10;//DataType;
}
void StoreMyCountFile(void)
{
try
{
CFile file(_T("count.txt"), CFile::modeReadWrite);
CArchive ar(&file,CArchive::store);
ar<<count<<open<<close<<price<<max<<min;
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
}
int ReadMyCountFile(void)
{
int size=0;
try
{
CFile file(_T("count.txt"), CFile::modeReadWrite);
CArchive ar(&file,CArchive::load);
size=file.GetLength();
if (size==0)
{
return 0;
}
ar>>count>>open>>close>>price>>max>>min;
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
return count;
}
BOOL CreateDataPacketStructs(int PacketNum)
{
float temp=0;
float r=0;
int i=0, j=0;
srand((unsigned)time(NULL));
for (int n=0;n<PacketNum;n++)
{
DataPacketStruct[n].Type=CreateDataType();
switch(DataPacketStruct[n].Type)
{
case 0x10: //index and stock
if (count%240 == 0)
{
r=rand()%3;
i = rand()%2;
if (i==1)
{
r = r*(-1);
}
close=price+r/10;//昨日收盘价
r=rand()%3;
i = rand()%2;
if (i==1)
{
r = r*(-1);
}
open=price+r/10;//今日开盘价
price=open;
max=open;
min=open;
vol=0;
amount=0;
count=0;
}
sprintf(DataPacketStruct[n].AllStruct.IndexAndStock.Code,"600000");
sprintf(DataPacketStruct[n].AllStruct.IndexAndStock.Name,"浦发银行");
DataPacketStruct[n].AllStruct.IndexAndStock.Close=close*1000;//昨日收盘价
DataPacketStruct[n].AllStruct.IndexAndStock.Open=open*1000;//今日开盘价
i = rand()%2;
j=rand()%10;
if (i==0) //跌
{
switch(j) {
case 0:
price*=0.999;
break;
case 1:
price*=0.998;
break;
case 2:
price*=0.997;
break;
case 3:
price*=0.996;
break;
case 4:
price*=0.995;
break;
case 5:
price*=0.994;
break;
case 6:
price*=0.993;
break;
case 7:
price*=0.992;
break;
case 8:
price*=0.991;
break;
case 9:
price*=0.990;
break;
}
if (min>price)
{
min=price;
}
if (min>close)
{
min=close;
}
}
else //涨
{
switch(j)
{
case 0:
price*=1.001;
break;
case 1:
price*=1.002;
break;
case 2:
price*=1.003;
break;
case 3:
price*=1.004;
break;
case 4:
price*=1.005;
break;
case 5:
price*=1.006;
break;
case 6:
price*=1.007;
break;
case 7:
price*=1.008;
break;
case 8:
price*=1.009;
break;
case 9:
price*=1.010;
break;
}
if (max<price)
{
max=price;
}
if (max<open)
{
max=open;
}
}
DataPacketStruct[n].AllStruct.IndexAndStock.High=max*1000;//今日最高价
DataPacketStruct[n].AllStruct.IndexAndStock.Low=min*1000;//今日最低价
DataPacketStruct[n].AllStruct.IndexAndStock.New=price*1000;//今日最新价
temp=rand()%10000; //虚拟单笔交易量
vol+=temp; //累计交易量
DataPacketStruct[n].AllStruct.IndexAndStock.Volume=vol;//总的交易量
amount+=price*vol; //累计成交钱数
DataPacketStruct[n].AllStruct.IndexAndStock.Amount=amount*1000;
DataPacketStruct[n].AllStruct.IndexAndStock.Pbuy1=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Pbuy2=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Pbuy3=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Pbuy4=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Vbuy1=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vbuy2=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vbuy3=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vbuy4=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Psell1=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Psell2=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Psell3=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Psell4=rand()%10000+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Vsell1=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vsell2=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vsell3=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Vsell4=(float)(rand()%10000+n);
DataPacketStruct[n].AllStruct.IndexAndStock.Syl=rand()%10000*100+n;
DataPacketStruct[n].AllStruct.IndexAndStock.Cjbs=(float)(rand()%1000/1000.0+n);
count++;
break;
default:
break;
}
}
return 0;
}
////////////////////////////////////////////////
//线程
UINT WorkerThreadProc(LPVOID pParam)
{
MAINSTRUCT *pMainStruct=(MAINSTRUCT *)pParam;
if (pMainStruct==NULL)
return -1;
while (true)
{
if(bRestart)
{
Sleep(1000);
ReadMyCountFile();
srand((unsigned)time(NULL));
DataPacketNum=1;
CreateDataPacketStructs(DataPacketNum);
StoreMyCountFile();
PostMessage(hMainWnd,uMessage,NULL,NULL);
Sleep(100);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -