PDA

View Full Version : Intergrating my console program into a windows application


spadez
02-26-2009, 08:39 PM
Hi,

This is my code for my CSV file reader:

#include <stdio.h>
#include <stdlib.h>

#define NAME 1
#define DATE 2

#define NOQUOTE '\0'
#define SINGLEQUOTE '\''
#define DOUBLEQUOTE '\"'

typedef struct record_t {
char name[6];
char date[10];
struct record_t * next;
} record;

record * newRecord() {
record * temp = (record *)malloc(sizeof(record));
temp->next == NULL;
// all other fields should be set to NULL;
return temp;
}

void deleteList(record * a) {
record * b;
while (a->next != NULL) {
b = a->next;
free(a);
a = b;
}
}

void append(char * string, char character) {
while(* string != '\0') {
string++;
}
* string = character;
string++;
* string = '\0';
}

int main(int argc, char ** argv){
if(argc < 2){
printf("Usage:\n\t%s filename\n", argv[0]);
return -1;
}

FILE * fp;
if((fp = fopen(argv[1],"r")) == NULL){
printf("Can't open %s\n",argv[1]);
return -2;
}

record * root;
record * current;
root = newRecord();
current = root;

char quote = NOQUOTE;
int field = 1;
int c;
while((c = fgetc(fp)) != EOF ) {
if(c == '\n' ) {
field = 1;
quote == NOQUOTE;
current->next = newRecord();
current = current->next;
}
else if(c == '\"') {
if(quote == NOQUOTE) {
quote = DOUBLEQUOTE;
}
else if(quote == DOUBLEQUOTE) {
quote = NOQUOTE;
}
}
else if(c == '\'') {
if(quote == NOQUOTE) {
quote = SINGLEQUOTE;
}
else if(quote == SINGLEQUOTE) {
quote = NOQUOTE;
}
}
else if(c == ',' && quote == NOQUOTE) {
field++;
}
else switch(field) {
case NAME:
append(current->name, c);
break;
case DATE:
append(current->date, c);
break;
}

}
fclose(fp);

current = root;
int length = 0;
do {
printf("%s, %s\n", current->name, current->date);
length++;
current = current->next;
} while(current->next->next != NULL);

free(current->next);
current->next = NULL;

deleteList(root);
return 0;
}


And this is my code for the window:


#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

Since this is my first C program I have no idea how to intergrate the two so that my CSV contents are displayed in the window instead of the console.

Can anyone give me some pointers please? Would I literally be able to post my console code into a section of the window code, or does it have to be modified in order to work?

oracleguy
02-27-2009, 12:39 AM
You'll have to modify your WndProc so that during the WM_PAINT event, you write any text you want displayed to the window. How you want your GUI to look is really up to you.

oesxyl
02-27-2009, 05:29 AM
maybe this will not help you but I would prefere another approach since c/c++ part of a gui is very complicated in my opinion.
1. use tcl/tk to build the gui
2. use swig to bind your c/c++ part with tcl/tk

this approach is portable, I use it to develop app on linux which work without problem on windows( and other os's) and is development process is fast and simple(few lines of code and complicated gui stuff is hidden by tcl/tk).
what is wrong with this is that you must learn to use tcl/tk but this is not so hard in my opinion.

http://www.activestate.com/activetcl/tcl8_6/
http://www.swig.org/tutorial.html

of course if you feel more confortable with python you can use it instead of tcl/tk in same way.

best regards