|
Try this...
#include <afxtempl.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
struct Person
{
Person(const CString& n = "", int a = 0) : name(n), age(a) {}
CString name;
int age;
};
class ComparePerson : public binary_function<Person, Person, bool>
{
public:
// Sort by name and then age
bool operator()(const Person& first, const Person& second) const
{ return first.name < second.name || (first.name == second.name && first.age < second.age); }
};
void SortTest()
{
CArray<Person, Person> person;
person.Add(Person("Kevin", 30));
person.Add(Person("Gary", 24));
person.Add(Person("Peter", 36));
person.Add(Person("Peter", 16));
person.Add(Person("Richard", 19));
person.Add(Person("Joanne", 40));
int count = person.GetSize();
afxDump << "Initial contents\n";
for (int i = 0; i < count; i++)
{
afxDump << "name = " << person[i].name << ", " << "age = " << person[i].age << "\n";
}
// Last Person
Person* begin = person.GetData();
Person* end = begin + count;
afxDump << "Last Person \n";
Person* last = max_element(begin, end, ComparePerson());
afxDump << "Last Person: " << "name = " << last->name << ", " << "age = " << last->age << "\n";
afxDump << "\n";
// Sort
sort(begin, end, ComparePerson());
afxDump << "Sorted persons\n";
for (i = 0; i < count; i++)
{
afxDump << "name = " << person[i].name << ", " << "age = " << person[i].age << "\n";
}
}
Output
Sorted persons
name = Gary, age = 24
name = Joanne, age = 40
name = Kevin, age = 30
name = Peter, age = 16
name = Peter, age = 36
name = Richard, age = 19
Is this what you want?
Kevin
|
|
|
|
|
I'm trying to find some code that will work for multinode b-tree. All of my searches yeilded broken code or class syllabus.
Does anyone have one that works for VC++ 6.0
Thanks in advance,
Nordyck
|
|
|
|
|
Hi,
It seems possible to connect 2 programs
when both are behind firewalls.
It seems that a "torrent tracker"
listens for clients, and when 2
firewalled clients wants to
connect one to the other, the tracker
establish the connection between them
and then leave?
How is the tracker able to do this?
Thanks for a great site.
Fabio, boicotinho@[hotmail.com]
|
|
|
|
|
Hi all. I'm trying to create a linked list of driver names. The following code snippet creates a 3 objects, the 1st outputs some text, the 2nd creates a details object and the 3rd creates a driver_list object which is a container class for a list of details objects.
The problem I'm having is that I don't know what to pass to "list.add_driver()", it expects a details object to be passed to it. I've tried passing a details object but I just get errors thrown up.
Can anyone give me any pointers? ( pardon the pun )
//////////////////////////////////////////////////////////////////
int main(){
clsoutput output;
output.GetDriverName();
details driver;
driver_list list;
for(int i=0;i<=2;i++){
driver.enter_driver_name();
output.DisplayDriverName(driver.get_name());
list.add_driver( // what goes here? ) // problem line
}
return 0;
}
//////////////////////////////////////////////////////////////////
add_driver method which is called when "list.add_driver()" is invoked
//////////////////////////////////////////////////////////////////
void driver_list::add_driver(details* pDetails)
{
package* pPackage = new package(pDetails);
if(pHead)
pTail->setNext(pPackage);
else
pHead = pPackage;
pTail = pPackage;
}
//////////////////////////////////////////////////////////////////
|
|
|
|
|
Seems like you have to pass a pointer to a details object:
list.add_driver(&driver); Am I missing something?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Joaquín, nice one, that works a treat. You're not missing a thing, its that simple. I've not touched C++ in ages and even then I didn't know it well so I'm quite rusty.
It compiles fine so it might not be safe to assume that the linked list works I'll need to iterate over the list to find out.
Thank you very much.
Graeme
|
|
|
|
|
I am having a problem in passing an integer as an agrument to a thread of the format:
UINT threadname (LPVOID);
I am passing 4 distinct values to 4 distinct threads using the same function as the thread operation. Whenever i typecast the address of the interger to LPVOID type, pass it an argument to CreateThread() & then re-typecast it to an integer in the thread function, i either get runtime errors or i get junk values. but never the original value of the integer that i wanted.
Is there any special way of typecasting pointers in vc++.net??
I also tried out using the reinterpret_cast operator and also replacing the CreateThread() function by AfxBeginThread(). Although i dont get any runtime errors now, but the same value is passed to 2 threads occasionaly (Rememeber i want 4 distinct values to be passed to 4 distinct threads) & sometimes a completely strange no is passed. The value is repeated for only 1 thread, the others are ok.
Plz help if any1 knows a workaround or solution to this.
--
Nikhil
|
|
|
|
|
Whenever i typecast the address of the interger to LPVOID type...
Here's the problem, I guess. You are passing the address of an int declared on the stack. As soon as you exit the function from where you're calling CreateThread , that int gets destroyed, so that when the thread tries to recover it it is likely garbage that's being retrieved instead.
As an int and LPVOID have the same size (in Win32), you can just reinterpret_cast the int (not its address!) to a LPVOID , and you should be done.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Yes it is bad problem... but try this ... instead pass iteager value as address pass a pointer to integer valeu... once i have similar problem .... i pass adress of varible with 3 distinct values to 3 ditinct threads but only first value is passed to all 3 distinct threads (no debug errors, no runtimes errors, no linking errors). Also in debbug mode it worked proparly but in release it didn't work proparly. The solution was to pass pointer to my varible instead as regular parameter. Some thing like that:
int *p = new int;
*p = 5;
CreateThread(..., p, ...);
|
|
|
|
|
OS: win 2000
Is it possible to write to the .ini file from a .bat file?
Here's what I'm trying to do:
I have .bat that is supposed to make the user's life easy by installing our software using a base directory that the user specifies. So the user would execute "installProd.bat C:\user_area" (or Whatever Disk>:\where ever). The .bat creates the directory specifed (as well as others below it) places our C++ executables and our supporting files (like any input data files or help files, etc) into the dirs.
Now I'm hoping to write to the .ini file from within the .bat so I can put the user defined base directory into the .ini. Then when the C++ executables run, the executables can access the .ini file for the base directory path (using GetPrivateProfileSettings or GetProfileString etc).
Is it possible? Or am I going down the wrong path for this? ANy suggestions and guidance is much appreciated!
Thanks,
blueSprite
|
|
|
|
|
You can use echo .For instance, the following commands
echo>parms.ini [general]
echo>>parms.ini key=value create an .INI file like this
[general]
key=value Note that a single > means "create new file" and >> means "append". Hope this helps.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
why not use an installer ?
InnoSetup (http://www.jrsoftware.org/isinfo.php) provides a simple tool for creating setup's. It has also builtin support for writing in .ini file.
The Tool is free available and well supported
If you still want to handle the stuff with a batch script:
append your stuff at the end of that ini-file via
...
echo [section] >> myIniFile.ini
echo myKey=%1 >> myIniFile.ini
...
pbloechl
|
|
|
|
|
Thanks both of you for your responses. I completely overlooked the obvious.
BTW, the installer suggestion is a great one, but my company often refuses to use anyone else's software but their own (the wheel has been reinvented more times than I can count
Thank you both so much for your responses!
blueSprite
|
|
|
|
|
After working on project for several months I started getting assertion failure at winocc - GetExStyle. I have been "solving" this problem by regressing to last working version of the project. But now I am stuck, it fails every time I add new stuff. I need to fix this. Rumor has it it has to do with OLE, but I have been unable to find anything on the web.
Here is the Output message :
...
Warning: Window creation failed: GetLastError returns 0x0000057F
Warning: failed to create CFrameWnd.
...
How can I get to this "last error" ?
Thanks for your help.
Vaclav
|
|
|
|
|
Check out line 570 of winfrm.cpp. CreateEx() returns ERROR_CANNOT_FIND_WND_CLASS if the class name specified is invalid. Set a breakpoint on that line and note the value of lpszClassName . Having that might help to narrow down the problem.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Thanks for your suggestion.
Found the cause , now what?
How is it possible to run wrong process?
My code seems to be failing when I modify/ add to my dependent library!
It works OK when I work on my main project only
Here is what is happenning:
MFC runs CWnd::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
instead off PreCreateWindow(cs)! per following sinipet:
LPCTSTR CFrameWnd::GetIconWndClass(DWORD dwDefaultStyle, UINT nIDResource)
...
CREATESTRUCT cs;
memset(&cs, 0, sizeof(CREATESTRUCT));
cs.style = dwDefaultStyle;
this call is not executed !
PreCreateWindow(cs);
// will fill lpszClassName with default WNDCLASS name
// ignore instance handle from PreCreateWindow.
...
So, the cs is never filled out and the file name is missing!
void CWnd::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
assertion fails here, as it should, because of wrong/ no data in cs
DWORD dwExStyle = GetExStyle();
if (nAdjustType == 0)
dwExStyle &= ~WS_EX_CLIENTEDGE;
::AdjustWindowRectEx(lpClientRect, GetStyle(), FALSE, dwExStyle);
}
WNDCLASS wndcls;
|
|
|
|
|
I have a problem with the include files and I am getting an error :
win32.obj : error LNK2019: unresolved external symbol _BltAlpha referenced in function _DD_Init
This is how I have set up the project:
I call the BltAlpha in DD_Init in
win32.c:
#include "stdafx.h"
bool_t DD_Init(win* p)
{
BltAlpha(p,.....
I have declared all my include files in
stdafx.h:
#include "win32.h"
#include "blend.h"
And then I have included stdafx.h in both blend.c and blend.h
blend.c:
#include "stdafx.h"
// The implementation of BltAlpha:
int BltAlpha( win* p,int iDestX, int iDestY, LPRECT lprcSource, int iAlpha, DWORD dwMode )
{ ............
blend.h:
#include "stdafx.h"
//The declaration of BltAlpha:
int BltAlpha( win* p,int iDestX, int iDestY, LPRECT lprcSource, int iAlpha, DWORD dwMode );
So why am i getting this link error, I have really tried to put the header files in different combinations, but with no luck?!
Please is there anyone that can help me with this one before I´m going crazy!?
Thanks, Dani
|
|
|
|
|
Hi,
I think that this is a tricky question : try and rename your .c files into .cpp files, and it should solve the problem . Visual adapts the calling convention according to file extensions. You have probably created a C++ project, and tried to insert a c file. A very naive explanation would be: the way the parameters are called with
BltAlpha(p,.....
is the C++ way and the way you declare in in blend.c is compiled as the C calling convention. This is Ok for the compiler, but it cannot be linked.
If you do not want to rename your files, try modify the calling conventions in the settings of your project Project->Settings->C/C++->Category:Code Generation->Calling convention.
But I must admit, this is tricky
~RaGE();
|
|
|
|
|
As for the header file organisation, this should do it:
stdafx.h:
#include "win32.h"
#include "blend.h"
win32.c:
#include "stdafx.h"
blend.c:
#include "stdafx.h"
~RaGE();
|
|
|
|
|
Thanks Rage!!!!!!
Whooooooops! the blend.c was named blend.cpp in my project, did not noticed that !
This have to be a bug in Visual C++ 6.0, because the only options for adding a file is
new->c/c++ header file
or
new->c++ Source file and there is no option for a new c file
This is strange though, anyway thanks Rage
|
|
|
|
|
Dani100001 wrote:
This have to be a bug in Visual C++ 6.0, because the only options for adding a file is
new->c/c++ header file
I use Project --> Add To Project --> Files
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Hi,
I want to replace the hardcoded string by the path in %windir% in the following code
#ifdef WIN32
CHAR CFM_INI_FILE[CFM_NAME_LEN] = "C:\\Winnt\\abc.ini";
#else
CHAR CFM_INI_FILE[CFM_NAME_LEN] = "/Winnt/abc.ini";
#endif
can I do that?
I cannot use GetWindowsDirectory function because this code is in global space in a cpp file.
My understanding is a function can be called only from within a function and not from global space.
Please advice..
P.S. :- this code has to be in global space. If i put it in function and initialize there i have to make changes in over 60 files to make sure it is initialized before it is used.
|
|
|
|
|
It is possible to get the value of %windir% with several functions, amongst them
char *getenv( const char *varname );
but from what I understand in your msg, you do not want to use any initialisation, e.g. function call. So I am afraid that the answer to
venadder wrote:
can I do that?
is : yes, you can, but it will unfortunately not be replaced at runtime with "C:\Winnt".
So, be ready to modify your 60 files
~RaGE();
|
|
|
|
|
Hi,
Am sorry if the subject looks too long. Didn't know how to explain it better....
Here is my problem. I divide my SDI into 2 columns using dynamic splitter. I associate the left column with some view. Now i want the right column's view to show a dialog resource from one .dll. I could get the dialog resource from the .dll and display it in the right view by getting the resource ID from the dll ( after loading it ofcourse ) and then using the FindResource() and LoadResource(). But the problem is that if i enter something in the dialog resource and then click "Ok", then i expect my handler in the .dll to be called and then retrieve all the configuration parameters that user enters in the dialog resource.
But my handler is not getting called, and obviously because of that, the dll cannot get the parameters entered into the dialog resource.
Is there any way to do this? So that the .dll can contain the dialog resource, and i can embed it in my main exe after loading that dll. And then once user clicks ok on the dialog, the appropriate handlers and data exchange happens inside the .dll, and my main application neednot care about the configuration parameters that the .dll wants...
Thanks & Regards,
Kumar
Kumar
|
|
|
|
|
Hello
I would like to call ImpersonateLoggedOnUser to gain some extra rights for a particular task in my application. For the call I need an impersonation token. To get this I tried the LogonUser function, but I don't have enough privileges. Probably it's the SE_TCB_NAME privilege. If I inspect the results from GetTokenInformation it is true that I don't have that priv and hence cannot enable it.
Does anyone know how to get a proper impersonation token.
I've tried to call CreateProcessWithLogonW to create a dummy process as a different users, and with that process handle I can call OpenProcessToken to get a token to send to ImpersonateLoggedOnUser. This gives the expected result, but it is a verry uggly work around.
|
|
|
|
|