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

📄 proemulatorapi.cpp

📁 一个用VC开发的嵌入式系统模拟器 可以对嵌入式系统进行软件测试
💻 CPP
字号:
#include <stdio.h>
#include "..\headers\ProEmulatorAPI.h"
#include "EmuGlobal.h"

//#include "debug.h"

//Null functions

void WINAPI nullFunc(){}
void WINAPI nullAPI_ShowInfo(LPCSTR nullarg){}
void WINAPI nullWriteSerial(char ch){}
int WINAPI nullReadSerial(char *p) {return 0;}

void WINAPI API_WriteCompileReport(LPCTSTR text)
{
	printf(text);
}

char* WINAPI API_LoadTextFile(LPCTSTR pFilename,char *beginWith, char *endWith)
{
	char *eof;
	HANDLE hFile;
	DWORD fileSize,bytesRead;
	static char* pTextFileBuffer=NULL;
	char* pRet;

	if (!pFilename){
		if (pTextFileBuffer){
		   free(pTextFileBuffer);
		   pTextFileBuffer=NULL;
		}
		return NULL;
	}
	hFile=CreateFile(pFilename,GENERIC_READ,FILE_SHARE_READ ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL);
	if (hFile==INVALID_HANDLE_VALUE){
		if (pTextFileBuffer) free(pTextFileBuffer);
		return NULL;
	}

	fileSize=GetFileSize(hFile,NULL);
	if (pTextFileBuffer){
		free(pTextFileBuffer);
	}
	pTextFileBuffer=(char*)malloc(fileSize+1);

	if (!pTextFileBuffer){
		CloseHandle(hFile);
		return 0;
	}

	ReadFile(hFile,pTextFileBuffer,fileSize,&bytesRead,0);
	CloseHandle(hFile);
	*(pTextFileBuffer+bytesRead)=0;

	if (beginWith)
	{
		if ((pRet=strstr(pTextFileBuffer,beginWith))==NULL)
			pRet=pTextFileBuffer;
	}
	else{
		pRet=pTextFileBuffer;
	}

	if (endWith){
		if ((eof=strstr(pRet,endWith))!=NULL)
			*eof=0;
	}
	return pRet;
}

DWORD WINAPI API_SendProcessorMsg(DWORD msg, WPARAM wParam, LPARAM lParam)
{
	return SendCPUMsg(msg,wParam,lParam);
}

void WINAPI API_SetHiliteLine(LPCTSTR pszSourceFilename,int nLineNumber)
{
}

void WINAPI API_SetCodeViewerText(char* pContent)
{
}

int WINAPI API_LoadTargetFile(int nFileType,LPCTSTR lpFilename,long offset)
{
	int nBytes;
	switch(nFileType){
	case TARGET_BINARY:
		nBytes=LoadBinFile(lpFilename,0,(LPBYTE)CODEMEM+offset,CODESIZE-offset);
		break;
	case TARGET_IHEX:
		nBytes=DecodeHexFile(lpFilename,(LPBYTE)CODEMEM+offset);
		break;
	default:
		nBytes=0;
	}
	SendCPUMsg(PM_CODELOADED,0,(LPARAM)lpFilename);
	return nBytes;
}

BOOL CALLBACK EnumThreadWndProc(HWND hwnd,LPARAM lParam)
{
	TCHAR buf[128];

	GetWindowText(hwnd,buf,sizeof(buf));
	MessageBox(0,buf,buf,MB_OK);
	return TRUE;
}

#define REDIRECT_BUFFER_SIZE 4
// dwFlag:
//	0 - hide window, log text
//	1 - hide window, no log text
//	2 -	show window
static char *lpBuffer=NULL;
static int cbBufSize;
char* WINAPI API_CallExternalApp(LPTSTR lpCommandLine,LPCTSTR pCurDir,LPVOID lpEnv,BOOL fRedirected,DWORD dwFlag)
{
	SECURITY_ATTRIBUTES saAttr={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
	PROCESS_INFORMATION piProcInfo;
	STARTUPINFO siStartInfo;
	BOOL fSuccess;
   	DWORD dwRead;
	char *p,*q;
	char env[4096],*pEnvironment;
	HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup, hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup;
	int nOffset=0,cnt,nLine=0;

///////////////////////////////////////////////////////////////////////
//	add extra environment variables
///////////////////////////////////////////////////////////////////////
	if (lpEnv)
	{
		q=GetEnvironmentStrings();
		p=env;
		while (*q || *(q+1))
			*(p++)=*(q++);
		*(p++)=0;
		q=(char*)lpEnv;
		while(*q)
		{
			if (*q=='\t')
			{
				*(p++)=0;
				q++;
			}
			else
				*(p++)=*(q++);
		}
		*p=0;
		*(p+1)=0;
		pEnvironment=env;
	}
	else{
		pEnvironment=NULL;
	}

	ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
	ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
	// Set up members of the STARTUPINFO structure.
	siStartInfo.cb = sizeof(STARTUPINFO);
	siStartInfo.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
	siStartInfo.wShowWindow=(dwFlag&0x2)?SW_SHOW:SW_HIDE;

///////////////////////////////////////////////////////////////////////
//	create redirecting pipes
///////////////////////////////////////////////////////////////////////

	if (fRedirected){
		saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
		saAttr.bInheritHandle = TRUE;
		saAttr.lpSecurityDescriptor = NULL;

		// Create a pipe for the child process's STDOUT.
		if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
			return 0;
		}
		// Create noninheritable read handle and close the inheritable read
		// handle.
		fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
				GetCurrentProcess(), &hChildStdoutRdDup , 0,
				FALSE,
				DUPLICATE_SAME_ACCESS);
		if( !fSuccess ) return 0;
		CloseHandle(hChildStdoutRd);

		// Create a pipe for the child process's STDIN.

		if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return 0;

		// Duplicate the write handle to the pipe so it is not inherited.

		fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
			  GetCurrentProcess(), &hChildStdinWrDup, 0,
			  FALSE,                  // not inherited
			  DUPLICATE_SAME_ACCESS);
		if (! fSuccess) return 0;
		CloseHandle(hChildStdinWr);

		siStartInfo.hStdError = hChildStdoutWr;
		siStartInfo.hStdOutput = hChildStdoutWr;
		siStartInfo.hStdInput = hChildStdinRd;

  		// allocate buffer
		if (!lpBuffer){
			lpBuffer=(char*)malloc(REDIRECT_BUFFER_SIZE);
			cbBufSize=REDIRECT_BUFFER_SIZE;
		}
	}

///////////////////////////////////////////////////////////////////////
//	create child process
///////////////////////////////////////////////////////////////////////
// Create the child process.

   fSuccess = CreateProcess(NULL,
      lpCommandLine, // command line
      NULL,          // process security attributes
      NULL,          // primary thread security attributes
      TRUE,          // handles are inherited
      CREATE_NEW_CONSOLE,      // creation flags
      pEnvironment,	 // process' environment
      pCurDir,      // current directory
      &siStartInfo,  // STARTUPINFO pointer
      &piProcInfo);  // receives PROCESS_INFORMATION

   if (fSuccess == 0) return 0;

// Close the write end of the pipe before reading from the
// read end of the pipe.

	if (fRedirected){
		CloseHandle(piProcInfo.hProcess);
		CloseHandle(piProcInfo.hThread);
		CloseHandle(hChildStdoutWr);
		while (ReadFile( hChildStdoutRdDup, lpBuffer+nOffset, cbBufSize-nOffset, &dwRead, NULL) && dwRead){
			for(cnt=nOffset;cnt<nOffset+(int)dwRead;cnt++){
				if (*(lpBuffer+cnt)=='\n') nLine++;
			}
			nOffset=cnt;
			if (cbBufSize-nOffset<64){
				cbBufSize+=REDIRECT_BUFFER_SIZE;
				lpBuffer=(char*)realloc(lpBuffer,cbBufSize);
				if (!lpBuffer) break;
			}
			*(lpBuffer+nOffset)=0;
			if (dwFlag==0) API_WriteCompileReport(lpBuffer+nOffset-dwRead);
		}
		cnt=nLine-(cbBufSize-nOffset)+1;
		if (cnt){
			//need to expand buffer
			cbBufSize+=cnt;
			lpBuffer=(char*)realloc(lpBuffer,cbBufSize);
		}
		ConvertUnixText(lpBuffer);
		CloseHandle(hChildStdinRd);
	}
	else
	{
		WaitForSingleObject(piProcInfo.hProcess,10);
	}
	return lpBuffer;
}

void MsgBoxThread(LPCTSTR info)
{	MessageBox(0,info,"ProEmulator",MB_OK|MB_ICONINFORMATION|MB_TOPMOST);}

void WINAPI API_MsgBoxA(LPCTSTR info)
{
	DWORD ThreadID;
	HANDLE hThread;
	hThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)MsgBoxThread,(LPVOID)info,0,&ThreadID);
	CloseHandle(hThread);
}

DWORD WINAPI API_GetSetting(LPCTSTR lpSectionName, LPCTSTR lpKeyName,LPCTSTR szDefault,LPTSTR buf)
{	
	return GetPrivateProfileString(lpSectionName,lpKeyName,szDefault,buf,128,inifile);
}

int WINAPI API_GetSettingInt(LPCTSTR lpSectionName, LPCTSTR KeyName,int nDefault)
{	
	return GetPrivateProfileInt(lpSectionName,KeyName,nDefault,inifile);
}

BOOL WINAPI API_WriteSetting(LPCTSTR lpSectionName, LPCTSTR KeyName,LPCTSTR KeyString)
{	
	return WritePrivateProfileString(lpSectionName,KeyName,KeyString,inifile);
}

BOOL WINAPI API_WriteSettingInt(LPCTSTR lpSectionName, LPCTSTR KeyName,int nKey)
{	
	char buf[16];
	return WritePrivateProfileString(lpSectionName,KeyName,itoa(nKey,buf,10),inifile);
}

HANDLE hTerm;
HWND hwndTerm;

void WINAPI WriteSerial(char ch)
{
	//DWORD dwWrite;
	//WriteFile(SerialDevice.hSerialWrite,&ch,1,&dwWrite,0);
	putchar(ch);
}

int WINAPI ReadSerial(char *p)
{
	//return serialbuf.Get(p);
	*p=getchar();
	return 1;
}

void WINAPI API_ShowInfo(LPCSTR lpszStatusString)
{
	ShowStatus(4,lpszStatusString);
}

HWND WINAPI API_GdbOpen(LPSTR pszGdbFilename,BOOL fNoWindow)
{
	return NULL;
}

void WINAPI API_GdbClose(BOOL fToCloseWindow)
{
}

char* WINAPI API_GdbQuery(char* pchCmd)
{
	*pchCmd=0;
	return pchCmd;
}

void WINAPI API_GdbUpdateInfo()
{
}

void WINAPI API_GdbShowInfo(char* pszText)
{
}

PEAPI ProEmuAPI={
	API_CallExternalApp,
	API_SetCodeViewerText,
	API_LoadTargetFile,
	API_WriteCompileReport,
	API_GetSetting,
	API_GetSettingInt,
	API_WriteSetting,
	API_MsgBoxA,
	nullReadSerial,			//null function for ReadSerial
	nullWriteSerial,		//null function for WriteSerial
	API_ShowInfo,
	API_WriteSettingInt,
	API_SendProcessorMsg,
	API_SetHiliteLine,
	API_LoadTextFile,
	API_GdbOpen,
	API_GdbClose,
	API_GdbQuery,
	API_GdbUpdateInfo,
	API_GdbShowInfo
};

⌨️ 快捷键说明

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