|
He Guys,
I have a problem on VC ++ pipes.
In case of a pipe there are two handles, one is the read handle and a write handle. When I create a pipe I send this read handle to the component which wants to read from my pipe. And on the opposite end I write to the Pipe which will be read by the read handle.
And the communication that happens over this pipe of mine will be to write the data in the pipe when ever the read guys asks for data. What is happening now is whether or not that guy is reading Iam just writing to the pipe, I want to figure out a way in which I know when the pipe wants data so that I can write back to the pipe.
This problem can be phrased to something like a DOS console. In this case the console does not take input from the keyboard until the application does not call a scanf.
I cannot change the pipe to make it a named pipe and use the client server way of communication...that could had been easier. Since the other component which uses my read handle is not mine belongs to a different group.
Please let me know if there is a way to figure out if my read handle of the piep is getting utilized or not
Thanks
|
|
|
|
|
This is what MSDN says in the documentation for CreatePIpe() :
"When a process uses WriteFile to write to an anonymous pipe, the write operation is not completed until all bytes are written. If the pipe buffer is full before all bytes are written, WriteFile does not return until another process or thread uses ReadFile to make more buffer space available."
So, you can't indefinitely go on writing to your pipe because the WriteFile() function wont return until someone at the other end uses ReadFile() to read the data out...
Bikram Singh
|
|
|
|
|
Hey, everyone, I have a problem with overloading operator+, here is the code:
CStr operator+ (const char* val)<br />
{<br />
CStr temp;<br />
char *pBuf=new char[strlen(val)+strlen(buffer)+1];<br />
strcpy(pBuf,buffer);<br />
strcat(pBuf,val);<br />
temp.buffer=pBuf;<br />
return temp;<br />
}
Here buffer is a member-variable which holds string data. I don't understand why, but this method never returns concatenated strings, it returns some garbage instead. Any ideas ?
|
|
|
|
|
Does CStr have a correct copy constructor?
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen
|
|
|
|
|
|
Here's my code:
class CStr<br />
{<br />
private:<br />
char *buffer;<br />
public:<br />
CStr(void)<br />
{<br />
buffer=NULL;<br />
}<br />
CStr(const char *init)<br />
{<br />
if (init!=NULL)<br />
{<br />
buffer = new char[strlen(init)+1];<br />
strcpy(buffer,init);<br />
}<br />
else buffer=NULL;<br />
}<br />
~CStr(void)<br />
{<br />
Empty();<br />
}<br />
void Empty()<br />
{<br />
delete[] buffer;<br />
}<br />
CStr& operator= (const char* val)<br />
{<br />
Empty();<br />
<br />
if (val!=NULL)<br />
{<br />
buffer = new char[strlen(val)+1];<br />
strcpy(buffer,val);<br />
}<br />
else buffer=NULL;<br />
<br />
return *this;<br />
}<br />
CStr operator+ (const char* val)<br />
{<br />
CStr temp;<br />
char *pBuf=new char[strlen(val)+strlen(buffer)+1];<br />
strcpy(pBuf,buffer);<br />
strcat(pBuf,val);<br />
temp.buffer=pBuf;<br />
return temp;<br />
}<br />
CStr& operator= (const CStr val)<br />
{<br />
if (this == &val) return *this;<br />
<br />
Empty();<br />
<br />
buffer = new char[strlen(val.buffer)+1];<br />
strcpy(this->buffer,val.buffer);<br />
<br />
return *this;<br />
}<br />
};
I use this class like this:
CStr str("Test");<br />
printf("%s",str+"ing");
But, instead of printing Testing it returns lot's of garbage.
I ran the debugger, it shows me that operator+ returns bad object
What shall I do ???
|
|
|
|
|
So my first idea was right - the class has no corfrect copy CTor
OK, here's what happens:
when you do return temp , the compiler returns a copy of temp , and calls the destructor of temp (which deletes temp.buffer).
For the copy, C++ looks for a copy constructor with the signature CStr(CStr const &) . Since you didn't specify a copy constructor, C++ generates a default one, which makes a "flat" copy (the .buffer member of the copy is pointing to the same buffer as temp.buffer , which is deleted in temp's destructor)
Provide a correct copy constructoir (similar to CStr(char const *) , but using a CStr const & as argument), it will work.
---------------------
Some more notes:
The behavior of a CStr object passed to printf() (or any other variable argument function) is not defined by the C++ standard. It works on Microsoft Visual C++ (and some MFC code relies on this), but it is not portable.
You might want to get a C++ book to learn a bit more about what is necessary to create a "correctly behaving" class, which elements are created by default, etc.
Writing a CStr class is an interesting practice, and might be useful if you working on a "no dependency mini exe" Project. However, there are so many reliable string class implementations out there that it rarely makes sense.
First there is std::string. Yes, it's ugly, compiler errors (if you do something wrong) are indecipherable, and the VC6 MSDN documentation for it is useless - but it works, it's portable, it's the standard.
Second, MFC's "less scientific" but widely successful CString class is available outside of MFC projects as well (on VC7, it's part of the ATL/MFC libraries, on VC6 you can use the semi-official WTL::CString clone, or any other CString clone you find on the internet)
good luck!
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen
|
|
|
|
|
Oh, thank's a lot, you helped a lot.
I'm writing an interpreter and I needed a specialized string class of my own, and that's the first time I hear about COPY constructor, I programmed a lot, but never heard about it, thanks a lot again )) ...
|
|
|
|
|
I have two view classes in my MFC VC6 project.
If I add a menu resource, it is active for the
correct view. But, if I add an accelerator
key under either view resource, the message
is not caught. The IDE puts in all the code,
but never response to the message. To get
an Accelerator key to work, I have to put
it under IDR_MAINFRAME. I can live with it.
But I am curious if it is something I have done,
a standard response or if I have to do something
to get the message passed to the proper view
code.
WedgeSoft
|
|
|
|
|
Have a dialog(Dialog1) with a combobox control(ID:IDC_COMBO and variables:m_combo) initilaise with some values.
Have a CFormView with a edit control(IDC_EDIT1).
How do you passed the value in the combobox to the edit?
Thanks
S1189216G
|
|
|
|
|
you told that the combobox variable is m_combo.... but what type is it? is it a CComboBox? an int?
Remember that classwizard let you add the DDX variable mapping to be a control or a value.
If it's a value or a control, you will need to get the string of the combobox using
GetDlgItem(IDC_COMBO)->GetWindowText(sValue);
Also if it is a value:
UpdateData(TRUE);
((CComboBox *)GetDlgItem(IDC_COMBO))->GetLBText(m_combo, sValue);
Cheers
Jaime
|
|
|
|
|
hello everyone..
currently i'm doing a mini assignment using vc. it's very simple. the input will be a string, then i have to convert it into hex and then show the hex version with a click of a button. so roughly i will have 2 edit boxes and a button. i have this code in a non MFC environment:
int main(void)
{
char input[n];
int result, i=0;
cout<<"pls key in your name"<
|
|
|
|
|
CString GetBuffer(int stringLen) is what you want
(By the by, you can use int len = str.GetLength()
to get the length of the string for getting
the buffer.)
Straight from MSDN CD:
// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" ); // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
Once you have a pointer to the buffer you can
parce it; but I would advise against changing it.
You can't print your output using cout in MFC.
Again use a CString (the member format)
Something like :
CString str
str.Format("The hex value is: %x\n", char);
Then use AFxMessageBox(str)
There are a million ways...
Hope the above helps
WedgeSoft
|
|
|
|
|
alrite.. i'll give it a try.. thanx..
~wilD cat~
|
|
|
|
|
hi,
i need to do search and remove content in a CMapStringtoPtr.
currently, i used a loop to iternate thro' the CMap, do search and RemoveKey(). however, my app. crash in the loop due to the RemoveKey() may change Map structure.
below is my code. anyone method to solve my problem? i can figure out do search in a loop, store the key and remove in another loop. but, i want to know any method to all of them in a loop?
thanks,
jim
POSITION pos, pos2;
pos = m_ActiveReleaseMap.GetStartPosition();
int listlen = m_ActiveReleaseMap.GetCount();
if (listlen == 0) {return;}
do
{
pos2 = pos;
m_ActiveReleaseMap.GetNextAssoc(pos, OrderNo, (void *&) ptr);
// do checking. if match, remove.
m_ActiveReleaseMap.RemoveKey();
}
while( pos != NULL) ;
|
|
|
|
|
I wrote a response, then re-read your question.
In the second half of your question you say
you have had success by storing and deleting
in a second loop -- that way my solution. my
response was a waste of your time. Sorry. I
have had the same problem and am of the belief
you can not remove an item within the loop. You
must do it in a second loop. But (more waste of
your time) I can't say way. I'll watch for other
answers to your question. perhaps someone will
post a definative reason.
WedgeSoft
|
|
|
|
|
In the loop, try putting a
pos = m_ActiveReleaseMap.GetStartPosition();
before this:
m_ActiveReleaseMap.GetNextAssoc(pos, OrderNo, (void *&) ptr);
Bikram Singh
|
|
|
|
|
actually, i did this b4 but seems still not ok!
for( pos = m_ActiveReleaseMap.GetStartPosition(); pos != NULL; )
m_ActiveReleaseMap.GetNextAssoc(pos, OrderNo, (void *&) ptr);
// search and removal
}
|
|
|
|
|
Hi !
I use multi-thread in my software. I must declare the ThreadProcedure static if I don't want the following error message:
error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
The problem is that I call many functions and variables which are not static. Do I need to declare everything static or I don't understand the goal of the STATIC keyword in my declaration of my ThreadProcedure ?
Thank you to help me
Note: I use VC++ 6.0
|
|
|
|
|
Danny Gilbert wrote:
I don't understand the goal of the STATIC keyword in my declaration of my ThreadProcedure
I assume "ThreadProcedure" is a class member?
If you declare "ThreadProcedure" static, the function essentially "does not belong" to any single instance of the class. So, to use this method, you need to pass the instance's pointer (eg. via ThreadProcedure(this);), for the ThreadProcedure to be useful at all.
Other than the passed-in pointer, anything that ThreadProcedure uses from the class it is declared in, must also be defined as static.
Bikram Singh
|
|
|
|
|
does anyone have code for a simple Non-MFC ftp server, I am doing a project and I would like help. Thanks for reading this.
-Ryan M.
|
|
|
|
|
How do I load a variable inside an edit box?
I have an edit box stated below, its called "OnCash", It's ID is "IDC_CASH".
Source:
----------------------------------------------
void CTycoonDlg::OnCash()
{
//$cash(variable)
}
----------------------------------------------
I want it to say in the editbox that is read only, $(cashvariable)
so it would look like this inside the editbox:
$100000 (default value for the cash, unsigned long)
Can anyone help me?
|
|
|
|
|
You need to create a variable for the value of the edit box
do this by going to the resource viewer right click on the dialog select "Class Wizard"
click on the Member Bariables tab
select IDC_CASH and click Add Variable
name it something like m_Cash
then to set the text in it do this
m_Cash = "$1000000";
UpdateData( TRUE ); // sets the edit boxes text to m_Cash's value
to get the edit boxes text and store it in m_Cash call UpdateData( FALSE );
|
|
|
|
|
Problem is:
I already made a value, and It's being read all across the program, im just trying to sorta make an active display in the MFC application on how much money I have... It's an edit box, displaying how much cash you have.
This is part of the source:
#include "stdafx.h"
#include "Tycoon.h"
#include "TycoonDlg.h"
#include "BUY.h"
#include "SELL.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
unsigned long cash = 100000;
But Class Wizard makes a new variable, and I want to use my old variable.
|
|
|
|
|
just delete the wizard-made variable and use your own. You will need to rename the variable in DDX macros section too.
Jaime
|
|
|
|
|