?? readmc_dll.cpp
字號:
// ReadMC_dll.cpp : DLL 傾僾儕働乕僔儑儞梡偺僄儞僩儕 億僀儞僩傪掕媊偟傑偡丅
//
#include "stdafx.h"
#include "pccard.h"
/* must be erase-block aligned for CardSoft */
#define BUFFER_SIZE (16*1024)
//#define ENGLISH
#ifdef ENGLISH
const char *FX_CSErrMsg [] =
{
"",
"Unsupported OS was encountered. The program can run only on Windows95/98.",
"The card service handler was not found.",
"The card service driver was not installed.",
"Can not create the specified file.",
"Can not register to the card service as a client.",
"Can not find a memory card in any socket.",
"MTD (Memory Technology Driver) was not installed.",
"Can not obtain necessary information (CIS) of the card.",
"Insufficient memory.",
"Fail to read the contents of the card.",
NULL
};
#define COMMAND_SYNTAX_ERROR "Command Syntax Error.\n"
#define USAGE "Usage: readcard.exe path\\filename."
#define ERROR2 "The content of the card is saved into the file: %s\n"
#define CARDTYPE "Card type: %s\n"
#define CARDSIZE1 "Card size: %uMB\n"
#define CARDSIZE2 "Card size: %luKB\n"
#else
const char *FX_CSErrMsg [] =
{
"",
"僒億乕僩偝傟側偄OS偱偡丅偙偺僾儘僌儔儉偼Windows2000/XP偱偺傒摦嶌偟傑偡丅",
"僇乕僪僒乕價僗僴儞僪儔偑尒偮偐傝傑偣傫丅",
"僇乕僪僒乕價僗僪儔僀僶偑僀儞僗僩乕儖偝傟偰偄傑偣傫丅",
"巜掕偝傟偨僼傽僀儖傪嶌惉偡傞偙偲偑偱偒傑偣傫丅",
"僋儔僀傾儞僩偲偟偰僇乕僪僒乕價僗偵搊榐偡傞偙偲偑偱偒傑偣傫丅",
"僇乕僪僗儘僢僩偵儊儌儕僇乕僪偑尒偮偐傝傑偣傫丅",
"MTD偑僀儞僗僩乕儖偝傟偰偄傑偣傫丅",
" 僇乕僪偵娭偡傞忣曬(CIS)偑庢摼偱偒傑偣傫丅",
"儊儌儕晄懌偱偡丅",
"僇乕僪撉崬偵幐攕偟傑偟偨丅",
NULL
};
typedef struct tagMediaTypeName
{
CS_TYPE MediaType;
LPCSTR szMediaName;
} MediaTypeName;
MediaTypeName MediaNames[]= {{CSTYPE_ROM, "ROM"},
{CSTYPE_OTPROM, "OTPROM"},
{CSTYPE_EPROM, "EPROM"},
{CSTYPE_EEPROM, "EEPROM"},
{CSTYPE_FLASH, "FLASH"},
{CSTYPE_SRAM, "SRAM"},
{CSTYPE_DRAM, "DRAM"},
{CSTYPE_FUNCSPEC, "FUNCSPEC"},
{CSTYPE_EXTENDED, "EXTENDED"},
{CSTYPE_ATA, "ATA"},
{CSTYPE_NULL, NULL}};
#define COMMAND_SYNTAX_ERROR "僐儅儞僪峔暥偵岆傝偑桳傝傑偡丅\n"
#define USAGE "梡朄: readcard.exe 丂僼傽僀儖柤\n"
#define ERROR2 "僼傽僀儖%s偵奿擺偟傑偟偨丅\n"
#define CARDTYPE "僇乕僪僞僀僾: %s\n"
#define CARDSIZE1 "儊儌儕僒僀僘: %uMB\n"
#define CARDSIZE2 "儊儌儕僒僀僘: %luKB\n"
#endif
enum
{
ERROR_OS =1,
ERROR_Card_Services_Handler ,
ERROR_Card_Services_Driver ,
ERROR_Create_File ,
ERROR_Register_Client ,
ERROR_NO_Memory_Card ,
ERROR_Open_Memory ,
ERROR_Insufficient_CIS ,
ERROR_Not_Enough_Memory ,
ERROR_ReadCard_Failure
};
BYTE *Buffer;
int Check_OS(void)
{
DWORD dwVersion = GetVersion();
// Get major and minor version numbers of Windows
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
DWORD dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
if(dwWindowsMajorVersion != 5 )
{
return ERROR_OS;
}
return 0;
}
int Check_File(char * filename)
{
int fd = -1;
if ((fd = open
(filename, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0644)) < 0)
{
return ERROR_Create_File;
}
close (fd);
unlink(filename);
return 0;
}
int ReadCard(int Socket,int fd, DWORD start, DWORD end)
{
struct
{
DWORD CardOffset;
DWORD Count;
} ReadMem;
DWORD result;
DWORD count;
int retcode = 0;
lseek (fd, 0, SEEK_SET);
for (ReadMem.CardOffset = start; ReadMem.CardOffset <= end ;
ReadMem.CardOffset += BUFFER_SIZE)
{
ReadMem.Count = BUFFER_SIZE;
if (ReadMem.Count > end - ReadMem.CardOffset + 1)
ReadMem.Count = end - ReadMem.CardOffset + 1;
result = ReadMemoryRegion (Socket, ReadMem.CardOffset,ReadMem.Count);
if (result != NOERROR)
{
retcode = -1;
}
count = write (fd, Buffer, (WORD) ReadMem.Count);
if (count < 0)
{
retcode = -1;
break;
}
if (count < ReadMem.Count)
{
retcode = -1;
break;
}
}
return retcode;
}
//
// Description: Main function of memory card reading
// Processing Steps:
// 1 Load card service handler
// 2 Check if Card Service is installed
// 3 Find a valid memory card in PCMCIA sockets
// 4 Obtain a MTD handle
// 5 Get the card information via CIS
// 6 Read the contents of the card
// Return code:
// 0 success
// >0 error code
int ReadCard(char * filename)
{
int return_code=0;
int intR=0;
// char p[512];
DWORD CardSize;
struct GetStatus_s;
WORD Socket = 0xFFFF;
DWORD result;
int fd = -1;
DWORD start = 0, end = ULONG_MAX;
CS_TYPE CardType;
//-------- Card_Check -----------------
intR=CSOpen ();
if ( intR!=0 )
{
return ERROR_NO_Memory_Card;
//return ERROR_Card_Services_Handler;
}
result = CSGetCardServicesInfo();
if (result != NOERROR)
{
CSClose ();
return ERROR_Card_Services_Driver;
}
result = CSRegClient();
if(result != NOERROR)
{
CSClose ();
return ERROR_Register_Client;
}
/* result = CSDetectCard(Socket);
if (result != NOERROR)
{
CSRelClient();
CSClose ();
return ERROR_NO_Memory_Card;
}
*/
//-------- Get_Params -----------------
result = GetParams (Socket,CardType,CardSize);
if (result != NOERROR )
{
CSRelClient();
CSClose ();
return ERROR_Insufficient_CIS;
}
//-------- Read_Card -----------------
start = 0;
end = CardSize - 1;
if ((Buffer = (BYTE *)malloc (BUFFER_SIZE)) == NULL )
{
CSRelClient();
CSClose ();
return ERROR_Not_Enough_Memory;
}
if ((fd = open
(filename, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0644)) < 0)
{
free( Buffer );
CSRelClient();
CSClose ();
return ERROR_Create_File;
}
if (ReadCard (Socket,fd, start, end) < 0)
{
return_code = ERROR_ReadCard_Failure;
}
close (fd);
free( Buffer );
CSRelClient();
CSClose ();
return return_code;
}
extern "C" __declspec(dllexport) int ReadMC( char filename[] )
{
int return_code=0;
//-------- OS_Check -----------------
if ( (return_code=Check_OS())!=0 ) {
return return_code;
}
//-------- File_Check -----------------
if ( (return_code=Check_File(filename))!=0 ) {
return return_code;
}
//-------- Read_Memory_Card -----------------
if ( (return_code=ReadCard( filename))!=0 ) {
return return_code;
}
return return_code;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -