|
I saw members recommending CString.GetBuffer() and double casting (LPSTR)(LPCTSTR)(CString). Is there any advantage to using GetBuffer() and/or casting?
Kuphryn
|
|
|
|
|
Do the following:
item.pszText = myCString.GetBuffer(iWhateverSizeIsNeeded);
myCString.ReleaseBuffer();
or
item.pszText = (LPSTR)(LPCTSTR)myCString;
if you're looking for trouble.
Michel
It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard
|
|
|
|
|
kuphryn wrote:
(LPSTR)(LPCTSTR)(CString)
That code is patently wrong. The fact that it compiles (because the casts override type checking) and that it even runs (because the buffer just happens not to be modified) does not make the code correct. Sure, it may work in the first situation you try it, but if you make a habit of doing so, it'll eventually break with some other API and you'll be stumped as to why the code "suddenly doesn't work".
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Okay. Thanks everyone.
I will use CString.GetBuffer() and CString.ReleaseBuffer().
Kuphryn
|
|
|
|
|
I am using EnumWindows to enumerate all windows till it finds my target window (which in this case is an instance of IEXPLORE.exe, Internet Explorer). Once found I use the Handle to the window and then do ShowWindow( hwnd, SW_HIDE).
My routine works for all targets/applications I have tested so far except for Internet explorer. I manage to find the window, but when I issue the ShowWindow(SW_HIDE) for it nothing happens. The browser just stands there and stares at me.
Any ideas?
Thanks
|
|
|
|
|
Can you MoveWindow() the window? If so, you could try moving it to (-32767,-32767) in lieu of hiding it.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Well apparently the reason it does not work is that I am creating an Instance of IE using CreateProcess. I retain the processID, which I then use to find the HWND using EnumWindows. Apparently the HWND that I obtain is not that of what we see as IE. I tried something similar to what you suggested, I did SW_MAXIMIZE instead of SW_HIDE and the entire screen became white. Obviously another window that I maximized. So IE is probably run through another parent process the window to which is always hidden (and the handle to which I get from EnumWindows) or something like that. What a pain!!!!!
So now I need a way to find the HWND for the main IE window that we see and use. Any suggestions?
Thanks,
Ranjan
|
|
|
|
|
Ugh, that's nasty.
Off the top of my head, the only cheesy solution I can think of is to use EnumWindows() to find all top level windows, and check the caption of each for "Microsoft Internet Explorer" (of course this won't work on non-English systems). For every match, get the process id to which the window's thread belongs and see if it matches the saved process id. I'd have to investigate to see how to do that last part. Sorry, I know I'm not being too helpful!
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
When I used debug version, it is OK, but when I used release version, there is always a mistake to take place: the instruction at "0x0000004e" referenced memory at "0x0000004e". The memory could not be "read". I do not know what is wrong with it.
I also encountered such problem before.
Please help me!!!!!! Otherwise I will die
|
|
|
|
|
If you are using windows strucs it is a good idea to make a "ZeroMemory" first
Casa.Sapo.pt
|
|
|
|
|
|
I read the article you recommend and thought it should be a unitializated problem. But I check the code and can not find where it come from.
Following is the code which the problem come from.
CString FileName;
int nCount;
// check whether only one item hit by mouse
// if not, return
if((m_locallist.GetSelectedCount())!=1)
{
MessageBox("Please select one file","Error", MB_OK);
return;
}
// get click position
POSITION pos = m_locallist.GetFirstSelectedItemPosition();
// get item index and name clicked
nCount = m_locallist.GetNextSelectedItem(pos);
FileName = m_locallist.GetItemText(nCount, 0);
// if it is a directory, change current directory
// and show files in this directory again
if(m_locallist.GetItemText(nCount,0) == "..")
{
chdir("..");
char lpBuffer[80];
GetCurrentDirectory(80, lpBuffer);
m_locallist.DeleteAllItems();
m_local.DisplayDir(m_locallist, lpBuffer);
}
else{
CFileFind finder;
// start working for files
char File[100];
char dir[80];
GetCurrentDirectory(80, dir);
sprintf(File,"%s\\%s", dir, FileName);
BOOL bWorking = finder.FindFile(File);
finder.FindNextFile();
if(!finder.IsDots()){
if(finder.IsDirectory())
{
m_locallist.DeleteAllItems();
chdir(FileName);
// GetCurrentDirectory(80, dir);
m_local.DisplayDir(m_locallist, File);
}
}
finder.Close();
}
|
|
|
|
|
Well there are some obvious problems which jump out at me.
First:
david fox wrote:
char lpBuffer[80];
GetCurrentDirectory(80, lpBuffer);
Will trash memory whenever the current directory is >= 80 characters. This is because you aren't leaving room for the terminating '\0' in the string. I'd recomend never using hard coded sizes like 80 in function calls. Instead use sizeof( whatever )-1. You should really be using _MAX_PATH here anyway.
david fox wrote:
sprintf(File,"%s\\%s", dir, FileName);
is another disaster waiting to happen. Who says 'dir + Filename' will fit into File?
Not testing the success or otherwise of functions like chdir(), GetCurrentDirectory() etc. is also bad practice.
If you build your release code with Debug info, you can simply jump into the debugger and see precisely where the error is. I'm sure that's mentioned in the article I mentioned.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
david fox wrote:
CString FileName;
...
char File[100];
Why are you mixing char* and CString so heavily?
Most things can be done with CString (use ist buffer-access functions).
And all you buffers (char* and those that you take from CString) should really be of size _MAX_PATH !
Hope this helps
|
|
|
|
|
Hi,
A while ago , and I cant find it now, I thought I saw a post about creating a directory chooser using some MFC class. Can you please give me some ideas on how to implement this dialog in the simplest way?
Thanks,
ns
|
|
|
|
|
You don't need MFC class - SHBrowseForFolder API will do nicely.
Tomasz Sowinski -- http://www.shooltz.com
"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.
|
|
|
|
|
BROWSEINFO strBrwsInfo = {0};
LPITEMIDLIST lpIdl ;
char szFilePath[MAX_PATH] = {0} ;
strBrwsInfo.hwndOwner = m_hWnd ;
strBrwsInfo.pidlRoot = NULL;
strBrwsInfo.pszDisplayName = (LPSTR)malloc(MAX_PATH) ;
strBrwsInfo.lpszTitle = "Select the folder..." ;
strBrwsInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT ;
strBrwsInfo.lParam = 0 ;
lpIdl = SHBrowseForFolder(&strBrwsInfo);
if(lpIdl)
{
if( SHGetPathFromIDList( lpIdl, szFilePath ) == FALSE )
return false;
}
else
return false;
free(strBrwsInfo.pszDisplayName);
You get the Folder path which the user selects in szFilePath
Renjith-CPian.
|
|
|
|
|
Many thanks. One question though. I'm using mfc and I think it wil all fit in, but I dont know where to get m_hwnd. Also Do we define MAX_PATH or is it known?
Whose m_hwnd is it?
Thanks,
ns
|
|
|
|
|
MAX_PATH is a standard C definition. I think it's #defined in <stdlib.h> . m_hWnd can be got from your CWnd derived object's GetSafeHwnd() method.
That being said, I humbly suggest you use Davide Calabro's CFileDialogST class (call the SelectFolder() method) to select a folder. It's a drop-in MFC replacement and rocks big time! Here's the article[^].
PS: How did the college send-off go?
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Ah Thanks. Thats my task this morning - to implement two browsers. My original plan of embedding my blobs into the db failed because though I put in very valid bmp data into APpendChunk, it never showed up in my THBImage (a acool image control)> but if I put the same bmp in directly it shows fine. SO appendchunk is putting in extra stuff or something and theres no info on that. SO my db now quizzes for the directory where the images are located, goes out grabs them, and extracts the binary info striaght from the bmps.
Sunday was hectic. Four hour drive, then parking hassle since the whole world was there with their parents on the first day of moving in. Then went to the bookstore for $500 worth of textbooks. After which we made repeated trips to depts stores and grocery stores etc for -"I forgot shampoo" , " I need acne stuff", and then we remembered laundry. So three separate trips in the heat to the grocery store. Four trips to Target like stores for phone, lamp, and then in search of a mini fridge. EVery one was all out of this popular dorm item, so we'll get in a couple of weeks.
(ANother trip). He seems to be surviving okay - configured his email, found the dining hall, and has a roommate from Louisiana.
I'm off to investigate your link above. I slept early last night and was wide awake at 2 AM so I've been up since 2:30 pottering around. Getting old - dont need too much sleep.
I need another computer at work to install dot.net on so I can play with it. I installed the beta on my primary machine last year and didnt like the side effects of having both 6 and 7 on the same machine. Of course I could buy a laptop (personal) - when I get time to breathe, I'll think about it. In the meantime, this project marches on. My partner went along with an assumption I had made about the info embedded in his binary files , and I was wrong. So I have to unravel a bit of code and weed it out. Thats not fun. Oh well.
|
|
|
|
|
WoW! It looks cool and seems ultra easy to use . No work at all! Hey - thanks so much! I was going to slave over this last night and got lazy.Glad I did. Its a nifty solution!
|
|
|
|
|
Yeah, Davide did a great job! Now go rate his article "5".
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I'm using a vector to save objects. Looking at STL documentation it says that it will make a copy of the objects for the vector when using push_back(). I'm probably doing this wrong but it does not appear to make copies of my data.
To make the copy, I have a default constructor which does nothing but clear all of the object's attributes. Next, I have an operator= overload but it never gets called when calling push_back(). I thought that a copy constructor used operator= or do I have to make it explicit like this??? :
class myObj
{
public:
myObj() { attribute1 = 0; attribute2 = 0;}
myObj(myObj& obj)
{
*this = obj;
}
myObj& operator=(const myObj& objPassed)
{
attribute1 = objPassed.attribute1;
attribute2 = objPassed.attribute2;
return *this;
}
private:
long attribute1, attribute2;
};
All I have now is the default constructor and operator= overloaded. I thought the compiler provided a default copy constructor.
My implementation code looks like:
myObj theObj;
for(int i = 0; i < numObjsToBuild; i++)
{
theObj.attribute1 = value1;
theObj.attribute2 = value2;
myObjArray.push_back(theObj);
}
Anything wrong here?
|
|
|
|
|
VC6 doesn't compile your code, unless you change the reference in copy constructor to const. Otherwise, the code works OK.
Tomasz Sowinski -- http://www.shooltz.com
"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.
|
|
|
|
|
The compiler provides default copy constructor & assignment operator - they take the form of copying/assigning each member of the class. For your class, the compiler-supplied methods would be fine, as you've only got POD(Plain Old Datatype) data members.
The copy constructor will never call the assignment operator unless that's the way you implement it. In addition, I think vector implements element insertion (which is what push_back calls down to) without using the assignment operator (i.e. copy constructor only).
It's probably all working OK - the STL tends to most of the time
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|