separated COM pipes for windows host

Here you can provide suggestions on how to improve the product, website, etc.
Post Reply
dldall
Posts: 6
Joined: 18. Jan 2016, 14:33

separated COM pipes for windows host

Post by dldall »

The things must not be repeated for others!

1.
In the morning I wanted to use COM pipe in windows host with your VM VirtualBox,
i believed i can do this just passing by the PC,
i intended to do something like this

FILE *fp=fopen(pipe_name,"ab+");
if(!fp){ perror("open pipe"); exit(1); }
...

fputc(fp,ch);
ch= fgetc(fp);

but insted of this i spent 3 and more hours trying to recombinate parameters of CreateFile/WriteFile/_beginthreadex etc
to guess what the hell does not work

2.
As i was able to undestand by the stupid experimentation with stdlib,
any read operation blocking any write operation to the same HANDLE
what is why i can not set terminal to work over COM

In global point of view,
i'm sure to read/write text with FILE* i must not write 250 lines of code with async IO etc
so
to resolve the problem there are to ways:

1) let VMWare Serial Line Gateway\vmwaregateway.exe" /t /v
will be able to accept two optional extra parameters
vmwaregateway.exe [pipe_name] [port_name] /t /v
also i can say vmwaregateway is not ready to support UAC (incorrect rigths to "\\\\.\\pipe\\vmwaredebug" used)

2) let Oracle VM VirtualBox split single windows pipe into two separated
first separated pipe to write to com port
second separated pipe to read from com port

your choice.
PS:
there is example of the my first code that is not work

Code: Select all

#if 0
FILE	*fp;
	fp= _wfopen(pname, L"wb");
	if(!fp){ _wperror(pname); exit(1); }

	fprintf(fp, "%s\n", "t");
	fprintf(stdout, "%c%c", getc(fp), getc(fp));
#endif
but i try to accept windows pipes (my second code that is not work too)

Code: Select all

#include "stdafx.h"

namespace{
_TCHAR		*pname = L"\\\\.\\pipe\\vmwaredebug";
HANDLE		hp,ihp,ohp;

void	open_pipe(_TCHAR *pname){

	hp = CreateFileW( 
         pname,				// pipe name 
         GENERIC_READ |		
         GENERIC_WRITE,		// read and write access 
         FILE_SHARE_READ |
		 FILE_SHARE_WRITE,	//sharing 
         //0,				// no sharing 
         NULL,				// default security attributes
         OPEN_EXISTING,		// opens existing pipe 
         0,					// default attributes 
         NULL				// no template file 
		 );          
 
	// Break if the pipe handle is valid. 
	if (hp != INVALID_HANDLE_VALUE)return;

	fwprintf( stdout, L"Could not open pipe. GLE=%d\n", GetLastError() ); 
	exit(1);
}

#if 0
void	open_pipe_read(_TCHAR *pname){

	ihp = CreateFile( 
         pname,				// pipe name 
         GENERIC_READ,		// read access 
         FILE_SHARE_READ |
		 FILE_SHARE_WRITE,	//sharing 
         NULL,				// default security attributes
         OPEN_EXISTING,		// opens existing pipe 
         0,					// default attributes 
         NULL);				// no template file 
 
	// Break if the pipe handle is valid. 
	if (ihp != INVALID_HANDLE_VALUE)return;

	fwprintf( stdout, L"Could not open pipe to read. GLE=%d\n", GetLastError() ); 
	exit(1);
}

void	open_pipe_write(_TCHAR *pname){

	ohp = CreateFile( 
         pname,				// pipe name 
         GENERIC_WRITE,		// write access 
         FILE_SHARE_READ |
		 FILE_SHARE_WRITE,	//sharing 
         NULL,				// default security attributes
         OPEN_EXISTING,		// opens existing pipe 
         0,					// default attributes 
         NULL);				// no template file 
 
	// Break if the pipe handle is valid. 
	if (ohp != INVALID_HANDLE_VALUE)return;

	fwprintf( stdout, L"Could not open pipe to read. GLE=%d\n", GetLastError() ); 
	exit(1);
}
#endif


//
void	write_pipe(char *buf, unsigned len){

BOOL	fSuccess;
DWORD	written;

	//assert(buf);
	//assert(len);
	if(!buf){ fwprintf( stdout, L"pipe: write: buf zptr\n" ); exit(1);	}
	if(!len){ fwprintf( stdout, L"pipe: write: buf zparam\n" ); exit(1); }
	//fprintf( stdout, "write hp: %x\n", hp ); 

	fSuccess = WriteFile( 
      hp,				// pipe handle 
      buf,				// message 
      len,				// message length 
      &written,			// bytes written 
      NULL				// not overlapped 
	  );                  

	if(!fSuccess){
	fwprintf( stdout, L"pipe: could not write. GLE=%d\n", GetLastError() ); 
	exit(1);
	}

	if(written != len){
	fwprintf( stdout, L"pipe: not all written: %u (got %u). GLE=%d\n", len, written, GetLastError() ); 
	exit(1);
	}
}

unsigned	read_pipe(char *buf, unsigned len){

BOOL	fSuccess;
DWORD	did_read;

	//assert(buf);
	//assert(len);
	if(!buf){ fwprintf( stdout, L"pipe: read: buf zptr\n" ); exit(1);	}
	if(!len){ fwprintf( stdout, L"pipe: read: buf zparam\n" ); exit(1); }
	//fprintf( stdout, "read hp: %x\n", hp ); 

	fSuccess = ReadFile( 
         hp,					// pipe handle 
         buf,					// buffer to receive reply 
         len,					// size of buffer 
         &did_read,				// number of bytes read 
         NULL					// not overlapped 
		 );    

	//err= GetLastError(); 
	//fprintf( stdout, "got: %u\n", did_read ); 
	
	//998 as EOF?
	//if( err == 998 )return 0;

	//
	if(!fSuccess){
	fwprintf( stdout, L"pipe: could not read. GLE=%d\n", GetLastError() ); 
	exit(1);
	}

	if(did_read>len){
	fwprintf( stdout, L"pipe: read buf was owerflowed: %u (got %u)\n", len, did_read ); 
	exit(1);
	}

	return did_read;
}

unsigned	__stdcall con_in_thread(void *){
	
char		obuf[16*1024];

	for(unsigned u;;){

	u=getc(stdin);
	if(u==EOF){ fprintf(stderr,"con: %s\n","EOF"); exit(0); }
	//fprintf(stderr,"con: %c\n",u);
	
	obuf[0]=u;
	write_pipe(obuf,1);
	}

	return 0;
}

unsigned	__stdcall con_out_thread(void *){
	
char		ibuf[16*1024];

	for(unsigned n;;){

	//n= read_pipe(ibuf, sizeof(ibuf)-1 );
	n= read_pipe(ibuf, 1 );
	
	//fprintf(stdout,"got: %u\n",n);
	if(!n){ 
		//fprintf(stderr,"@zero got\n"); 
		continue; 
	}
	//fprintf(stderr,"n: %u\n",n);
	ibuf[n]=0;
	
	fprintf(stdout, "%s", ibuf);
	fflush(stdout);
	}
}

}//namespace

int _tmain(int argc, _TCHAR* argv[])
{
	if(argc >= 2){ pname = argv[1]; }
	fwprintf(stdout, L"%s\n", pname);

	//
	open_pipe(pname);
	#if 0
char		obuf[16*1024];
	//strcpy(obuf,"hello\n");
	strcpy(obuf,"\n");
	write_pipe(obuf,strlen(obuf));
	#endif

	#if 1
uintptr_t	in_tid;
	in_tid = _beginthreadex(NULL, 0, con_in_thread, NULL, 0, NULL);
	if(!in_tid){ perror("_beginthreadex"); exit(1); }
	#elif 1
	con_in_thread(0);
	#endif

	#if 0
	con_out_thread(0);
	#endif

	for(;;);
	return 0;
}
===
end of text
Post Reply