|
Tarek Jabri wrote: mshflexgrid problem
Instead of fighting with the flex grid (which is what your going to be doing), try this grid MFC Grid control 2.26
by Chris Maunder [^]
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
|
|
|
|
|
I don't understand why this fails:
LPTSTR FileHelper::GetFileFullPath( LPWSTR lpwName )
{
LPTSTR lptFileFullPath;
DWORD len = GetCurrentDirectory( 0, NULL );
LPWSTR lpwFolder;
lpwFolder = (LPWSTR) malloc( len );
if ( lpwFolder == NULL )
{
free ( lpwFolder );
return NULL;
}
if ( GetCurrentDirectory( len, lpwFolder ) != 0 )
{
len = (DWORD) ( wcslen( lpwFolder ) + wcslen( lpwName ) + 6 );
lptFileFullPath = (LPTSTR) malloc( len );
// Here lptFileFullPath simply evaluates to NULL, showing the <bad ptr=""> ?!
if ( lptFileFullPath == NULL )
{
free ( lptFileFullPath );
return NULL;
}
// ...the rest of the code stripped off...
}
return lptFileFullPath;
}
This is under Visual Studio 2005, UNICODE defined by default.
Thanks.
|
|
|
|
|
I think your first malloc is wrong. "len" is the length of the current directory in characters but malloc() takes a number of bytes i.e. it should be sizeof(TCHAR) * len.
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall.
Awasu 2.2.3 [^]: A free RSS/Atom feed reader with support for Code Project.
|
|
|
|
|
First, why are you not using new and delete ? and second, why are you calling free() on what you already know to be a NULL pointer?
Now for your problem, GetCurrentDirectory() is returning the length required excluding the terminating NULL character, but you are not adding this when you call malloc. so your second call will read the string and not write the NULL. Later when you call wstrlen(), it is reading the string uo until the first NULL it finds, which could be anywhere. Therefore may return an insanly high value. When used in malloc, it returns NULL because there is not enough memory.
The problem has nothing to do with sizeof(TCHAR)
|
|
|
|
|
Thanks, you are totally right.
|
|
|
|
|
I would recommend looking VERY closely at all the string functions you use.
Some use/return lengths in characters. Some use/return lengths in bytes.
Some include NULL terminator in lengths. Some do not.
LPTSTR FileHelper::GetFileFullPath( LPWSTR lpwName )
{
LPTSTR lptFileFullPath;
DWORD LenInChars = GetCurrentDirectory( 0, NULL );
++LenInChars;
LPWSTR lpwFolder;
lpwFolder = (LPWSTR) malloc( LenInChars * sizeof(TCHAR) );
if ( lpwFolder == NULL )
{
return NULL;
}
if ( GetCurrentDirectory( LenInChars, lpwFolder ) != 0 )
{
LenInChars = (DWORD) ( wcslen( lpwFolder ) + wcslen( lpwName ) + 1 );
lptFileFullPath = (LPTSTR) malloc( LenInChars * sizeof(TCHAR));
if ( lptFileFullPath == NULL )
{
free( lpwFolder );
return NULL;
}
}
free( lpwFolder );
return lptFileFullPath;
}
*EDIT* And for goodness sakes, use new/delete instead of malloc/free!!!
|
|
|
|
|
Haha...okay, I like your style (both of writing and of coding), and thanks for your help. Now 3 questions:
1. Why malloc sucks, I don't see any problems with it. New and delete is better when you are creating some objects that have a constructor and a destructor, as they get called automatically, but not if you use malloc. But here I only use strings and string pointers, so why?
2. I already rewrite the code, to use something like:
lpwFolder = (LPWSTR) malloc( (len + 1) * 2 );
Is it wrong? (Apart from not being elegant :=) ).
3. I use _tcslen instead of wcslen, is it better suited here?
Thanks for your help again.
|
|
|
|
|
damir_tk wrote: 1. Why malloc sucks, I don't see any problems with it. New and delete is better when you are creating some objects that have a constructor and a destructor, as they get called automatically, but not if you use malloc. But here I only use strings and string pointers, so why?
I didn't say it sucks You're using C++ so why not take advantage of the much stronger type
safety provided by the language? Using "new", you create a typed pointer so the compiler can
help point out mistakes during development because it knows the pointers type without having
to use the old C-style cast. Use what you want, of course, but I bet you'll find new and delete
much more pleasing to use.
damir_tk wrote: 2. I already rewrite the code, to use something like:
lpwFolder = (LPWSTR) malloc( (len + 1) * 2 );
Is it wrong? (Apart from not being elegant :=) ).
3. I use _tcslen instead of wcslen, is it better suited here?
The only thing I see that's not elegant is mixing the generic "T" types with the hard-coded "W"
types. By using the "T" types you have the advantage of your code working for both UNICODE and
non-UNICODE builds. It also fits the way the Windows APIs are declared.
If you know you are only ever going to use UNICODE then you could just use the "W" string types.
It's just easier to read the code if you stick to one set of types and functions.
This is all just my opinions of course.
Here's another version of your code using all "T" char types and new/delete...
LPTSTR FileHelper::GetFileFullPath( LPCTSTR lpwName )
{
LPTSTR lptFileFullPath;
DWORD LenInChars = ::GetCurrentDirectory( 0, NULL );
++LenInChars;
LPTSTR lpwFolder = new TCHAR[ LenInChars ];
if ( lpwFolder == NULL )
{
return NULL;
}
if ( ::GetCurrentDirectory( LenInChars, lpwFolder ) != 0 )
{
LenInChars = (DWORD)( _tcslen( lpwFolder ) + _tcslen( lpwName ) + 6 );
lptFileFullPath = new TCHAR[ LenInChars ];
if ( lptFileFullPath == NULL )
{
delete[] lpwFolder;
return NULL;
}
}
delete[] lpwFolder;
return lptFileFullPath;
}
|
|
|
|
|
Thank a lot. Point taken.
|
|
|
|
|
I have a property page which is derived from a class (CXYzproperty)
it has 5 TABS . One of the tab page is also derived from (CXYzproperty)
I want to colour the radio & check box in the tab page i tried to use the
OnCtlColor but it dont work . Actully i dont think the message is pumped
to the tab page .
Do anyone know how to do this .??
I found out that the Message WM_CTLCOLOR is not been passed to the Page inside the
Properth sheet . As shown below I have 5 tabs on the property sheet , when i open the
Tab 1 , In the constructor i create a page .The place were Vikas is written , i want
to change color of it ( ther are some radio & check box on that page );
------------------------------
| |
---- ---- ---- ---- --- |
| 1 | 2 | 3 | 4 | 5 | |
|--- ------------------------|
| |
| |
| Vikas |
| |
| |
| |
------------------------------
thanks in advance
-- modified at 7:07 Sunday 12th November, 2006
Vikas Amin
EATON
PUNE
|
|
|
|
|
vikas amin wrote: Vikas Amin
EATON
PUNE
So left Embin?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Yes Sir
i have left embin & bombay
Can i know u ?
enjoy
Vikas Amin
EATON
PUNE
|
|
|
|
|
vikas amin wrote: Can i know u ?
We've met on CP before I think. I just remembered your previous signature when I read your name.. Vikas Amin, Embin Technologies, Bombay. Since it was different just asked you if you've shifted to a new location
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
I should admit that u have a good memory
Have a nice day
Vikas Amin
EATON
PUNE
|
|
|
|
|
Hi everyone,
I have a project in VC++ ,it is ClassLibrary project and it has a function that return jbyte *.
I would like to call this function in VB project ,how can I convert jbyte * to byte array ?
I would appreciated for any help .
If there is any document,sampe code or link for "type conversion between VC++ and VB.net "please inform me.
yours sincerely,
Orchid
|
|
|
|
|
ive been working in win32 console apps (c++) and mfc (c++) based stuff, and both of them have a type of dialog, but now how do i create a project that has no dialog interface what so ever, so like when i run it its only visible in the taskman?
i know i could set the console size or dialog size small etc, but none of that stuff.
any simple ways to do this?
/Johannes
|
|
|
|
|
|
Only try Windows Services if you want your program to run under a different session so it continues to run even when the user logs off. If that is what you want, services are the only way to go.
Otherwise, what about ditching MFC and creating a raw Win32 Project as a GUI application, and just declaring your own WinMain function. If you aren't displaying any windows whatsoever, you probably don't need a message loop.
I'm sure there's a way to do it with MFC, so you could keep using CString, etc., but since I have never used MFC I don't know.
Mike
|
|
|
|
|
I dont need it to run when logged off, just have no interface, and it doesnt have to be mfc either as im ok without it.
so from WinMain i clould make it seem like its creating a dialog, but it actualy doesnt, just runs without console or dialog in background?
sounds nice, how would i trick WinMain to have no dialogs?
/Johannes
|
|
|
|
|
You don't have to do anything special; just treat it like a main() function from a console application. It won't create any dialogs unless you tell it to via CreateDialog, CreateWindow, or the MFC equivalent.
So a simple program: (no guarantees this actually works; it is untested)
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int)
{
FILE *fp = fopen("C:\\test.txt", "wt");
fprintf(fp, "Hello, world!\n");
fclose(fp);
MessageBox(GetActiveWindow(), "Done!", "GUI-less Program", MB_OK);
}
|
|
|
|
|
How should i place that in MS visual c++ in a new win32 console project?
/Johannes
|
|
|
|
|
How should i place that in MS visual c++ in a new win32 console project?
thanks for teh help
/Johannes
|
|
|
|
|
Johpoke wrote: How should i place that in MS visual c++ in a new win32 console project?
You need to create a Win32 GUI project.
|
|
|
|
|
thanks a mil
/Johannes
|
|
|
|
|
In my program can i easily add mfc support?
/Johannes
|
|
|
|