is it possible to use one header file to retrieve and feed 2 programs?

i want to retrieve information from a console when its run and feed it to my program.

i have created a header with global variables and two function, one to be called be console program, the other by my windows program. in my logic the data retrieved from console program will be saved in the global variables and when windows calls the feed function, it will read the global variables.

does this works??

ps i wanted to use sendmessage function. however, i could not figure out how to obtain my program's hwnd and give it to the console program.
Last edited on
But does my amateur program style work?

Also thank you for letting me know how to do it the proper way. THANKS
[quote = jcylam]i have created a header with global variables and two function, one to be called be console program, the other by my windows program. in my logic the data retrieved from console program will be saved in the global variables and when windows calls the feed function, it will read the global variables.[/quote]

NO - I can't see this happening. What you have here will be two separate processes and they can't directly cross share data like that.
You are into the realm of IPC (Inter-Process Communication). See Information on IPC here:
http://msdn.microsoft.com/en-us/library/aa365574%28VS.85%29.aspx
The CopyData option seems to be the best one for you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void RetrieveGT(GAZEOUT GToutput)
{
#define BUFFER 1024

	HWND hwndDestination,
		 hwndFound;

	COPYDATASTRUCT data;
	char pszNewWindowTitle [BUFFER];
	char pszOldWindowTitle [BUFFER];

	//Code obtained from http://support.microsoft.com/kb/124103
	GetConsoleTitle((LPWSTR)pszOldWindowTitle, BUFFER);

       // Format a "unique" NewWindowTitle.

    wsprintf((LPWSTR)pszNewWindowTitle,(LPCWSTR)"%d/%d",
                GetTickCount(),
                GetCurrentProcessId());

       // Change current window title.

    SetConsoleTitle((LPCWSTR)pszNewWindowTitle);

	   // Ensure window title has been updated.

    Sleep(40);

       // Look for NewWindowTitle.

    hwndFound=FindWindow(NULL, (LPCWSTR)pszNewWindowTitle);

       // Restore original window title.

    SetConsoleTitle((LPCWSTR)pszOldWindowTitle);
	//End of code use from MS support
	data.dwData = GAZETRACKER;
	data.cbData = sizeof (GToutput);
	data.lpData = &GToutput;

	hwndDestination = FindWindow ( TEXT ("GazeTrackerMouse"), TEXT ("Gaze Tracker Mouse v2"));

	if (hwndDestination != NULL)
		SendMessage (hwndDestination,WM_COPYDATA,(WPARAM) hwndFound,(LPARAM) (LPVOID) &data);
	else
		MessageBox (NULL, TEXT("Cannot send Windows Message to GTM v2"), TEXT ("TCP client"), MB_OK);

	return;
}

So far am i correct?
and then the receiving part which i do not understand from MS website
http://msdn.microsoft.com/en-us/library/ms649009(VS.85).aspx

this is what i wrote, but i doubt it anything near right apart from the fact its incomplete.
1
2
3
4
5
	case WM_COPYDATA:
		RetrievedGTout = (PCOPYDATASTRUCT) lParam;
		switch (
		//SetCursorPos (wParam, lParam);
		return 0;


GAZEOUT struct code
1
2
3
4
5
typedef struct
{
	long x;
	long y;
}GAZEOUT;
I think you may be on the wrong track here - I thought you were trying to send from the console window
to the GUI window - if so then all that stuff about getting the console window is unnecessary.

You should be trying to find the handle to the GUI window.

I'll come back to this later.
the MS website said i need the destination hwnd, then in wParam Handle to the window passing the data. http://msdn.microsoft.com/en-us/library/ms649011(VS.85).aspx

I m totally confuse with this. MS should really start hiring someone who can speak english doing the explaining of these function.

MSDN is extremely bad at explaining stuff.
OK I knocked up a test and it works.
We will be sending a simple data structure. The data structure looks like this:

CopyDataTest.h
1
2
3
4
5
6
#pragma once

struct DATATOCOPY
{
  char arr[80];
};


The console program looks like this. It simply loops and gets ten lines from the console and each one as it gets it across to the GUI program. Notice the global variables.
Note: When you use Sendmessage to send the WM_COPYDATA message, the third parameter can be used to put the handle of the window that is sending the message (in this case the console window handle) - but you dont have to put anything -that is why I have put NULL there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "stdafx.h"
#include <windows.h>
#include "CopyDataTest.h"

#include <iostream>

using namespace std;

//Make our data structure variable a global one
DATATOCOPY DTC;

//The windows wrapper struct
COPYDATASTRUCT CDS;


int main(int argc, char* argv[])
{
	
  //Sort out the data we want to send 
  
  
  for (int n =0; n<10; ++n)
  {
    cin.getline(DTC.arr,80);


    //Prepare the Windows wrapper structure
    CDS.cbData = 0;
    CDS.cbData = sizeof(DATATOCOPY);
    CDS.lpData = &DTC;

    //Find the handle of the  window we want to send to
    HWND hDestWnd;

    if ( ( hDestWnd = FindWindow(NULL, _TEXT("jcylam")) )!= NULL )
    {
      //send the data
      SendMessage(hDestWnd,WM_COPYDATA,NULL,(LPARAM) &CDS);
    }

  }

  return 0;
}



The GUI program windows procedure looks like this - I set up the window to have a caption of jcylam by the way.
It is a simple WINAPI program using MSVC.
Note the code for the WM_COPYDATA message.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
  
  
  //====Static variable to hold the data we will be copying
  static DATATOCOPY Data ={"Empty String - Nothing received yet"};//*****************
  

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;



//==The WM_COPYDAT Message ======================================
  case WM_COPYDATA:
    DATATOCOPY *PDTC;
    PCOPYDATASTRUCT pDataStruct;
    pDataStruct = (PCOPYDATASTRUCT) lParam;

    PDTC = (DATATOCOPY*) pDataStruct->lpData;
    //copy our data
    strcpy(Data.arr, PDTC->arr);
    InvalidateRect(hWnd,NULL,TRUE);//Repaint
    return true;
    break;

  case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);

    //=========Paint our message=======
    TextOutA(hdc,10,10,Data.arr, strlen(Data.arr) );
		EndPaint(hWnd, &ps);
		break;
	
  
  
  case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

Topic archived. No new replies allowed.