locked
How to get FastCGI NamedPipe handle in C RRS feed

  • Question

  • User-466302488 posted

    Hi

    I try to connect my application in C with IIS via NamedPipe but I cannot get a pipe handle. CreateFile function always return ERROR_PIPE_BUSY value. What is correct way to get this handle? This is my source code:

    #include <Windows.h>
    #include <stdio.h>
    #pragma warning(disable : 4996)
    
    static void Log(const char * log, int logLen)
    {
    	FILE *	file;
    
    	file = fopen("C:\\Programy\\fastCGI\\Debug\\log.log", "ab");
    	fwrite(log, 1, logLen, file);
    	fwrite("\n", 1, 1, file);
    	fclose(file);
    }
    static void LogTxt(const char * log)
    {
    	Log(log, strlen(log));
    }
    static void LogErrCode(const char * log, int logLen, int err)
    {
    	char	buf[1024];
    
    	itoa(err, buf, 10);
    
    	strcat(buf, log);
    	LogTxt(buf);
    }
    
    int main(int argc, char * argv[])
    {
    	HANDLE		pipe;
    	BOOL		fSuccess = FALSE;
    	wchar_t		pipeName[1024];
    
    	LogTxt(getenv("_FCGI_X_PIPE_"));
    
    	while (1)
    	{
    		pipe = CreateFileA(getenv("_FCGI_X_PIPE_"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    
    		// Break if the pipe handle is valid. 
    		if (pipe != INVALID_HANDLE_VALUE)
    			break;
    
    		// Exit if an error other than ERROR_PIPE_BUSY occurs. 
    		if (GetLastError() != ERROR_PIPE_BUSY)
    		{
    			LogErrCode(" Could not open pipe.", 21, GetLastError());
    			return -1;
    		}
    
    		// All pipe instances are busy, so wait for 2 seconds. 
    		if (!WaitNamedPipeA(getenv("_FCGI_X_PIPE_"), 2000))
    		{
    			LogErrCode(" Could not open pipe: 2 second wait timed out.", 45, GetLastError());
    			return -1;
    		}
    	}
    	return 0;
    }

    When I read from stdin I get the request, but how to send a response?

    This is my source code to read from stdin:

    int main()
    {
    	char	buf[1024];
    	HANDLE	hStdin;
    	HANDLE	hStdout;
    	HANDLE	hStderr;
    	DWORD	bytes;
    
    	hStdin = GetStdHandle(STD_INPUT_HANDLE);
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    	hStderr = GetStdHandle(STD_ERROR_HANDLE);
    
    	if (hStdin == INVALID_HANDLE_VALUE)
    		LogErrCode("STDIN", 5, GetLastError());
    
    	// hStdout and hStderr always return INVALID_HANDLE_VALUE
    	if (hStdout == INVALID_HANDLE_VALUE)
    		LogErrCode("STDOUT", 6, GetLastError());
    	if (hStderr == INVALID_HANDLE_VALUE)
    		LogErrCode("STDERR", 6, GetLastError());
    
    	ReadFile(hStdin, buf, 1024, &bytes, 0);
    	return 0;
    }


    Thanks for any advice

    Monday, December 19, 2016 11:09 AM

All replies