//#define DEBUG
#include <windows.h>
#include <stdio.h>

#include "..\ClientChannel\channel.h"
#include "..\Utilities\Util.h"

// Display error
void error() {
	LPVOID lpMsgBuf;
		
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,    NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,    0,    NULL );// Display the string.

	MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
	LocalFree( lpMsgBuf );
}

void msg(char *lpMsgBuf)
{
	MessageBox( NULL, lpMsgBuf, "wwGetLastError", MB_OK|MB_ICONINFORMATION );
}

#ifdef GUICLIENT
int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE hinstExePrev,
				   LPSTR szCmdLine, int nCmdShow) {
#else
void main(int argc, char **argv) {
#endif
	int a;
	HANDLE hServer = NULL;
	int server_id;
	HWND topwindow;
	char *s;

//	char temp[1000];

	/*
	char buffer[1000];
	int dwServerId;
	DWORD result;
	*/
//	__asm int 3
//	temp[0] = NULL;
//	GetCurrentDirectory(1000,&temp);



	InitClientDLL();
	/*
	__asm int 3

		__asm int 3

		// dwServerId
		result = GetEnvironmentVariable("dwServerId",&buffer,100);
		if( !result ) {
			msg( "DllMain: ClientChannel.dll  could not find environment variable1" );
			ExitProcess(-1);
		}
		result = sscanf( buffer, "%u", &dwServerId);
		if( !result ) {
			msg ("conversion failed");
			ExitProcess(-1);
		}
	*/

	a = DoReqS( cleanstring("LibInit\n\n") );

	a = *((int *) (a + 4));

//	__asm int 3

	if( a == 0 ) {
		msg ( "client: adres bestaat niet" );
		ExitProcess(-1); }
	else
		CallFunction(a);
}

Bool CallFunction(int func)
{
	void (*MyFunc)() = func;

	if( func == NULL) {
		msg ( "CallFunction: adres is 0" );
		return 0;
	}

//	msg( "entering CLEAN" );

	MyFunc();

	return 1;
}
