⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myvc.cpp

📁 170话费查询系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// myvc.cpp : Defines the initialization routines for the DLL.
//

#include "io.h"
#include "stdafx.h"
#include "myvc.h"

#include	<srllib.h>
#include	<dxxxlib.h>
#include	<dtilib.h>
#include	<sctools.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CMyvcApp

BEGIN_MESSAGE_MAP(CMyvcApp, CWinApp)
	//{{AFX_MSG_MAP(CMyvcApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyvcApp construction

CMyvcApp::CMyvcApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMyvcApp object

CMyvcApp theApp;



///////////////////////////////////////////////////////////////////////////////////////////////
//  陈旭编写于 2000-12-20            >>>>>>>>>>>>>>>>>>>>


/**********************************************************************************************
*
*
*                                     全局变量
*
*
**********************************************************************************************/
static int     SemaphoreNumber = 0;
static bool    SemaphoreExist  = false;
static bool    GCAExist        = false;

static int * pMutex;

HANDLE  handle_sem;

char   *DigitVoc[]={"零","一","二","三","四","五","六","七","八","九"};
/**********************************************************************************************
*
*
*                                     通用函数
*
*
**********************************************************************************************/

///////////////////////////////////////////////////////////////////////////////////////////////
//                 
//   函数  int PASCAL ReplaceStr(char * str, char *ch_1, char *ch_2, char *n, char * rep, char *outstr)
//   
//   功能:将 str中的第 n个以ch_1开头,ch_2结尾的子串替换成rep, 然后返回outstr.
//   参数:
//         str		: 字符串, 如 "我喜欢<红与白>,不喜欢<老人与海>"
//         ch_1		: 起始字符,如 '<'
//         ch_2		: 终止字符,如 '>'
//		   num		: 第n个以ch_1开头,ch_2结尾的字符串。n = 2.
//		   rep      : 替换字符串,如 "<飘>"
//		   outstr   : 结果字符串,如以上参数则返回: "我喜欢<红与白>,不喜欢<飘>"
//         如果 ch_2 为空字符串,则将 ch_1位置的字符视做一个字符串进行替换。
//
//   返回:被替换的字符串个数
//
///////////////////////////////////////////////////////////////////////////////////////////////
extern "C" _declspec ( dllexport ) int PASCAL ReplaceStr(char * str, char *ch_1, char *ch_2, char *num, char * rep, char *outstr)
{
   char ReturnStr[TXT_LEN];
   char c_1, c_2;
   int  p_1, p_2, n, i, j;
   int  rep_number=0;
   char *p;

   c_1 = ch_1[0];
   if (strcmp(ch_2,"")==0)
   {
	   c_2 = NULL;
   }
   else
   {
		c_2 = ch_2[0];
   }

   n   = atoi(num);
   if (n==0) {
		return 0;
   }

   i = 0;
   j = 0;
   while(i<n && str[j]!='\0'){
		if (str[j] == c_1) i++;
		j++;
   }
   if (i!=n) {						//找到第 n 个 ch_1
		return 0;					//没有指定子串
   }
   
   p_1 = j-1;						//ch_1的位置
   p_2 = 0;
   if (c_2==NULL){
		p_2 = p_1;
   }
   else
   {
	   while(str[j]!='\0' && str[j]!=c_1){
		   if (str[j] == c_2){
				p_2 = j;			//ch_2的位置
				break;
		   }
		   j++;
	   }
   }

   if (p_2 == 0) {
		return 0;
   }

   for (i=0;i<p_1;i++){
		ReturnStr[i]=str[i];
   }
   ReturnStr[i]='\0';

   strcat(ReturnStr,rep);

   p= str+p_2+1;
   strcat(ReturnStr,p);

   strcpy(outstr, ReturnStr);
   return 1;
}

////////////////////////////////////////////////////////////////
//                 
//  函数  LPVOID PASCAL getsubstring(char * instr , int startpos, int len, char * outstr)
//  说明:取子串
//   instr          : 字符串
//   startpost		: 起始位置从1开始
//   len			: 长度
//   outstr         : 子串
////////////////////////////////////////////////////////////////
extern "C" _declspec ( dllexport ) LPVOID PASCAL getsubstring(char * instr , int startpos, int len,char * outstr)
{
   char tmpstr[1024];
   int  slen, i;
   char * p;

   slen = strlen(instr);
   if (startpos <1 || startpos > slen) {
       strcpy(outstr, "");
	   return NULL;
   }
   
   p = instr;
   p = p+(startpos-1);

   i=0;
   strcpy(tmpstr,"");
   while (i<len && (*p)!='\0'){
         tmpstr[i] = *(p+i);
		 i++;
   }
   tmpstr[i] = '\0';
   strcpy(outstr, tmpstr);
   return outstr;

}

/************************************************************
 *        NAME: WinPrintf()
 * DESCRIPTION: Popup error message box for cases where
 *              no child windows exist.
 ************************************************************/
extern "C" _declspec ( dllexport ) void WinPrintf(char *szFormat, ...)
{
   char szBuffer[256];
   va_list pArguments;
   HWND hWnd;

   hWnd=AfxGetMainWnd()->m_hWnd;
   va_start(pArguments,szFormat);
   vsprintf(szBuffer, szFormat, pArguments);
   MessageBox(hWnd, szBuffer, "Program message...", MB_SYSTEMMODAL | MB_OK);
}

/*************************************************************
 Function: trimstr()
 删除字符串前后的空格
*************************************************************/
extern "C" _declspec ( dllexport ) void trimstr(char * str, char * output)
{   char ret[TXT_LEN];
    int  len;

    strcpy(ret ,str);
	
    while (ret[0] == ' ')
	{
	   strcpy(ret, &ret[1]);  //去前面的空格
	};

	len = strlen(ret);
	while(ret[len-1]==' ')    //去后面的空格
	{  
		ret[len-1]='\0';
        len = strlen(ret);
	};

	strcpy(output, ret);
    return;
}
/*************************************************************
 Function: upstring()
 将字符串转换成大写
*************************************************************/
extern "C" _declspec ( dllexport ) void upstring(char * str, char * output)
{
    char tmpstr[TXT_LEN];
	int  len,i;

	strcpy(tmpstr, str);
	len = strlen(tmpstr);
	for (i=0; i<len; i++) {
		if (isalpha(tmpstr[i])!=0) {
	      tmpstr[i] = toupper(tmpstr[i]);
		}
	}
	strcpy(output,tmpstr);
}


/**********************************************************************************************
*
*
*                               		语音文件索引表操作函数
*
*
**********************************************************************************************/

/***************************************************************
*
*    函数 CreateVFIT(char * s_number)
*    说明:在内存中创建VFIT
* 
***************************************************************/
extern "C" _declspec ( dllexport ) int PASCAL CreateVFIT(char * s_number)
{
	char msg[TXT_LEN];
    int  number;
    HANDLE hMap;                 //内存文件句柄
    //LPVOID p_Map;                //内存文件的指针,可以象数组一样操作

	number = atoi(s_number);
	if (number<=0){
       return 0;
	}
	hMap = CreateFileMapping((HANDLE)-1,NULL,PAGE_READWRITE,0, sizeof(VFIT)*number,VoiceFileIndexTableName);
    if (hMap != NULL)  { //文件创建成功

		if(GetLastError() == ERROR_ALREADY_EXISTS) {
           //::MessageBox(NULL, "语音文件索引表件已经存在", "信息", MB_OK);	
		}
		else{
		   //::MessageBox(NULL, "语音文件索引表件创建成功", "信息", MB_OK);
		}           
		//共享资源已经创建,然后创建资源共享琐
		CreateShareLock(MUTEX_VFIT);
        return 1;
	} 
	else {
		sprintf(msg, "创建/打开共享文件%s出错",VoiceFileIndexTableName);
	    ::MessageBox(NULL, msg, "信息", MB_OK);	
		return 0;   
	}
}


/****************************************************************************
 函数: int SetVFIT(char *  v_index, char *   key, char *   v_value)
 说明:给初始化文件赋值
 index    : 信号灯位置(取值 1 .. 30)
 key      : 项目 (取值 "indexname", "vocfile", "io_offset")
 value    : 用于复制的变量
****************************************************************************/
extern "C" _declspec ( dllexport ) int PASCAL SetVFIT(char *  v_index, char *   skey, char *  v_value)
{   	
	VFIT * pSem;
    char msg[1024], value[1024];
	char key[40];
    int index;
    HANDLE hMap;                 //内存文件句柄
    LPVOID p_Map;                //内存文件的指针,可以象数组一样操作
   	HANDLE hlock;				 //共享锁

	//等待共享文件被释放,然后抢占并琐定
	hlock = WaitLock(MUTEX_VFIT);


    strcpy(value, v_value);
	index = atoi(v_index);

	strcpy(key, skey);
	upstring(key,key);

	//打开共享文件	
    hMap=::OpenFileMapping(FILE_MAP_WRITE,FALSE, VoiceFileIndexTableName);	

	if (hMap == NULL) {
		sprintf(msg, "打开共享文件%s出错",VoiceFileIndexTableName);
	    ::MessageBox(NULL, msg, "DLL message", MB_OK);	
		ReleaseLock(hlock);
		return 0;   
	}
    	
	//获得文件内存地址的指针
	p_Map = MapViewOfFile(hMap,FILE_MAP_WRITE ,0,0,0);
	if (p_Map == NULL) {
       ::MessageBox(NULL, msg, "p_Map is null)", MB_OK);	
	}
	pSem  = (VFIT *)p_Map;
	if (pSem == NULL) {
		::MessageBox(NULL, msg, "pSem is null)", MB_OK);	
	}

	if (strcmp(key, "INDEXNAME")==0) {
		strcpy(pSem[index-1].indexname ,value);
	}

    if (strcmp(key, "VOCFILE")==0) {
		strcpy(pSem[index-1].vocfile ,value);
	}

    if (strcmp(key, "IO_OFFSET")==0) {
	    strcpy(pSem[index-1].io_offset ,value);
	}
	
    if (strcmp(key, "IO_LENGTH")==0) {
	    strcpy(pSem[index-1].io_length ,value);
	}

    if (strcmp(key, "FILEHANDLE")==0) {
	    strcpy(pSem[index-1].filehandle ,value);
	}


	CloseHandle(hMap); 
    //if (!UnmapViewOfFile(p_Map))
	//{ AfxMessageBox("could not unmap view of file"); }
	ReleaseLock(hlock);
    return 1;
}
/****************************************************************************
函数: int  ReadVFIT(char * v_index, char * v_key, LPSTR  v_value)
 说明:给初始化文件赋值
 filename : 内存文件名
 index    : 信号灯位置(取值 1 .. 30)
 key      : 项目 (取值 "indexname", "vocfile", "io_offset","io_length", "filehandle")
 value    : 用于复制的变量
****************************************************************************/
extern "C" _declspec ( dllexport ) LPVOID PASCAL ReadVFIT(char * v_index, char * v_key)
{   	
	HANDLE hMap;        //内存文件句柄
	LPVOID p_Map;       //内存文件的指针,可以象数组一样操作
	LPVOID p_result;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -