|
1. initialize your variables !!!!!!
CString userInput<font color=red> = ""</font>;
CString device<font color=red> = ""</font>,
cmd<font color=red> = ""</font>,
input1<font color=red> = ""</font>,
input2<font color=red> = ""</font>;
int i<font color=red> = ""</font>;
2. use the CString::GetBuffer() method to allow a C method to write in your CString...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: CString userInput = "";
CString device = "",
Doing that with CString objects does nothing but waste time. They initialize to an empty state and do not normally need to be initialized like that. If you do, you end up basically doing a copy operation to copy an empty string.
When you use GetBuffer(...) to write into CString object, remember to use ReleaseBuffer(...) to ensure that the CString correctly handles the changes.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: Doing that with CString objects does nothing but waste time
waste time ????
hey, this calls the copy constructor, so how can it loose time.
moreover, it's much clean to do so, ,even is the default constructor already creates an empty CString.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: waste time ????
Yes.
The default constuctor (in VC++ 6.0 at least, the ATL version does stuff a bit different) does something more than just creating an empty CString . There is something called afxEmptyString that is used as the shared reference to an empty string.
Normal construction basically just sets the internal data object pointer to the shared empty string's data object.
toxcct wrote: moreover, it's much clean to do so, ,even is the default constructor already creates an empty CString.
Initializing the object as in your example does not call the copy constructor, and would even be more ineffecient if it did because it would have to create a temporary CString object, initialize it, count the characters in the target string (zero in this case), and if a non-zero count, dynamically allocate memory for the string for it and assign it into the temporary object, and then call the copy constructor using the temporary object. Again, it would "waste time".
Initialzing with a static string causes a overloaded constructor to be used (an optimization to eliminate the need to create a temporary as shown above). This constructor initializes just like the default constructor, counts characters in the target string, and allocates memory and copies the string over if necessary.
All of these things take time. When time is taken for no good reason, it is wasted time. Hence, it wastes time. I hope this clears things up for you.
I understand why some people are ignorant about this - not everyone actually takes a look at what goes on behind the scenes. Having a look at the implementation of CString::CString(LPCTSTR lpsz) and CString::CString() will clear things up.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Just FYI:
Initialization (or lack thereof) had nothing to do with the original poster's problem.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
FYMNI, my post was a reply to someone other than the OP. My post addressed the incorrect parts of the provided solutions, as well as provided some background that will hopefully get others to learn more and thus become better developers.
You see, some of us are capable of learning something even if provided out of context, and some... as recently demonstrated... simply are not.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: FYMNI, my post was a reply to someone other than the OP. My post addressed the incorrect parts of the provided solutions, as well as provided some background that will hopefully get others to learn more and thus become better developers.
You see, some of us are capable of learning something even if provided out of context, and some... as recently demonstrated... simply are not.
I wasn't trying to discount what you were saying (nor the discussion) ... just mentioning that the original problem had nothing to do with this line of discussion.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: I wasn't trying to discount what you were saying (nor the discussion) ... just mentioning that the original problem had nothing to do with this line of discussion.
Understood... Tone lowered...
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
You are going to have a couple problems here:
1) When using scanf-derivatives, the %s directive doesn't work quite like you would expect it to. You will probably want to use the %[] directives with the appropriate delimiter, or better yet, write a simple split method that takes a delimiter and splits a CString into a CStringArray. This part of the error will just give you incorrect data.
2) The scanf-methods (as with all C methods) were not designed to work with class objects. You have to give it a pointer to a character buffer. You can either declare a set of char[]'s, or use the GetBuffer/ReleaseBuffer calls appropriately:
CString userInput = _T("");<br />
char device[20] = {0};<br />
char cmd[20] = {0};<br />
char input1[20] = {0};<br />
char input2[20] = {0};<br />
int i = 0;<br />
<br />
i = _stscanf(userInput,_T("%[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9]"), device, cmd, input1, input2);
or, alternatively:
CString userInput = _T("");<br />
CString device = _T("");<br />
CString cmd = _T("");<br />
CString input1 = _T("");<br />
CString input2 = _T("");<br />
int i = 0;<br />
<br />
i = _stscanf(userInput,_T("%[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9]"), device.GetBuffer(256), cmd.GetBuffer(256), input1.GetBuffer(256), input2.GetBuffer(256));<br />
<br />
device.ReleaseBuffer();<br />
cmd.ReleaseBuffer();<br />
input1.ReleaseBuffer();<br />
input2.ReleaseBuffer();<br />
Passing in the address of a CString object will lead to a stack-corruption and hence your Access Violation.
The old-style C methods (printf, scanf, etc) have been deprecated in VS.Net 2005 (can't remember if they were also in 2003). There is a link in the MSDN documentation that describes their replacements. You can either use those replacements (and avoid the warnings), pragma out the deprecated warnings, or write a split function (which is what it looks like you really want in this case anyway).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
God I love you guys!
Kitty5
|
|
|
|
|
I created my own CPropertySheet which is CMyPropertySheet and I created my own CPropertyPage, CMyPropertyPage. CMyPropertyPage is a superclass for my pages. It provides access to CMyPropertySheet which is run from my App like a dialog program runs it's dialog. So CMyPropertySheet holds my state information for filling out the pages as they gather information from the user. I am misunderstanding my compiler because I have the #ifndef __mypage_h__ ... #endif markup so that my files are only included once and still if I add #include "MyPropertySheet.h" to MyPropertyPage.h my project recurses the includes until it gives up. Naturally (to me at least), the sheet knows of the pages so that the sheet can make itself, and the page knows of the sheet so it can store info.
I tried prototyping the classes but I must be doing something improper. An example of a working relationship like this would save me two Aspirins and a morning.
|
|
|
|
|
I ran in to a saomewhat similar situation and to resolve it I removed the include of "MyPropertySheet.h" from 'MyPropertyPage.h" and use a forward reference for CMyPropertySheet instead. Are you able to use a forward reference instead of including the file?
|
|
|
|
|
Thanks, you set me on the path to a buildable project. For anyone who runs into a similar situation, here's what I did.
In CMyPropertyPage's header file, I forward reference "class CMyPropertySheet;", and in CMyPropertyPage's source file, I include CMyPropertySheet (MyPropertySheet.h). Be careful, when forward referencing you cannot use any operations on CMyPropertySheet in the header file. That was throwing me off.
Case closed.
|
|
|
|
|
I'm trying to create a .dll using ATL, which I want use in C#.
Can anybody please guide me to create a simple method / function which takes string in input parameters and returns the same(string)..
I tried a lot but always faced problem of returing string back to calling function in C#.
I did the same with C++ class library successfully but when it comes to ATL its not the same.
As I'm using other SDK API's in creation of .dll and I can't go with Class Library its having problem with "CLR and MTd". Which can't go together. Finally I decided to stick to ATL.
Now please guide me to create the methods which I can use in C# to get the string value.
Arun
|
|
|
|
|
Write a C++ program to implement this pointer object
Define Class customer containing
Code , Name , Billtotal and member function
Accept()
print()
In the main function declare two or multiple object of the above class
and find out which object contains highest billtotal and print that object
at end , use function prototype given below
customer Highbilltotal( customer &)
Implement object passing as referene and returning object
|
|
|
|
|
|
joyalfernandes wrote: Write a C++ program to implement this pointer object
1. i never talked to you that bad, so change your language please.
2. here at codeproject, people aren't paid for their help, so no one owe you anything.
3. Homework are bad seen here. if you have some homework to do, it's because you have to learn how things are done, so don't ask people to do it for you, this is child thinking.
Think about your problem first, and only then, ask for specific questions if you can do it yourself.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
|
Ya man, the people here are very helpful if you have a specific problem... why not try and tackle it first and come back if you run into major obstacles.
Btw this is pretty impressive
Birthday Tuesday 20th October, 1987
Location India
Occupation Software Components, Web design, Science and Engineering, Systems engineering
Interests C++, WTL, Win32, Managed C++, C#, ASP, ASP.NET, VB6, VB.NET, J#, Javascript, Perl, SysAdmin, Database
|
|
|
|
|
|
|
This will help :
HBRUSH CtestMFCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor== CTLCOLOR_EDIT )
hbr=CreateSolidBrush(RGB(0,100,0));
return hbr;
}
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
THANKS A LOT
Please mail me
|
|
|
|
|
<imho>
We should use the reflect message =WM_CTLCOLOR on CMyEdit more OOPs Oriented.
Regards,
FarPointer
Blog:http://farpointer.blogspot.com/
|
|
|
|
|
If you are doing this in OnCtlColor handler for WM_CTLCOLOR notification, and you want to affect the colours of all of edit boxes, then you can try the last argument of OnCtlColor , which describes the type of control:
HBRUSH CMyDialog::OnCtlColor( CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
if(nCtlColor == CTLCOLOR_EDIT)
{
. . .
}
else
{
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}
Hope it helps.
|
|
|
|
|