Please help newbie!

Hi,

First let me say thank you in advance to anybody who is willing to take the time to help me out.

I am a VB6 guy from way back trying to update my programming skills. I thought I would try C++. I'm working with Visual C++ 2008.

It's been going pretty well but I have run into a difficulty. It's probably a very simple thing for experienced programmers but C++ is great divergence from VB6 and I'm a stuck.

My goal is to output a simple log file. I thought this would work best via a source (cpp) file as opposed to rewriting it for every form. I have created the .h header file and the .cpp file but can't seem to make it work. I've rewritten it 50 times at this point and google searched and dug through my book but can't seem to figure it out.

I will throw some code out here, it's probably as scrambled as my brain at this point so please consider it a starting point.

Here is my CPPLog.cpp source file: (what do I actually need to #include and where and how do I need to innitialize the string?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "CPPLog.h"
#include <iostream>
#include <string>
using namespace System::IO;
using namespace System;
 
String ^ sLog;
 
void pLogWriter(String ^ sLog){
        StreamWriter^ LogFile = gcnew StreamWriter("LogFile.txt");
        LogFile->WriteLine(sLog);
        LogFile->Close();
        } 



Here is my CPPLog.h header file: (I get an error saying invalid use of void. Plus I read on some web site you can simply use the keyword "string" in the declaration but don't know if this is correct.)

1
2
3
4
5
6
#ifndef CPPLOG_H
#define CPPLOG_H
 
void pLogWriter(string);
 
#endif  



Here is my main form:

1
2
3
#include "CPPLog.h"
 
pLogWriter("Test"); 


Thanks again for your help, I greatly appreciate it.
What you are using is not C++, is a Microsoft only dialect called C++/CLI. Don't be fooled by that. If you want to continue using that language, that's fine, but not many users out there are using it, even Microsoft has removed the language from newer versions of Visual Studio.

Why don't you stick with standard C++ ?
I'm an old VB6 user too, and I write data to log files all the time. Its my main way of coding. In VB6 I'd do something like this...

1
2
3
4
5
Dim fp As Integer
fp=Freefile
Open "Output.txt" For Output As #fp
Print #fp, "iMyValue = " iMyValue
Close #fp 


in C++ (C++ Includes the entire C Standard Library too, so C idioms are fine by me - and oftentimes preferred)...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>

int main()
{
 FILE* fp=NULL;
 int iMyValue = 12345;

 fp=fopen("Output.txt","w");
 if(fp)
 {
    fprintf(fp,"iMyValue = %d\n",iMyValue);
    fclose(fp);
 }

 return 0; 
}
Last edited on
Thanks modoran,

When I couldn't figure it out from my book (there isn't an example that closely aligns with what I'm tryig to do) I started googleing and came across many variations. What you are witnessing is a code snippet I stumbled across. Posting to this forum was my latest attempt to figure it out. I appreciate you pointing that out to me.
Thank you Freddie, I appreciate the help!

I'll give this code a try!
But like Modoran said, I'd strongly consider learning or doing C++ as opposed to CLI. Unless you have some overarching reason to do so.
No overarching reason. I will take, and appreciate, your advice.

Say Ash, here is a template for you I just put together. I did it in VStudio 2008. Tell it to create an EMPTY C++ Win32 Project. I did this quick, but its largely OK. It shows how I do debugging using log text files. Here is Form1.h

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
//Form1.h
#ifndef Form1_h
#define Form1_h

#define dim(x) (sizeof(x) / sizeof(x[0]))

struct WndEventArgs
{
 HWND                         hWnd;
 WPARAM                       wParam;
 LPARAM                       lParam;
 HINSTANCE                    hIns;
};

long fnWndProc_OnCreate       (WndEventArgs& Wea);
long fnWndProc_OnDestroy      (WndEventArgs& Wea);

struct EVENTHANDLER
{
 unsigned int                 iMsg;
 long                         (*fnPtr)(WndEventArgs&);
};

const EVENTHANDLER EventHandler[]=
{
 {WM_CREATE,                  fnWndProc_OnCreate},
 {WM_DESTROY,                 fnWndProc_OnDestroy}
};
#endif 

And here is Main.cpp

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
84
85
86
87
88
89
/*
  Form1 -- Basic Simple Template Program For Win32 Programming With The Windows Api.
*/

//Main.cpp
#include <windows.h>
#include <cstdio>
#include <tchar.h>
#include "Form1.h"
#define  MyDebug
FILE* fp=NULL;


long fnWndProc_OnCreate(WndEventArgs& Wea)         // This is your Form_Load() From VB      
{ 
 #ifdef MyDebug
 fp=fopen("Output.txt","w");
 if(fp)
 {
	fprintf(fp,"Entering fnWndProc_OnCreate()\n");
    fprintf(fp,"  Output.txt Opened In fnWndProc_OnCreate()\n");
	fprintf(fp,"  Wea.hWnd = %d\n",Wea.hWnd);
 }
 #endif
 Wea.hIns=((LPCREATESTRUCT)Wea.lParam)->hInstance;
 #ifdef MyDebug
 if(fp)
    fprintf(fp,"Leaving fnWndProc_OnCreate()\n\n");
 #endif

 return 0;
}


long fnWndProc_OnDestroy(WndEventArgs& Wea)         
{   
 #ifdef MyDebug
 fprintf(fp,"Entering fnWndProc_OnDestroy()\n");
 fprintf(fp,"  Wea.hWnd = %d\n",Wea.hWnd);
 fprintf(fp,"Leaving fnWndProc_OnDestroy()\n");
 fclose(fp);
 #endif
 PostQuitMessage(0);                                
 
 return 0;                                          
}


long __stdcall fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam,LPARAM lParam)
{
 WndEventArgs Wea;                  //This procedure loops through the EVENTHANDER array
                                    //of structs to try to make a match with the msg parameter
 for(unsigned int i=0; i<2; i++)    //of the WndProc.  If a match is made the event handling
 {                                  //procedure is called through a function pointer -
     if(EventHandler[i].iMsg==msg)  //(EventHandler[i].fnPtr).  If no match is found the
     {                              //msg is passed onto DefWindowProc().
        Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
        return (EventHandler[i].fnPtr)(Wea);
     }
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int __stdcall WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=_T("Form1");
 WNDCLASSEX wc={};
 MSG messages;
 HWND hWnd;

 wc.lpszClassName=szClassName;            
 wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);  
 wc.hInstance=hIns;
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; 
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,100,350,300,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}


Note that it shows a program structure you should be familiar with, i.e., event procedures. My fnWndProc_OnCreate() is your Form_Load() from Visual Basic. Also, you can control creation of debug output by just commenting out the symbol MyDebug. The Output.txt file created by that app looks like so ...

1
2
3
4
5
6
7
8
Entering fnWndProc_OnCreate()
  Output.txt Opened In fnWndProc_OnCreate()
  Wea.hWnd = 1705464
Leaving fnWndProc_OnCreate()

Entering fnWndProc_OnDestroy()
  Wea.hWnd = 1705464
Leaving fnWndProc_OnDestroy()
Being handled here too: http://www.cplusplus.com/forum/beginner/138350/

If you must double post at least link to the other one so people don't waste time duplicating advice.
Thank you Freddie, I greatly appreciate it. I look forward to playing with this code!

And I apologize cnoeval, I am new to the forum and miscostrued a comment believing, incorrectly, I needed to post in another forum. Hence the duplication. Admittedly my question was answered more than once and I gained a lot from the resonses. I will only post in one forum in the future. -Thanks
Last edited on
Topic archived. No new replies allowed.