//Demo Knihovny knihovna.dll
//22.3.2010
// hacesoft
// Pavel Prochazka.zde.cz
//
//***********************************************************************************************//
#pragma comment(lib,"knihovna\\wwio.lib")
#include "Windows.h"

#define Ha_OK 0
#define Ha_ERROR -1
using namespace System;

typedef struct snUSB_LPT {//konfigurace USB_LPT
	PTCHAR NamePort;		//out
	int LengthNamePort;		//out
	PTCHAR DeviceDesc;		//out
	int LengthDeviceDesc;	//out
	PTCHAR Service;			//out
	int LengthService;		//out
	PTCHAR Mfg;				//out
	int LengthMfg;			//out
	PTCHAR HardwareID;		//out
	int LengthHardwareID;	//out
	PTCHAR ClassGUID;		//out
	int LengthClassGUID;	//out
}sUSB_LPT;

#define S0_R 1
#define S1_R 2
#define S2_nIRQ 4
#define S3_nERR 8
#define S4_ONL 16
#define S5_PE 32
#define S6_nACK 64
#define S7_nBSY 128

#define C0_nSTB 1
#define C1_nAF 2
#define C2_nINT 4
#define C3_nSEL 8
#define C4_IEN 16
#define C5_DIR 32
#define C6_nc 64
#define C7_nc 128

#define InitStatus ~(S2_nIRQ | S3_nERR | S4_ONL | S5_PE | S6_nACK )
#define InitControl ~( C2_nINT | C4_IEN | C5_DIR )

#define SPP 0x5
#define Bidirection 0x25
#define AutoStrobe 0x45
#define ECP 0x65
#define EPP 0x85
#define RESERVED 0xA5
#define TestMode 0xC5
#define Configuration 0xE5

#define DataPort 0
#define Status 1
#define Control 2
#define ECR 10

typedef struct {//konfigurace portu
	int DMA;						//out
	int IRQ;						//out
	int Type_Port;					//out
	int adress;						//out
	int range;						//in
	PCHAR port;						//in
	struct snUSB_LPT *s_USB_LPT;	//out
}CONFIG_PORT;

typedef struct {//Existing_Driver
	unsigned int DriverInstall;	//out
	unsigned short DriverStart;	//out
}EXISTING_DRIVER;

typedef struct nWwioRwData{//Existing_Driver
	unsigned int Data;			//inout -> Data na zapis/cteni
	unsigned int Adress;		//out -> Adresa kde se cetlo/zapisovalo
}Wwio_RwData;

typedef struct {//Registr provedeni funkci pri ukoncovani prace s ovladacem giveio.sys
	unsigned r_OpenSCManager : 1; /*bit 0 -->  */
	unsigned r_DriverStop : 1; /*bit 1 -->  */
	unsigned r_DriverRemove : 1; /*bit 2 -->  */
	unsigned r_RegDeleteKey : 1; /*bit 3 -->  */
	unsigned r_Existing_Driver : 1; /*bit 4 -->  */
	unsigned r_UnLock : 1; /*bit 5 -->  */
	unsigned r_Lock : 1; /*bit 6 -->  */
}RETURN_CODE;



void DemoKnihovna_Dll(int nLenghtString);
void WriteConsoleWwio(int nRnt,String ^ sString ,int nLenghtString);
void WriteConsoleOpenPort(int nRnt,CONFIG_PORT Config_Port ,int nLenghtString);
void WriteConsoleRead(int nRnt,Wwio_RwData WwioRwData,int nLenghtString);
void WriteConsoleWrite(int Data,int nRnt, Wwio_RwData WwioRwData,int nLenghtString);
void WriteConsoleExistingDriver(int nRnt,EXISTING_DRIVER ExistingDriver,int nLenghtString);

#define DllImport __declspec(dllimport)

extern "C"
//Není zde nic zvláštního - pro nás neobvyklá je deklarace funkce s extern "C" (která je potřeba k tomu, aby překladač nedoplňoval ke jménu funkce dekoraci z C++), 
//a dále klíčové slovo _cdecl, které zdůrazňuje volací konvenci CDECL, která se používá v programovacím jazyce C

//Deklarace DLL funkce v jazyce Microsoft Visual C++
//Tento příklad demonstruje, jak vypadá rozhraní funkcí "factor" a "nibble" z příkladů 1 a 2 v jazyce C. Proměnná typu "double" má délku 8B, proměnná typu "char" 1B a proměnná typu "short" 2B
//extern "C" double __declspec(dllexport) __cdecl factor(short n); 
//extern "C" char __declspec(dllexport) __cdecl nibble(char B, char *bit1, char *bit2, char *bit3, char *bit4);
{
DllImport unsigned int WINAPI Lock_(__out RETURN_CODE* ReturnCode = NULL);
DllImport int WINAPI Existing_Driver(__out EXISTING_DRIVER* ExistingDriver);
//DllImport int WINAPI UnLock_(void);
DllImport int WINAPI Error_(void);
DllImport int WINAPI Write_Data(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Write_Status(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Write_Control(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Write(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Read(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Read_Data(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Read_Status(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI Read_Control(__inout nWwioRwData *WwioRwData);
DllImport int WINAPI OpenPort(__inout CONFIG_PORT* Port_Config);
DllImport void WINAPI ErrorEx(__in LPCWSTR text);
DllImport int WINAPI Wait(unsigned int delka);
DllImport int WINAPI WaitEx(unsigned int delka);
namespace wwio_USB2LPT{
	DllImport bool WINAPI ReadSerialAndDate (WORD adr, WORD date, DWORD BytesRet, FILETIME ft, SYSTEMTIME st, LPTSTR sBuffer, int LenghtsBuffer);
	DllImport void WINAPI WriteData(BYTE a, BYTE b);
	DllImport BYTE WINAPI ReadData(BYTE a);
	DllImport void WINAPI WriteSerial(DWORD sn);
	DllImport void WINAPI GetPinStates(BYTE states[], DWORD BytesRet);
}
}
