|
the simplest way is to have the child call a member function on the parent.
void CChildDlg::OnHScroll(whatever)
{
CParentDlg *pParent = (CParentDlg *)GetParent();
pParent->UpdateZoomInfo(some info about the zoom);
}
|
|
|
|
|
You question is already answered, I just wanted to say that your solution seems to be a bad gui design. Setting the parameters of a disabled window from a modal dialog box isnt the way to go. I think you should do that from a modeless toolbox window.
|
|
|
|
|
hi guys,
i am using vs.net 2010 c++ and i need help:
i have
class school
{
public:
vector<student*>* students;
school()
{
vector<student*> s;
students = &s;
}
}
however when i try to
school s;
students.push_back(&s);
there is always an error,
Unhandled exception at 0x0f5757aa (msvcr100d.dll) in ast1.exe: 0xC0000005: Access violation reading location 0xccccccc0.
could anyone help me please.
|
|
|
|
|
There are so many issues with your code ...
Your vector is a pointer; you must allocate it before using it :
The student vector is a member of the class; so you must access it via the school class.
Change to (it is still very early here, and I've noy yet had coffee):
class school
{
public:
vector<student*> students;
school()
{
}
}
int main()
{
student* pStudent = new student; school aSchool; aSchool.students.push_back( pStudent );
return 0;
}
As a side note, your vector could contain "plain" students if the size and the data inside each student is small and simple enough; that would reduce the need for allocatation and de-allocation.
Also, have a look at smart/shared pointers for cleaner code (more advanced concepts).
Good luck.
Watched code never compiles.
|
|
|
|
|
neodeaths wrote: students.push_back(&s);
You are trying to push the address of a school object onto a vector expecting a student pointer. Is that the intent?
"One man's wage rise is another man's price increase." - Harold Wilson
"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
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
The exception is caused by invalid initialization code. In your constructor, you define a local vector s , and store the address to that variable in school::students . That local variable is located on the stack. The moment you leave that constructor though, the stack is unwound and the local variable will be destroyed. Then school::students points to an invalidated memory location. The moment you try work with the students vector, you'll get an exception as it points to invalid memory.
Instead of initializing your variable on the stack, you have to allocate it on the heap like this:
class school {
public:
vector<student*>* students;
school()
{
students = new vector<student*>:
}
~school()
{
delete students;
}
};
Note that I also added a destructor to make sure the memory will be released again when it is no longer needed.
|
|
|
|
|
Environment: Winows XP Pro, Visual Studio 2008, MFC, C++
Goal: To modify my blocking TCP code that transmitts data to a client to non-blocking or asynchronous operations. The blocking code works, but needs to be changed to async, or to a separate thread, or maybe both, I am uncertain but think that last will be best.
I have been reading the Article: Programming Windows TCP Sockets in C++ for the Beginner
By c_07 and found here:
Programming Windows TCP Sockets in C++ for the Beginner
I think I follow fairly well up through the method definition: int ListenOnPort(int portno)
As I understand it, this is the code for the server to listen for a client to connect and it is blocking. The the author proceeds into the async section and says to put this line of code after the listen() call:
WSAAsyncSelect (s, hwnd, MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |
FD_READ | FD_CLOSE); I don't understand how putting a call after the Listen() can transform that call from a blocking call to a non blocking call. I suspect there are more code changes to the server code but I am not unable to recognize what I need to change. I am writing the server part to communicate with a vendor's client application.
Please advise as to what I have missed.
Thank you.
Thanks for your time
|
|
|
|
|
The listen() call never blocks not even if you use blocking sockets. It just tells the operating system to establish listening on that socket and to put connection requests to a queue for that socket (the size of the queue is the backlog parameter of listen() ). What might block is the accept() call that removes an incoming connection from the listen queue. Your code replaces this blocking accept() call, windows sends your window a message when you have an incoming connection. Async or blocking? Either way you go its definitely better to put networking to a separate thread instead of handling it with the windows specific window-message events. This window-message driven networking mixes ui code with networking (against the Separation of Concerns - and the result is not reusable code), might decrease the responsiveness of your ui if you use high throughput. Async networking is more difficult to program but sometimes that's the clear way to go. Whether blocking or async it depends on what you exactly want to do.
modified 27-Aug-12 5:03am.
|
|
|
|
|
Well that is a quite interesting reply.
This application will be sending between three and nine messages per millisecond, maybe as much as nine killobytes per millisecond. I presume that puts it an the high end of the scal for throughput.
Just in case: This application outputs data and other than status from the TCP/IP manager, does not input any data via the TCP/IP port
Do you have a favorite tutorial or guideline for starting new threads and transferring data from the main thread to the new one?
Thanks for your time
|
|
|
|
|
That's around 10MByte/sec, thats quite OK, with good network 100MByte/sec can be achieved. You might want to set the send/receive buffer size of your sockets (setsockopt/SO_SNDBUF/SO_RCVBUF), without this sometimes its not possible to reach the best throughput with the default buffer sizes (I don't know why, found this out by experimenting). But this is not a parameter that decides between async/blocking. Its rather the protocol. When do you send and when do you receive data? If you can exactly determine when to receive and when to send then use blocking sockets because its easier (the typical protocol for blocking sockets is request - response, one of the peers is active and the other just replies). If you might want to send at arbitrary points and you might have to receive incoming packets at random points then you need to write an async solution that also handles when the bandwith isn't good enough to send data with the desired rate.
EDIT: Transferring data from one thread to another one: this stuff is called blocking queue. It is the most universal and often the most efficient way to send data/events from one thread to another. The worker thread has a blocking queue and it always gets the first item from it, if the queue is empty then the worker thread blocks on the get() method of the queue until another thread puts an item to it. After getting the item from the queue the worker thread processes that item (sends data on the socket) and then it goes back to the beginning of the loop and gets another item from the loop for processing. The other thread or threads can put data to the other side of the queue. The put method doesn't block. You should just store pointers in this queue, pointers to data objects to minimize the synchronization overhead (by avoiding data copying) in the blocking queue. Unfortunately I have no favorite threading tutorial but I'm currently working on a codeproject article about threading for dummies. That will describe threading step by step and will teach using blocking queues too.
EDIT: Since in your case the data flow goes to a single direction you should use a blocking socket. You should also handle somehow when your bandwidth goes too low, in that case you should avoid the blocking queue from growing ultra large causing an out of memory error. You should somehow suspend the generation of messages in that case because sooner or later you will not have space to store the newly generated messages.
EDIT: A very simple example to using a multi-producer single consumer blocking message queue (with only a single producer thread - the main thread):
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <queue>
template <typename T>
class BlockingQueue
{
public:
BlockingQueue()
{
InitializeCriticalSection(&m_Lock);
m_hBecameNonEmptyEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
}
~BlockingQueue()
{
assert(m_Queue.empty());
DeleteCriticalSection(&m_Lock);
CloseHandle(m_hBecameNonEmptyEvent);
}
void Put(const T& item)
{
EnterCriticalSection(&m_Lock);
if (m_Queue.empty())
SetEvent(m_hBecameNonEmptyEvent);
m_Queue.push(item);
LeaveCriticalSection(&m_Lock);
}
void Get(T& item)
{
EnterCriticalSection(&m_Lock);
if (m_Queue.empty())
{
ResetEvent(m_hBecameNonEmptyEvent);
LeaveCriticalSection(&m_Lock);
WaitForSingleObject(m_hBecameNonEmptyEvent, INFINITE);
EnterCriticalSection(&m_Lock);
}
item = m_Queue.front();
m_Queue.pop();
LeaveCriticalSection(&m_Lock);
}
private:
BlockingQueue(const BlockingQueue&);
BlockingQueue& operator=(const BlockingQueue&);
private:
CRITICAL_SECTION m_Lock;
HANDLE m_hBecameNonEmptyEvent;
std::queue<T> m_Queue;
};
BlockingQueue<int> q;
DWORD __stdcall ThreadProc(void* param)
{
printf("thread started\n");
for (;;)
{
int i;
q.Get(i);
printf("processing %d\n", i);
if (i == 0)
break;
}
printf("thread is out of the loop, returning from threadproc...\n");
return 0;
}
int main(int argc, char* argv[])
{
printf("Creating thread...\n");
DWORD thread_id;
HANDLE hThread = ::CreateThread(NULL, 0, ThreadProc, NULL, 0, &thread_id);
if (!hThread)
{
fprintf(stderr, "CreateThread error!\n");
return 1;
}
for (;;)
{
char buf[0x100];
if (!gets(buf))
break;
int i;
if (1 == sscanf(buf, "%d", &i))
{
q.Put(i);
if (i == 0)
break;
}
else
{
printf("Not an integer: %s\n", buf);
}
}
printf("Waiting for the thread to finish...\n");
WaitForSingleObject(hThread, INFINITE);
printf("Wait finished.\n");
CloseHandle(hThread);
return 0;
}
You should store pointers in the blocking queue and you can use for example the NULL pointer to ask the other thread to exit like I used the zero value.
modified 27-Aug-12 16:11pm.
|
|
|
|
|
bkelly13 wrote: This application will be sending
Then why are you creating a listener?
bkelly13 wrote: This application will be sending between three and nine messages per millisecond
That means it must generate 9,000 messages a second.
To send that many the app must create that many. And given that you are suggesting that a message is 9k I doubt the creation is trivial.
|
|
|
|
|
Hello, my first post here.
I have a question. I have a pretty old PS2 optical mouse which has the standard two buttons, wheel + middle button, and it has two additional buttons for clicking with thumb. The mouse works great for a few years already, but I lost the drivers and they were on a floppy disk anyways.
I was wondering how can I get the input from these additional buttons, I wanna make a program in C/C++ which would get the input from the buttons and convert it to a virtual joystick. I can handle the joystick part, it's easy (I'm using PPJoy for this).
Please note that I never did any port programming, but I'm very good at anything else.
So, I'd like to know how to get the input from these keys. I'm using Win XP SP3.
And, please don't tell me to get a new mouse, I want to improve my C++ skills and get two additional keys for free
Thank you. David.
|
|
|
|
|
You should try these: WM_XBUTTONDOWN and related window messages, WM_INPUT window message, directinput, and here is a hack on codeproject that might come handy: How to use your extra mouse buttons in games or apps[^]. Note: I havent used WM_XBUTTON messages and used WM_INPUT only to get mouse movement delta instead of mouse pos but trying these isnt a big waste of time.
|
|
|
|
|
Hi all,
i am writing a program that opens an explorer window, when CreateProcess open new window the main program must be minimized when i will close Explorer.exe i want that program return in SW_SHOW state.
I simply need to detect when Explorer.exe will be Close.
I have seen that when i use createprocess with microsoft MSDN example it will check only one time.
What i have to do to complete my task please help me
|
|
|
|
|
It is possible to wait for a program to exit when you start it with CreateProcess() but in your case you shouldn't do that. Many text editor and browser and whatever programs work like this: They allow you to open many documents on tabs. When you use CreateProcess() to start the editing or viewing of a document (or a webpage in your case) you launch an instance of the editor program that does the following: It checks if another instance of that program is already running and if that is the case then the process you created is just passing some info to the already running process and the process you started exits immediately. The already running process shows your document/webpage on a new tab. You shouldn't check for closing the explorer because you can't for the reasons I listed previously, moreover, I know that many users never exit their web browser - they use it open all they with hundreds of tabs for some reason.
|
|
|
|
|
You can wait on a process handle:
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bOk = CreateProcess(
_T("C:\\Windows\\system32\\notepad.exe"),
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
if (bOk)
{
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
MessageBox(NULL, _T("Finished"), _T("Create and wait"), MB_OK);
}
Steve
|
|
|
|
|
Thanks for ask
I have tried your solution and it goes good when i need to close my opened app,
but i need this:
i want open an explorer.exe window and when i close my caller program i need that program closes explorer window too.
Do you think it's possible?
|
|
|
|
|
Now I'm using VC++ 2010, but the
syntax_option_type of VC++ 2010 only contains the following options:
static const flag_type icase = regex_constants::icase;
static const flag_type nosubs = regex_constants::nosubs;
static const flag_type optimize = regex_constants::optimize;
static const flag_type collate = regex_constants::collate;
static const flag_type ECMAScript = regex_constants::ECMAScript;
static const flag_type basic = regex_constants::basic;
static const flag_type extended = regex_constants::extended;
static const flag_type awk = regex_constants::awk;
static const flag_type grep = regex_constants::grep;
static const flag_type egrep = regex_constants::egrep;
It doesn't contain
perl_syntax_group (Boost Library has the option). However, I don't want to use the Boost Library.
There are many regular expression written in Perl, So I want to convert the existing Perl regular expressions to
ECMAScript (or any one that VC++ 2010 support). After conversion I can use the equivalent regular expressions directly in VC++ 2010 without using the third party libray.
One example:
const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
const CString human_format = __T("$1-$2-$3-$4");
CString human_readable_card_number(const CString& s)
{
return boost::regex_replace(s, e, human_format);
}
CString credit_card_number = "1234567887654321";
credit_card_number = human_readable_card_number(credit_card_number);
assert(credit_card_number == "1234-5678-8765-4321");
In the above example, what I want to do is convert e and format to ECMAScript style expressions.
ECMA Script regex is a subset of Perl regex, if I don't use the features exclusive to Perl regex.
Is it possible to find a general way to convert all Perl regular expressions to
ECMAScript style? Are there some tools to do this?
Any help will be appreciated!
|
|
|
|
|
I'm having a very strange problem with a third party dll. When I try to use this dll in my program an exception is thrown even before WinMain is called, yet other programs use it without any problems.
Eventually, I narrowed it down. If any program uses the dll and also calls a certain Windows function (for example SHGetSpecialFolderPath, or timeGetTime) then the the exception happens.
This problems seems really bizarre to me. Does anyone know what could be causing it?
|
|
|
|
|
You stated that the exception is thrown before your WinMain(). Statically linked dlls are loaded by windows for your executables and DLLs. The DllMain() of statically linked DLLs execute eralier than the main of the module (dll or exe) that actually uses the DLL. I would suspect that something is screwed up in your 3rd party DLL and it might be sensitive to DLL loading order. If you exe uses SHGetSpecialFolderPath() from shell32.dll and the 3rd party library also uses a function from shell.dll then the loading of shell32.dll might happen before or after the loading of your 3rd party depending on whether your program loads shell32.dll or the 3rd party first. However this whole situation seems to be totally screwed up for me. Try to set a breakpoint on "module load" events and debug the loading order of your DLLs, maybe you find out something useful. Try to search for the exception code too in google.
|
|
|
|
|
Post a call stack. Always post a call stack!
Steve
|
|
|
|
|
Ok:
> ntdll.dll!7c967406()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!7c940e5a()
ntdll.dll!7c918790()
ntdll.dll!7c90e457()
libGLESv2.dll!00360062()
I doubt if that's any use though
|
|
|
|
|
The one useful bit is the last line that suggests the problem started in libGLESv2.dll. Assuming this is the 3rd party library you should contact the author(s) for help.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Yeah, I already contacted them, but no answer. I think I'm just going to give up and use an alterate library.
|
|
|
|
|
Use a symbol server when getting a stack trace.
Steve
|
|
|
|