-1

How can i implement listener in non-console C++ application to listen for standard input from Adobe AIR?
in C# console app it look like:

namespace HelloNativeProcess
{
    class Program
    {
        static void Main(string[] args)
        {

            using (Stream stdin = Console.OpenStandardInput())
            using (Stream stdout = Console.OpenStandardOutput())
            {
                byte[] buffer = new byte[2048];
                int bytes;
                while ((bytes = stdin.Read(buffer, 0, buffer.Length)) > 0)
                {
                    stdout.Write(buffer, 0, bytes);
                }
            }
        }
    }
}

I need an example in C++ with APIENTRY winMain() - starting function. Thanks.

Y Borys
  • 543
  • 1
  • 5
  • 21
  • Not sure why you want to do this but probably take a look at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231(v=vs.85).aspx And then use ReadFile on this from a thread and post data to the main thread's message queue as required. – Pete Apr 04 '13 at 12:04
  • The reason i'm doing it is that i have c++ class to control TWAIN image scanners. When i use it in winMain application it works perfect. My previous goal was to create an native extension for adobe AIR, based on DLL, but in case of using class in DllMain it crashes when closing Twain Device UI window. So i decide to create native process to communicate using standard I/O, but don't have any clue how to listen CIN in this case. My prevoius question about DllMain is here http://stackoverflow.com/questions/15792494/adobe-air-native-extension-twain-image-scanner – Y Borys Apr 04 '13 at 15:11
  • You could actually use named pipes for this instead – Pete Apr 05 '13 at 11:19

1 Answers1

0

Found an answer. Thanks to Pete.

HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateAppThread, (LPVOID)NULL, 0, NULL);

Thread function

 BOOL CreateAppThread()
    {       
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        hStdin = GetStdHandle(STD_INPUT_HANDLE);
        CHAR buffer [2048];
        DWORD bytes_read, bytes_written;
            while(ReadFile( hStdin, buffer, sizeof(buffer), &bytes_read, NULL))
            {   
                WriteFile(hStdout, buffer, sizeof(buffer), &bytes_written, NULL);
            }       
            return TRUE;    
    }
Y Borys
  • 543
  • 1
  • 5
  • 21
  • You should make it possible to end the thread without having to terminate it by having a flag in the inner loop. When you application exits its should signal the that thread should stop and then wait on the thread handle for it to complete. This is slightly tricky, however since ReadFile will block and you can't use an overlapped ReadFile. So, you probably need to open a file to $CONIN and wait on its handle for events: http://stackoverflow.com/questions/4551644/using-overlapped-io-for-console-input – Pete Apr 05 '13 at 11:18