|
Howdy'
I have some very bad code that should crash but I'm having difficulty figuring out why it does not crash (or even give out warnings); it works in VS2003 in release&debug and in VS2008 in debug, not in release.
class Info
{
public:
Info();
Info(std::wstring s1, std::wstring s2);
std::wstring m_string2;
};
struct myStruct
{
std::vector <Info*> m_JobInfoVector;
};
m_MyStruct = (myStruct*)malloc(sizeof (myStruct) );
ZeroMemory(m_MyStruct, sizeof(myStruct));
Info* p = new Info( std::wstring(_T("allo")) );
m_MyStruct->m_JobInfoVector.clear();
Question : What happens to the vector member when the ZeroMemory is called ? and why it does not crash in debug but it does in release ?
Thanks.
Max.
This signature was proudly tested on animals.
|
|
|
|
|
Why worry? The code is erronous, so the result of executing it is undefined. Just replace your malloc with new myStruct ...and don't, just don't ever write over a class with something like ZeroMemory - it just doesn't make sense. You have constructors to initialise structs and classes in C++.
But anyway - the error is in comparison of iterators (which clear does) - in VS2008, Debug mode iterators are different to Release mode iterators unless you specifically enable iterator debugging, which probably has something to do with it.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I know that, but I was wondering why it did not crash; anyway, the code has been fixed.
Thanks.
This signature was proudly tested on animals.
|
|
|
|
|
I think you are assuming that a struct in C++ works the same way as a struct in plain old C, but alas it doesn't... well not really anyway.
When you introduce non-primitive data types into structs in C++ they cease to be simple "data structures" and become basically the same as classes only their members default to public instead of private.
My guess is when you try to overwrite the object you are writing over the beginning of its virtual table which probably contains extra debug information in debug mode. In release mode you are then probably overwriting more important class data.
Remember, the sizeof operator is not designed to get the size of an object or its virtual table.
http://en.wikipedia.org/wiki/Virtual_method_table[^]
Chris Smith
|
|
|
|
|
Thanks, that's probably it; me think i will look at the memory a little closer.
This signature was proudly tested on animals.
|
|
|
|
|
hi,
My program creates files dynamically for that I gave location as desktop.
This code I wrote inside a fr loop, that means how many times this loop continues that many number of files will be created. For this situation how can I mention path that should able to generate any number of files.
sampath-padamatinti
|
|
|
|
|
What are you talking about? You already said that you're creating files on the desktop. What is your query?!
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
char directory[] = "my desktop";
for (int i=0;i<numFiles;++i)
{
char filePath[MAX_PATH];
sprintf(filePath, "%s\\%d.ext", directory, i);
} ?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
In addition to Stuart's reply,
There is an API to create file names, uniquely GetTempFileName
It will give a filename in specified directory with the prefix. It will automatically set a unique file name if you insist to do so. Read the MSDN for detailed information. An example is given here
|
|
|
|
|
It probably would have been better to reply to the OP rather than me
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: It probably would have been better to reply to the OP rather than me
Hello Madhu Nair, I agree with Stuart.
Sorry, couldn't resist.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Rajesh R Subramanian wrote: I agree with Stuart
Of course you do - everyone agrees with me...if they know what's good for them
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I'm not sure if you got the pun. I've modified the previous post to make it clearer.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I've really no idea as to what you are talking about but if I were to hazard a guess, are you wanting to create a different file each iteration of the loop?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi everyone, I got a question about disabling context menu of the web browser and I've searched MSDN, this forum and goole. But seems most of the are either not Dialog Based (most of them use CHtmlView) or not quite same as what I see in my project.
I am using Visual Studio 2005 and Here is my steps of creating the application:
I use MFC application and choose dialog based. Then, in the dialog editor, I add Microsoft Web Browser into the toolbox, and then drag it into the dialog. After that, I right click on that control and choose Add Variable and leave everything default and give it a variable name "myBrowser". Then it automatically creates a explorer.h and explorer.cpp. The explorer.h contains a class declearation of "class CExplorer1 : public CWnd". Then I get stuck at this point. I've tried to declear a message map in explorer.h and find ON_WM_CONTEXTMENU() but seems doesn't work.
From the search of the internet, it seems that I need to implemente IDocHostUIHandler and there I can overload ShowContextMenu() and return S_OK to disable it. But with most of the information I got, seems most of them are for CHtmlView but not CWnd. So, I'd like to ask what I should do to disable the context menu of a Dialog Based Web Browser which inherit CWnd (or my approach is wrong?) and with Visual Studio 2005? (coz I see most of the information are VC++6 and I can't find the same thing in VS2005).
I am new to MFC and these kinds of browser things so my question maybe a stuipd question....
Thanks for any help.
modified on Thursday, July 2, 2009 7:32 PM
|
|
|
|
|
|
thanks, I've found that article too, but I am working with C++...
|
|
|
|
|
Well, a quick Google shows this article[^] - you'll need to fix up the HTML formatting a bit, but it loks like all the code's there.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
how can i convert below code from vbscript to c++
strComputer = "."
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
'Wscript.Echo objGroup.Name
if objGroup.Name ="Administrators" then
For Each objUser in objGroup.Members
Wscript.Echo vbTab & objUser.Name
Next
end if
Next
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
Mogaambo wrote: how can i convert
What do you mean? Are you looking for a tool that will do it or what? Most of the people here know how to write C++ code and that is why they might be able to do the work. What can you do and what do you want from the people in this forum?
|
|
|
|
|
How can i enumerate users
// adminGroup.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"Netapi32.lib")
#include <windows.h>
#include < LMCONS.H>
#include <lm.h>
#include <lmaccess.h>
#define MAX_PREFERRED_LENGTH ((DWORD) -1)
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
NET_API_STATUS ret = 0 ;
LPCWSTR servername = NULL;
LPCWSTR groupname = L"Administrators";
LPBYTE buffer = NULL;
DWORD entriesRead;
DWORD totalEntries;
DWORD resumeHandle =0;
typedef struct _LOCALGROUP_MEMBERS_INFO_1 {
PSID lgrmi1_sid;
SID_NAME_USE lgrmi1_sidusage;
LPWSTR lgrmi1_name;
}LOCALGROUP_MEMBERS_INFO_1, *PLOCALGROUP_MEMBERS_INFO_1, *LPLOCALGROUP_MEMBERS_INFO_1;
//function call
ret=NetLocalGroupGetMembers (NULL, groupname, 1, &buffer, MAX_PREFERRED_LENGTH, &entriesRead, &totalEntries, &resumeHandle);
for(int i = 0;i< entriesRead ;i++)
{
//here i wan to enumerate but how
}
int n=0;
return 0;
}
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
I've never used that API but based on the documentation my first guess would be that if entriesRead is more than 1 the buffer will contain an array of them so you would iterate through the array, something like:
PLOCALGROUP_MEMBERS_INFO_1 pData = (PLOCALGROUP_MEMBERS_INFO_1)buffer;
for(int i = 0;i< entriesRead ;i++, pData++)
{
pData->lgrmi1_sid;
}
Of course I would have to verify that by debugging since I've never worked with it.
BTW: Don't forget to free the buffer
"bufptr [out]
Pointer to the address that receives the return information structure. The format of this data depends on the value of the level parameter. This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA."
|
|
|
|
|
Here what i get after a lot of googling or say binging
LPLOCALGROUP_MEMBERS_INFO_1 pstMembersInfo = 0;
DWORD entriesread = 0;
DWORD totalentries = 0;
if( 0 != NetLocalGroupGetMembers( NULL, _T("Administrators"), 1, (LPBYTE*)
&pstMembersInfo,MAX_PREFERRED_LENGTH,
&entriesread, &totalentries, 0 ))
{
AfxMessageBox( _T("NetLocalGroupGetMembers failed !"));
return ;
}
for( DWORD dwIdx =0; dwIdx < entriesread; dwIdx ++ )
{
AfxMessageBox( pstMembersInfo[dwIdx].lgrmi1_name );
}
NetApiBufferFree( pstMembersInfo );
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
That's pretty much equivalent to Mike's - it's just the casting's done slightly differently.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|