|
put it in InitInstance method of your CWinApp derived class
InitInstance()
{
...
if(IsAdministrator())
{
CMyDialog dlg;
dlg.DoModal();
}
else
{
AfxMessageBox("Your not an admin");
}
return FALSE;
}
|
|
|
|
|
Anonymous wrote:
...but it did not work.
Which tells us nothing. Do you get a compiler, linker, or run-time error? Does an assertion fire? Is an exception thrown? Does the function not produce the desired result? Have you single-stepped through the code to see what specific line fails? Be just a tad more specific.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
I am converting some of our projects from Visual Studio 6 to Visual Studio 2003
I have found that the following code gives different results from these compilers
In VS6 "Josh" is printed. in VS2003 nothing is printed.
If I trace into c_str() I can see that the correct pointer is returned to s. However between the call to TestFn() and printf the destructor of stl::string is called. This is for the temp object created when returning from TestFn(). In VS2003 a '\0' is inserted at the fist element in the buffer by this destructor. In VS6 this does not happen.
In VS2003 If I use the memory viewer I can see that s points to a \'0' followed by 'o' 's' 'h' '\0';
In VS6 the same things points to 'J' 'o' 's' 'h' '\0'
Can anyone explain why this is happening?
#include "stdafx.h"
#include <string>
using namespace std;
string globalString = "Josh";
string TestFn()
{
return globalString;
}
int main(int argc, char* argv[])
{
const char * s = TestFn().c_str();
printf(s);
return 0;
}
|
|
|
|
|
Your code is buggy.
TestFn is returning a string object. In your main routine you are getting the const char * of the returned string. Then the string is going out of scope and the memory is being deleted. Finally you do a printf on a pointer that now points to trash.
It worked on VC6 out of luck.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I agree the code is not the best.
Tim Smith wrote:
Then the string is going out of scope and the memory is being deleted.
it seems like in VS6 the temp object created when TestFn() returns goes out of scope at the end of main(), and in VS2003 it goes out of scope after the call to TestFn() but before the return statement
|
|
|
|
|
Josh Gray wrote:
I agree the code is not the best.
I don't think that is the point. The code accesses a memory location of a temporary and then, later, it uses that memory in an instruction. That violates the principles of temporary objects. If you have some compiler or circumstance that causes it to not fail that is pure coincidence and does not change the fact that it is just wrong.
Hate is not a family value.
-pete
|
|
|
|
|
Maybe some scope semantics changed between compiler versions? Dunno
What happens if you try this though:
int main(int argc, char* argv[])
{
//const char * s = TestFn().c_str();
string s = TestFn();
printf(s.c_str());
return 0;
}
|
|
|
|
|
that works and so does this
int main(int argc, char* argv[])
{
//const char * s = TestFn().c_str();
CString s = TestFn();
printf(s);
return 0;
}
this is because the constructor of string and CString copyies the data before the destructor of the temp string object deletes its copy
|
|
|
|
|
This is the correct syntax. You must make a copy of any temp object you receive from a function call if you want to use it in this way (after the return value is evaluated). The original code worked on VC6 because VC6 was not following the standard.
John
|
|
|
|
|
Thanks to everyone who replied.
That explanation sounds reasonable. I plan to change all these methods in our code to return a string reference and see what happens. Fortunatly the 100's of methods that return a string are generated at build time so I can just change the template, rebuild and see what happens.
|
|
|
|
|
Just don't return a reference to a local stack variable. That will cause the same type of bugs.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Yeah I know. The actual problem I am attempting to resolve is 'getter' functions that return a member variable. If i change it to a reference I should be able to use the string while the object remains in scope
|
|
|
|
|
Hi i am trying to set up a tab control in an mfc dialog based application however i have been unable to find just any sort of article or anything on how to just set one up and use it can someone either point me to a good article thatll show me how to set up the panels and move through them or at least give me a brief run down of the functions that are needed in setting it up
Thanks
Tyrus
|
|
|
|
|
|
I need to add a string resource manaully to the .rc2 file in an MFC project, so did this (copying from the syntax in .rc file):
STRINGTABLE
BEGIN
IDS_THESTRING "really long string here.."
END
and with a define inside resource.h:
#define IDS_THESTRING 101
This compiles but the strings defined in the normal .rc file (that are edited through the string table editor in VS) are no longer valid, any attempt to reference them fails.
Is this the correct syntax for manually adding a string resource?
|
|
|
|
|
This is the correct syntax. You just have to make sure two strings don't have the same ID.
John
|
|
|
|
|
I think in your resource.h there is a counter of different controls. You have to set it up by one. (This very maybe could be your problem.)
the counters are round about the last lines of the file.
good luck.
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
I'm a newbie and have an OCX written in VB (from vendor) which I'd like to use with a VC++ application. Is it possible to use the OCX w/ out a form/dialog, and if so what needs to be done outside of importing it to the project?
Newbie1
|
|
|
|
|
Usualy when i validate data on the OnOK of a CDialog , i can "return", and the OnOK is simply canceled, the dialog remains open.
I whant to make the same thing with a property page:
Needed result:
- Property sheet with multiple pages.
- Validate data on the OnOK of the pages and prevente property sheet closing if necessary
Problem:
- When i press OK button,and "return" , i can "cancel" the OnOK of that page only
- the OnOK was fired and the property sheet closes.
does any one knows how to do this? (i have searched the forums with no results)
thanks.
|
|
|
|
|
Override OnApply() (called when the Apply or OK buttons are pressed) and return TRUE to continue, or FALSE to abort the operation.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Im very new to the boost library, and have just started to intergrate the shared_ptr smart pointer into one of my applications. I am storing a lot of pointers (to objects) in a std::vector.
typedef boost::shared_ptr<cmyclass> my_shared_ptr;
typedef std::vector<my_shared_ptr> my_shared_ptr_vector;
What i need to do is to additionally store a reference to the same objects
(in the vector) in a CListCtrl (MFC). The CListCtrl allows you to associate
a pointer with each item (row) in the list control.
Before i used shared_ptr i used to add the data as below:
CMyClass pItem = new CMyClass();
my_vector->push_back(pItem);
CListCtrl::SetItemData->SetItemData(iItemNumber, (DWORD)pItem);
Now using shared_ptr (see below) i get a compiler error saying that it cannot convert from my_shared_ptr to DWORD.
my_shared_ptr myPtr (new CMyClass();
my_shared_ptr_vector->push_back(myPtr);
CListCtrl::SetItemData->SetItemData(iItemNumber, reinterpret_cast<dword_ptr>(myPtr));
My second question is if i was able to add the item data as ...
CListCtrl::SetItemData->SetItemData(iItemNumber, reinterpret_cast<dword_ptr>(myPtr));
...would the internal reference counter be incremented in shared_ptr?
My vector may be destroyed before my CListCtrl and I dont want the item data to be deleted until both are out of scope.
Thanks
Andy
|
|
|
|
|
See if this helps.
CListCtrl::SetItemData->SetItemData(iItemNumber, (DWORD)(void*)myPtr.get());
You can use reinterpret_cast twice if you like.
Hate is not a family value.
-pete
|
|
|
|
|
Thanks for your suggestion.
Does get() increment the internal reference count in shared_ptr?
From what ive read it doesnt.
|
|
|
|
|
how can i copy a complete partition into a back up file
if there is any API functions please send me or tell me the way i can make it.
|
|
|
|
|
Hello,
I have created an MFC application and have added Printing and Print Preview capability. The only problem i have is that the "Prev Page" and "Zoom Out" buttons on the Print Preview toolbar are always disabled. The "Two Pages" button and "Next Page" are enabled and work correctly.
For the Prev page button, I am setting the CPrintInfo::SetMaxPage() and CPrintInfo::SetMinPage() and CPrintInfo::m_nCurPage values in OnPreparePrinting() but this doesnt seem to make a difference. Also, shouldnt the Zoom Out button be enabled once ive clicked Zoom In???
Is their anything else that i can try which will enable these button?
Thanks
Andy
|
|
|
|