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

📄 virtual_win32_api.c

📁 Virtual Win32 API For BSD UNIX, 可在UNIX下直接编译呼叫标准Win32 API之C/C++原始码, 单一原始码可跨平台, 可在Windows下使用Visual C++
💻 C
字号:
#ifndef WIN32
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <string.h>
#include "virtual_win32_api.h"
extern int silence;
char* _strlwr(char *string)
{
	unsigned int i=0;
	for (i=0 ; i < strlen(string) ; i++)
		if(*(string+i) >= 'A' && *(string+i) <= 'Z')
			*(string+i) = *(string+i) + 32;
		return string;
}
#ifndef WIN32

HANDLE FindFirstFile(char* lpFileName,LPWIN32_FIND_DATA lpFindFileData)
{ // currently only finds *.*
	struct dirent* fp;
	struct stat statbuf;
	char fullpath[MAX_PATH] = "";
	int i;
	DIR* dp;
	for(i=strlen(lpFileName)-1;i>=0;i--)
	{
		if(*(lpFileName+i) == '/')
		{
			strncpy(fullpath,lpFileName,i);
			break;
		}
	}
	if(!silence)
		printf("opening : %s\n",fullpath);
	dp = opendir(fullpath);
	if(dp == NULL)
		return INVALID_HANDLE_VALUE;
	fp = readdir(dp);
	if(fp == NULL)
		return INVALID_HANDLE_VALUE;
	stat(fp->d_name,&statbuf);
	lpFindFileData->dwFileAttributes = 0;
	if( (statbuf.st_mode & S_IFDIR) == S_IFDIR)
		lpFindFileData->dwFileAttributes = lpFindFileData->dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY;
	else
		lpFindFileData->dwFileAttributes = lpFindFileData->dwFileAttributes | FILE_ATTRIBUTE_NORMAL;
	strcpy(lpFindFileData->cFileName,fp->d_name);
	strcpy(lpFindFileData->cWorkingDir,fullpath);
	return (HANDLE) dp;
}

BOOL FindNextFile(HANDLE hFindFile,LPWIN32_FIND_DATA lpFindFileData)
{
	char tmppath[MAX_PATH];
	struct dirent* fp;
	struct stat statbuf;
	DIR* dp = (DIR*) hFindFile;
	if(dp == NULL)
		return FALSE;
	fp = readdir(dp);
	if(fp == NULL)
		return FALSE;
	sprintf(tmppath,"%s/%s",lpFindFileData->cWorkingDir,fp->d_name);
	stat(tmppath,&statbuf);
	lpFindFileData->dwFileAttributes = 0;
	if( (statbuf.st_mode & S_IFDIR) == S_IFDIR)
		lpFindFileData->dwFileAttributes = lpFindFileData->dwFileAttributes | FILE_ATTRIBUTE_DIRECTORY;
	else
		lpFindFileData->dwFileAttributes = lpFindFileData->dwFileAttributes | FILE_ATTRIBUTE_NORMAL;
	strcpy(lpFindFileData->cFileName,fp->d_name);
	return TRUE;
}

BOOL FindClose(HANDLE hFindFile)
{
	DIR* dp = (DIR*) hFindFile;
	if(closedir(dp) < 0)
		return FALSE;
	else
		return TRUE;
}

/* remark : hi byte of file size is ignored */
DWORD GetFileSize(HANDLE hFile/* handle to file*/,LPDWORD lpFileSizeHigh/*high-order word of file size*/)
{
	LP_V32_FILE_HANDLE vhandle = (LP_V32_FILE_HANDLE) hFile;
	struct stat statbuf;
	if(stat(vhandle->cFileName,&statbuf) != -1)
	{
		if(lpFileSizeHigh != NULL)
			*lpFileSizeHigh = 0;
		return statbuf.st_size;
	}
	else
		return -1;
}

HANDLE CreateFile(LPCTSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
{
	char mode[8];
	LP_V32_FILE_HANDLE p_file_handle;
	if((dwDesiredAccess&GENERIC_READ) == GENERIC_READ)
		strcpy(mode,"r");
	if((dwDesiredAccess&GENERIC_WRITE) == GENERIC_WRITE)
	{
		if(*mode == 'r')
			strcat(mode,"+");
		else
		{
			if((dwCreationDisposition&TRUNCATE_EXISTING) == TRUNCATE_EXISTING)
				strcpy(mode,"w");
			else if((dwCreationDisposition&CREATE_NEW) == CREATE_NEW)
				strcpy(mode,"w");
			else if((dwCreationDisposition&CREATE_ALWAYS) == CREATE_ALWAYS)
				strcpy(mode,"w");
			else if((dwCreationDisposition&OPEN_EXISTING) == OPEN_EXISTING)
				strcpy(mode,"a");
			else if((dwCreationDisposition&OPEN_ALWAYS) == OPEN_ALWAYS)
				strcpy(mode,"w");
		}
	}
	if(*mode == NULL)
		strcpy(mode,"r");
	p_file_handle = (LP_V32_FILE_HANDLE) malloc(sizeof(V32_FILE_HANDLE));
	p_file_handle->filepointer = fopen(lpFileName,mode);
	p_file_handle->h_type = 1;
	strcpy(p_file_handle->cFileName,lpFileName);
	return (HANDLE) p_file_handle;
}

BOOL CloseHandle(HANDLE hObject)
{
	LP_V32_HANDLE p_handle;
	if(hObject == NULL)
		return;
	p_handle = (LP_V32_HANDLE) hObject;
	if(p_handle->h_type == 1) /* HANDLE type is FILE */ 
	{
		LP_V32_FILE_HANDLE p_file_handle;
		p_file_handle = (LP_V32_FILE_HANDLE) p_handle;
		fclose(p_file_handle->filepointer);
		free(p_file_handle);
	}
}

DWORD GetCurrentDirectory(DWORD nBufferLength,LPTSTR lpBuffer)
{
	if(getcwd(lpBuffer,nBufferLength) != NULL)
		return strlen(lpBuffer);
	else
		return 0;
}

BOOL ReadFile(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped)
{
	LP_V32_FILE_HANDLE p_file_handle = (LP_V32_FILE_HANDLE) hFile;
	*lpNumberOfBytesRead = fread((void*)lpBuffer,1,nNumberOfBytesToRead,p_file_handle->filepointer);
	if(nNumberOfBytesToRead == *lpNumberOfBytesRead)
		return 1;
	else
		return 0;
}

BOOL WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped)
{
	LP_V32_FILE_HANDLE p_file_handle = (LP_V32_FILE_HANDLE) hFile;
	*lpNumberOfBytesWritten = fwrite((void*)lpBuffer,1,nNumberOfBytesToWrite,p_file_handle->filepointer);
	if(nNumberOfBytesToWrite == *lpNumberOfBytesWritten)
		return 1;
	else
		return 0;
}


#endif

⌨️ 快捷键说明

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