|
If you're using MFC and your object is derived from CObject you can use CArchive with CMemFile, then save the contents of CMemFile to .ini using CWinApp::[Get|Set]ProfileBinary.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
What you're after is called reflection, the ability to access in runtime to the structure of a class. Efforts devoted to achieve some sort of reflection in C++ are usually very far-fetched (look for OpenC++ and Meta Object Protocol (MOP), for instance). Nevertheless, it is possible that some kind of library exists fith more modest objectives that suits your needs.
Good luck
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
How can I drag icon in CListCtrl ? Just like Windows Desktop!
|
|
|
|
|
See MFCDRAG and maybe LISTHDR samples in MSDN. Maybe it will help.
|
|
|
|
|
Does anyone know how to detect if a item was checked or unchecked in a CTreeCtrl? I took a look at the CTreeCtrl and it seems that there are no messages for this kind of thing.
|
|
|
|
|
Sorry - no special notification for that. You'll have to handle WM_LBUTTONDOWN and use CTreelCtrl::HitTest. If returned flags have TVHT_ONITEMSTATEICON bit set, it was a click on checkbox.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
I have an assignment about writing 2 dimensional arrays class Array2D, but I don't know the syntax for using int *iptr and int **array2D to dynamically allocate memory. The following is my program. The class interface and main driver given as it is. I couldn't really find the error on my class Array2D implementation part. Please help me out.
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
#include <cassert>
#include "PA_02.h"
//Output the array in row & column format (table format)
//Overloaded output operator for class Array2D
ostream &operator<<( ostream &out, const Array2D &obj )
{
for ( int i = 0; i < obj.rows; i++ )
{
for ( int j = 0; j < obj.cols; j++ )
out <<setw(10)<< obj.array2d[i][i];
="" out="" <<="" "\n"="" <<endl;
="" }
=""
="" return="" out;
="" input="" the="" entire="" array="" contents
="" overloaded="" operator="" for="" class="" array2d
="" istream="" &operator="">>( istream &in, Array2D &obj )
{
cout<<"Enter array values, one row at a time:\n"<<endl;
for="" (="" int="" i="0;" <="" obj.rows;="" i++="" )
="" {
="" cout<<"enter="" column="" values="" row="" "<<="" <<":\n"<<endl;
="" j="0;" obj.cols;="" j++="" cout="" <<"\nenter="" value="" ("="" <<="" <<"="" ,"="" <<")="" :";=""
="" in="">> obj.array2D[i][j];;
}
}
cout<<"\n\nInput complete\n"<<endl;
return="" in;
="" }
="" initialization="" and="" termination="" methods
=""
="" dynamically="" create="" an="" initialized="" array,="" contents="" will="" be="" to="" value
="" array2d::array2d(="" int="" r,="" c,="" value="" ):rows(r),="" cols(c)
="" {
="" i,="" j;="" iptr="new" int[="" r="" ];
="" assert(="" !="0);
" array2d="&iptr;
" for(="" i="0;" i<r;="" i++="" )
="" j="0;" j<c;="" j++="" array2d[i]="new" int[j];
="" );
="" }
="" }=""
="" initialize="" array="" equals="" 0
="" for="" (="" <="" r;="" c;="" array2d[i][j]="value;
" }
="" the="" copy="" constructor
="" const="" &rhs="" )="" :="" rows(="" rhs.rows),=""
cols(rhs.cols)
="" j;
="" rows="" rows;="" j<cols;="" rhs="" into="" object
="" cols;="" assignment="" operator="" (performs="" assignment)
="" &array2d::operator="(" j,="" i;
="" if="" check="" self="" assignment
="" delete="" []="" iptr;
="" cols="" array2d;
="" i<rows;="" *this;
="" clean="" up="" (e.g.="" allocated="" memory)
="" array2d::~array2d(="" void="" cout<<"\ndestructor="" called"<<endl;
="" =set="" get="" methods.="" explicitly="" whether="" index="" is="" within="" bounds="" of="" array
="" set="" item="" in="" at="" location(="" row,="" col="" ).
="" returns="" false="" out-of-bounds,="" else="" true
="" bool="" array2d::set(="" &new_item,="" if(="" row="">0 && col>0 )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="new_item;
" }
="" return="" true;
="" else
="" false;
=""
="" }
="" get="" an="" item="" in="" the="" array="" at="" location="" (="" row,="" col="" ).
="" returns="" false="" if="" index="" is="" out-of-bounds,="" else="" true.
="" bool="" array2d::get(="" &item,="" )="" const
="" if(="" row="">0 && row<=rows )
{
if( col>0 && col<=cols )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="item;
"
="" }
="" return="" true;
="" else
="" false;
="" }
="" returns="" the="" length="" of="" array
="" array2d::size(="" void="" )="" const
="" (="" sizeof(="" array2d[rows][cols]="" );
="" number="" rows
="" array2d::getrow(="" rows;
="" columns
="" array2d::getcol(="" cols;
="" compare="" this="" array="" with="" <rhs=""> for equality. Two arrays are equal if
//their
//rows, and cols are equal and all the elements are equal
bool Array2D::operator==( const Array2D &rhs ) const
{
if ( rows != rhs.rows && cols != rhs.cols )
return false; //arrays of different size
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] != rhs.array2D[i][j] )
return false; //arrays are not equal
}
return true;
}
//Compare this array with <rhs> for inequality
bool Array2D::operator!=( const Array2D &rhs ) const
{
if ( rows == rhs.rows && cols == rhs.cols )
return false;
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] == rhs.array2D[i][j] )
return false;
}
return true;
}
//Overload () to allow use of ( i, j ) notation ( leftvalue )
int &Array2D::operator()( int i, int j )
{
if ( i >= rows || j >= cols )
cout<<"out of bounds"<<endl;
="" return="" array2d[i][j];
="" }
="" overload="" ()="" to="" allow="" use="" of="" (="" i,="" j="" )="" notation="" rightvalue="" )
="" const="" int="" &array2d::operator()(="" const
="" {
="" if="" i="">= rows || j >= cols )
cout<<"out of bounds"<<endl;
return="" array2d[i][j];
="" }
="" utility="" functions
="" returns="" true="" if="" <index=""> is within range, else false
bool Array2D::in_range( int r, int c ) const
{
if ( r < 0 || r >= rows && c < 0 || c >= cols )
return false;
else
return true;
}
//Perform dynamic memory allocation
void Array2D::allocate( void )
{
rows = ( rows>0 ? rows );
cols = ( cols>0 ? cols );
iptr = new int[ rows ];
assert( iptr != 0 );
array2D = &iptr;
for( int i=0; i<rows; i++="" )
="" {
="" for(="" int="" j="0;" j<cols;="" j++="" array2d[i]="new" int[="" ];
="" assert(="" !="0" );
="" }
=""
="" }
--------------------------------------------------------------------------------
#include="" <iostream="">#include <cassert>
#include "PA_02.h"
#include "a2D.cpp"
using namespace std;
int main()
{
Array2D a1( 2, 3, 5 );
Array2D a2 = a1;
Array2D a3, a5( 4, 2, 3 );
Array2D a4( a3 );
//Test the << operator
cout<<"\nPrinting a1 ...\n"<<a1;
cout<<"\nprinting="" a2="" ...\n"<<a2;
="" a3="" ...\n"<<a3;
="" a4="" ...\n"<<a4;
="" a5="" ...\n"<<a5;
="" test="" the="">> operator
cin>>a3;
cout<<"\nPrinting a3 ...\n"<
|
|
|
|
|
You could save a lot of time by replacing the element of your matrix in a vector : let say you have a mxn matrix you just allocate a vector of size mn.
you can access the elements easily e.g. array[i*m+j]
Easier, faster, cleaner.
ps: by the way you have a bug in your << operator. [i][i] should be [i][j]
|
|
|
|
|
I am running a multiproject application and in one of the projects, there is a Process created using CreateProcess. This process uses an executable created in another project of this same application to perform certain tasks.
I am under the impression that if I add "DEBUG_PROCESS" as one of the options under CreateProcess, I should be able to debug into the Process (i.e. Go F11 and right into the actual file).
However, what I seem to get is a program that sort of hangs when it gets to WaitForSingleObject statement.
Any ideas of how to debug into a Process? What am I doing wrong?
thanks
|
|
|
|
|
Hello, the codegurus around the world.;)
I have the same problem to debug some project.
In this case, I wrote the following.
#ifndef _DEBUG
WaitForSingleObject (...);
#endif
This approach will not solve the basic problem, but
we can debug the different EXE created by CreateProcess.
So, in the debug mode, we can teminate the main exe process by debug menu,
otherwise, we will get some messy situation since we never call WaitForSingleObject.
Have a nice day!
-Masaaki Onishi-
|
|
|
|
|
I am using MFC for a XP program, and have various controls on the dialog box.
When I add my .manifest file, all the controls are displayed as they should be (flat, hot tracking etc), but if I add the style "ES_PASSWORD" to an Edit box, I don't get the '*' character or the XP black circle, instead I get a small black rectangle.
Help.
Joe.
|
|
|
|
|
Hello, the codegurus around the world.;)
I think that this is normal since MFST seems to change the password
word from * to the black circle on XP.
Windows application depends on the core DLL of Windows, so
as long as we use DLL of XP, I think that we can't change this.
Have a nice day!
-Masaaki Onishi-
|
|
|
|
|
A while back I posted this question here but no one replied. Let me try again...
I have a console app which prompts the user for a user id and password. The problem is that I don't know how to prevent the password from being displayed as the user types it. I'm using good old cin to retrieve it, like this:
cout << "Password: ";
char szPassword[80];
cin.getline(szPassword, sizeof(szPassword));
Is there a way to hide what the user types or perhaps mask it somehow?
Thanks,
Alvaro
|
|
|
|
|
char c = 0;
while (c!=' ')
{
if(kbhit())
{
c=getch();
printf("*");
}
}
-c
POKE 808,234
|
|
|
|
|
Thanks for the quick answer Chris. Now, to throw a monkey wrench into the solution: it has to compile and run on both Windows and UNIX. Is there an equivalent to kbhit on UNIX?
Thanks again,
Alvaro
|
|
|
|
|
Alvaro
In windows you can do this :-
while((c=_getch())!='\n')
{
printf("*");
s[i++]=c;
}
s[i]='\0';
For Linux/Unix you'll need to write your own _getch() function.
You can do this with not too much difficulty
(1)First open /dev/tty
(2)use ioctl to change keyboard device parameters [there is some flag to prevent echoing]
(3)Then read one character
(4)use ioctl to set echo back
(5)Now close /dev/tty
I am sorry I couldnt actually put the code here, but if you have access to a unix box you can lookup ioctl and /dev/tty in the man pages and figure out a way.
Regards
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
Unix/Linux does haven't a getch()!
I'd have thought with all those open source programmers they could get round to doing one of the most basic input functions. I'd have though getch() would have been part of the standard c library.
Michael
|
|
|
|
|
Thats cause in Linux they dont take passwords from the keyboard. They put it in a file and put the file in /etc and chown it to root and chmod it to 000
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
Nish,
Thanks for your reply. I got it working on Windows, which by the way requires me to look for '\r' instead of '\n'. Now I'm trying UNIX but I'm lost:
(1)First open /dev/tty how? with fopen?
(2)use ioctl to change keyboard device parameters [there is some flag to prevent echoing] could not find such flag.
(3)Then read one character how? with fgetc?
(4)use ioctl to set echo back again, I don't know how
(5)Now close /dev/tty I imagine with fclose, but then what, repeat for the next character?
Thanks for your help,
Alvaro
|
|
|
|
|
Well, I got it working so that it doesn't show any characters. It's not as pretty as the masked solution, but it's easier and it works. Here's the code for the method, in case someone's interested:
string getPrompt(const char* szPrompt, bool bShow = true)
{
cout << szPrompt << ": ";
char szResponse[80];
if (bShow)
cin.getline(szResponse, sizeof(szResponse));
else
{
#ifdef _WIN32
int iPos = 0;
for (int c = 0; (c = getch()) != '\r'; )
{
if (c == '\b')
{
if (iPos > 0)
iPos--;
}
else if (isprint(c) && iPos < sizeof(szResponse) - 1)
szResponse[iPos++] = c;
}
szResponse[iPos] = 0;
#else
system("stty -echo");
cin.getline(szResponse, sizeof(szResponse));
system("stty echo");
#endif
}
return szResponse;
}
Thanks for your assistance.
Alvaro
|
|
|
|
|
Not within iostream , I think. You'll have to capture keystrokes with _getch() and print out the asterisks yourself (handling backspaces is a little tricky, also).
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
How do i use loops in MFC?
Whenever i use a for loop my MFC program freezes!
thanks
|
|
|
|
|
Could you post your code?
"But, daddy, that was back in the hippie ages..." My twelve year old son - winning the argument.
"Stan, you are an intelligent guy who responds in meaningful ways" Paul Watson 16/10/01
|
|
|
|
|
heres the code
do
{
if ((x % counter == 0) && (y % counter == 0))
{
x = x / counter;
y = y / counter;
}
counter -= 1;
}
while (counter > 0);
|
|
|
|
|
wait.
i figured it out
it was because i was using INT_MAX
how do i use INT_MAX in MFC without crashing or is there an other way?
|
|
|
|