|
i have this problem which is the magic square. i need to know how to use the keyboards arrows in the program like he says. and i only can put number "1" and "2" in their right place can't get the equations for other numbers.
the prompt is:
A magic square is an n*n matrix in which each of the integers 1,2,3…n^2 appears exactly once and all column sums, row sums, and diagonal sums are equal. For example, the following is a 5*5 magic square in which all rows, columns and diagonals add upto 65. The magic number can be calculated according to the following equation : Magic = Sum (1..n^2)/n.
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The following is a procedure for constructing an n*n magic square for any odd integer n. Place 1 in the middle of the top row. Then after integer k has been placed, move up one row and one column to the right to place the next integer k+1, unless one of the following occurs:
• If a move takes you above the top row in the jth column, move to the bottom of the jth column and place k+1 there
• If a move takes you outside to the right of the square in the ith row, place k+1 in the ith row at the left side
• If a move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k+1 immediately below k.
Write a program to construct n*n magic square for any odd value of n. Your program should provide the facility for the user to input the numbers in any desired position (given it is free). Your program should check the user’s input to verify that it results into a magic square. If this condition is valid you congratulate the user, otherwise you generate the right answer for the user. The user is allowed to play the game as many times as he/she wants, when the user chooses to end the puzzle, your program should tell him his score (number of times he managed to solve the puzzle/number of games).
Input
Max dimensions of the array
The user will then play the game by placing numbers from 1..n^2 on the board
Output
Solution of the puzzle in each time the user chooses to play
User’s percentage
|
|
|
|
|
|
Hello
I am coding an application I do have problem to work with ActiveX COM. I do not know how to convert
System::String * form structure or char and pass to function with argument which is type of System::Object _gc * _gc *.
Does anybody can help???
Tnx
Mirdous
|
|
|
|
|
Hello,
I am new to Mulithreading. In my project I am working with multiple threads. Since I haven’t worked with threads before, I am facing many hurdles. Can any one please guide me or give some hints.
I am explaining in brief about the threads used by me in my project.
1st thread: Filter Thread
Is started at application start up.
Filters each line read from file( Log data is read from file)
I have used CCriticalSection and CSingleLock to lock the data access here as this
LIST is accessed in Display thread.
Adds the data to the LIST from top.
This thread has to run continuously till the application end.
2nd thread:Display Thread
Is started at application start up.
Displays each line filtered by Filter thread
I have used CCriticalSection and CSingleLock to lock the data access here as this
LIST is accessed in Filter thread.
Retrieves the data from the LIST from bottom.
Deletes the data from the list after display.
This thread has to run continuously till the application end.
3rd Thread: Monitor Thread
Is started at application start up.
This thread has to continuously check if new records are added to the file ( Here another external application will add records to the file)
If new records are added, it notifies the Filter Thread, which fetches the added records, filters them and gives to Display thread for displaying.
Since all the above three threads have to run continuously, I had used sleep in all the threads as shown below.
UINT ThreadFunction(LPVOID lp)
{
while(TRUE)
{
// Do the work
}
}
The result of this is that CPU time is 100% used because of the While loop.
Can anyone please suggest an alternative for while loop in the other two threads.
Thanks
Madhavi
Madhavi.
|
|
|
|
|
Try to replace Sleep with WaitForSingleObject .
UINT ThreadFunction(LPVOID lp)
{
m_eventShutdown.CreateEvent();
while(TRUE)
{
DWORD dwRet = WaitForSingleObject(m_eventShutdown.m_hObject,1);
if(dwRet==WAIT_TIMEOUT)
else if(dwRet==WAIT_OBJECT_0)
else
}
}
|
|
|
|
|
Hi,
Thanks for the inputs.
I have a doubt here. If I use WiatForSingleObject inside my While loop , won't that eat my CPU time because While loop is still present?
Madhavi.
|
|
|
|
|
If you set the timeout not equal 0 in WaitForSingleObject , each time your thread calls WaitForSingleObject it passes the control to the system scheduler and it won't eat 100% of your CPU time (it is similar to "pumping messages" in UI threads).
|
|
|
|
|
Hi Andrzej Markowski,
Thanks for your inputs.
I have declared my monitor event as below first.
volatile HANDLE m_MonitorEvent;
m_MonitorEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
ResetEvent(m_MonitorEvent);
//This event is set in the part of code which triggers this event.
SetEvent(m_MonitorEvent);
//Thread function
BOOL b_EventRunning = TRUE;
UINT ThreadProc(LPVOID lpVoid)
{
while(b_EventRunning == TRUE)
{
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
//Do the work
ResetEvent(m_MonitorEvent);
break;
default:
Sleep(1);
break;
}
}//end of while
}//end of ThraedProc
I used INFINITE in WaitForSingleObject because my this thraed has to run till the application is exited.
When this thread is doing the job, the CPU usage is shown as 100%. Please let me know if I'm missing out something here which is making my CPU consume 100%?
Thanks
Madhavi
|
|
|
|
|
My response to your previous post outlined how to implement locking between two threads.
If I understand your architecture you need to have two mutual exclusion locks. By mutual exclusion I don't only mean mutex, although mutex would work fine, but also any other scheme to inpliment mutual exclusion between threads and/or proceses, i.e. mutex, event, semaphore.
You need to coordinate the interaction between the monitor and filter with one object (monitor writes and filter reads) and between the filter and display with another (filter writes and display reads).
The names need to be unique across the entire system because they are kernel objects.
See previous post for implementation details.
Unfortunately I didn't know your naming when I responded to your previous post and we both chose monitor to mean different things. Don't be confused by the names I used. It is a general example.
|
|
|
|
|
Hello,
In my project, I have to check if a particular file is updated and take some action accordingly. Hence I have started a thread which continously checks for file update ashown below:
UINT CheckFileUpdates(LPVOID lp)
{
while(TRUE)
{
CFile::GetStatus(pView->m_LogFilePath,st);
//code for checking the status of file
// take some action when the file is updated
// in this case send the added new line to destination
}
}
This thread should be started at application start up and should run continously till the application is closed.
Since I used While(TRUE) to check continously the status, the application is becoming slowler. Is there any other way to make the thread continously check the status without using while loop.
Can anyone please guide me ?
Thanks
Madhavi
|
|
|
|
|
The easiest fix is to throw in a SleepEx.
Other more "elegant" solutions exist, but for a single file, I havent found them worth the effort.
|
|
|
|
|
You can use a kernel mode synchronization object, i.e. event or mutex. You can also use a semaphore and set the max count to 1 and it will act like a mutex but then why not use a mutex.
Basically what you need to do is acquire the event or mutex in the code updating the file so that no one else can get it. After the object is acquired you start the monitor thread and attenpt to obtain the object in that thread. The monitoring thread will block and not be eligable for scheduling again until the object is acquired which will not happen until the file process thread releases it after making an update to the file. After a short wait, 1 or even 0 microseconds, to insure the file processing thread gives up the remainder of it's timeslice thus insuring that the object is acquired by the waiting file monitoring thread, attempt to obtain the object again in the file processing thread.
The file monitor then wakes up, does what it does and releases the object so that the file processing thread can get it again. Since the file processing thread is queued up to get the object you can try to acquire the object again in the monitoring thread after a short wait so that the monitoring thread gives up the remainder of it's time slice to insure that the file processing thread will obtain the object. In this way the monitoring thread will be blocked and not using CPU cycles until it comes to life when the file processing thread releases the object.
This will work even if the file processing thread is in a different process than the monitoring thread. Note that all the threads have to be executing at the same dispatch priority for this logic to work properly.
The important thing to remember is to relinquish the remainder of the threads time slice before trying to acquire the object again thus insuring that the other thread is able to get the object.
If more than two threads are involved in the logic you will need to use a memory mapped file or additional, uniquely named, kernel mode synchronization object(s) to exchange state information between processes so that they know when to attempt to obtain the object.
You will have to insure that the object name is unique between all processes so it is best to use either the guidgen or uuidgen programs to generate a unique identifier for use in the object's name. I would also include a user friendly prefix or suffix so if you use a spy program to debug the interaction between these threads you will be able to recognize the the object by it's name.
|
|
|
|
|
I want to have a window creation (through the WinAPI) in the dll. Looks so:
class SX_API SXWindow{
protected:
//Class variables
HINSTANCE hInst;
WNDCLASS WndClass;
HDC hDC;
HWND hWindow;
//User variables
char *szWindowTitle;
SX_BOOL bFullscreen;
SX_UINT nWidth;
SX_UINT nHeight;
SX_UINT nBpp;
public:
static SXWindow* GetMain();
LRESULT CALLBACK MessWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
SX_BOOL SetState(sxEnum State, char *szValue);
SX_BOOL SetState(sxEnum State, int nValue);
void *GetState(sxEnum State);
SX_BOOL Show();
SX_BOOL Create();
SX_BOOL Run();
void Destroy();
SXWindow();
~SXWindow();
};
In the main file the user may only start Create, Show and Run functions.
But here is a problem: when the application starts there is no any window on the screen, but the process is in the memory.
Global function that inserts window class pointer into WL_USERDATA.
When any other message receives it gets the pointer to the class and call class message handler function.
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == WM_CREATE)
{
SetWindowLong(hWnd, GWL_USERDATA, (LONG)(((CREATESTRUCT*)lParam)->lpCreateParams));
return 0;
} else
{
return
((SXWindow*)GetWindowLong(hWnd, GWL_USERDATA))->MessWndProc(hWnd, uMsg, wParam, lParam);
}
}
Here is the window creation function:
SX_BOOL SXWindow::Create()
{
hInst = (HINSTANCE)GetModuleHandle(NULL);
WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
WndClass.lpfnWndProc = (WNDPROC)WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInst;
WndClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(NULL));
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = NULL;
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = "SXWindowClass";
if(!RegisterClass(&WndClass))
{
SXCore::GenError(SX_ERROR_FAILED, "RegisterClass()");
return SX_FALSE;
}
DWORD windowStyle = WS_OVERLAPPEDWINDOW;
DWORD windowExStyle = WS_EX_APPWINDOW;
if(bFullscreen)
{
ShowCursor(FALSE);
windowStyle = WS_POPUP;
windowExStyle |= WS_EX_TOPMOST;
} else
{
RECT WndRect;
WndRect.right = nWidth;
WndRect.bottom = nHeight;
AdjustWindowRectEx (&WndRect, windowStyle, 0, windowExStyle);
windowExStyle |= WS_EX_WINDOWEDGE;
}
hWindow = CreateWindowEx(windowExStyle, "SXWindowClass",
szWindowTitle,
windowStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, nWidth, nHeight,
NULL, NULL, hInst, this);
if(hWindow == NULL)
{
SXCore::GenError(SX_ERROR_FAILED, "CreateWindowEx()");
return SX_FALSE;
}
//Getting window DC
hDC = GetDC(hWindow);
if(hDC == NULL)
{
SXCore::GenError(SX_ERROR_FAILED, "GetDC()");
return SX_FALSE;
}
return SX_TRUE;
}
And other functions:
LRESULT CALLBACK SXWindow::MessWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE: PostQuitMessage(0); break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
SX_BOOL SXWindow::Show()
{
if(hWindow != NULL)
{
ShowWindow(hWindow, SW_SHOW);
return SX_TRUE;
}
return SX_FALSE;
}
SX_BOOL SXWindow::Run()
{
MSG msg;
BOOL done = FALSE;
while(!done)
{
GetMessage(&msg, NULL, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return SX_TRUE;
}
The main file:
#include <windows.h>
#include <sxcore.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
SXCore::GetMainWindow()->SetState(SX_WINDOW_TITLE, "TestApp");
SXCore::GetMainWindow()->SetState(SX_WINDOW_FULLSCREEN, SX_FALSE);
SXCore::GetMainWindow()->Create();
SXCore::GetMainWindow()->Show();
SXCore::GetMainWindow()->Run();
SXCore::GetMainWindow()->Destroy();
return 0;
}
There are no any error messages, but there is no window on the screen. Where the error can be ?
|
|
|
|
|
Hi,
I'm having a problem opening a file. GetFileName() doesn't seem to give me a name. If I open a file, the dialog appears, I select a file and my code returns a blank message box and then a messagebox with no error happened.
I'm kinda new to all this MFC stuff (and my C++ skill isn't that good) so I really don't know what's wrong.
if( FileDlg().DoModal() == IDOK )
{
CFileException ex;
if( f.Open(FileDlg().GetFileName(), CFile::modeRead) == FALSE )
{
AfxMessageBox(FileDlg().GetFileName());
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError);
return;
}
AfxMessageBox("OK");
CArchive test(&f, CArchive::load);
CLoader2Doc::Serialize(test);
}
else
return;
f.Close();
|
|
|
|
|
How is FileDlg() declared? Unless it's a reference to an existing file dialog object, I doubt it's going to give you the results you expect. It sounds like you're getting a freshly constructed CFileDialog object each time you call FileDlg() , which would return a blank filename.
The conventional way to do this sort of thing is as follows:
CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK) {
try {
CFile f(dlg.GetPathName(),CFile::modeRead);
while (f.Read(...)) {
}
}
catch (CFileException *e) {
e->ReportError();
e->Delete();
}
} Some notes on the snippet of code:
- You can add arguments to the CFileDialog constructor to specify the default file extension, filename, and so on.
- Embedding all of the file operations in the try/catch block lets you handle any problems with the file access in one place.
- The two-argument constructor for the CFile object opens the file. The CFile destructor will automatically close the file when the f object goes out of scope.
- The ReportError() method provided by the CFileException object displays an appropriate error message for you. The nice thing here is, you get the standard Windows error messages, appropriate to the type of error, and they're in the language of the user.
Software Zen: delete this;
|
|
|
|
|
Thx a lot! I did indeed forgot to declare it, stupid me
|
|
|
|
|
how can I hide a file but without making it's attributes to invisible.
gabby
|
|
|
|
|
|
use fantasy...
- put in on a http server, download it from there on demand?
- put it into a resource dll, load the dll on demand and get the data?
- recreate file as JPG which has your data as payload in the JPG-comment section?
But sounds all like script kiddie solutions, I would simply do NOT hide the file and encrypt it... or tell your g/f to not use your login.
|
|
|
|
|
The EN_CHANGE notification message is sent when the user has taken an action that may have altered text in an edit control. Unlike the EN_UPDATE notification message, this notification message is sent after the system updates the screen. The parent window of the edit control receives this notification message through a WM_COMMAND message.
This is written in the MSDN.
so i 've made a callback function in my w32 api for the dialog that contains the Edit Control like this:
long FAR PASCAL fereastra1(HWND hdlg1,WORD wmessage1,WPARAM wparam1,LPARAM lparam1)
{
switch(wmessage1)
{
case WM_COMMAND:
switch (wparam1)
{
case IDC_LIST1:
switch(lparam1)
{
case 0x0300:
MessageBox(NULL,"modified","HGabby",MB_OK);
break;
case EN_UPDATE:
MessageBox(NULL,"modified","HGabby",MB_OK);
break;
}
case IDOK:
EndDialog(hdlg1,0);
break;
}
break;
}
return 0;
}
can you tell me please how should I handle this problem
When I do it in mfc it works. I mean each time I modifi the edit box a message box appears. I want to do it in a w32 api. so please help me
thankx
gabby
|
|
|
|
|
Add one more break to your code.
It should work now.
long FAR PASCAL fereastra1(HWND hdlg1,WORD wmessage1,WPARAM wparam1,LPARAM lparam1)
{
switch(wmessage1)
{
case WM_COMMAND:
{
switch (wparam1)
{
case IDC_LIST1:
{
switch(lparam1)
{
case 0x0300:
MessageBox(NULL,"modified","HGabby",MB_OK);
break;
case EN_UPDATE:
MessageBox(NULL,"modified","HGabby",MB_OK);
break;
}
}
break;
case IDOK:
EndDialog(hdlg1,0);
break;
}
break;
}
return 0;
}
|
|
|
|
|
This is a working code:
LRESULT CALLBACK wndproc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
else if(LOWORD(wParam) == IDC_EDIT1)
{
switch(HIWORD(wParam))
{
case EN_CHANGE:
MessageBox(NULL,"EN_CHANGE","Edit Notify",MB_OK);
return TRUE;
case EN_UPDATE:
MessageBox(NULL,"EN_UPDATE","Edit Notify",MB_OK);
return TRUE;
}
}
break;
}
return FALSE;
}
____________________
A.M.
|
|
|
|
|
I have written an NT service that starts automatically when the system boots. It seems to work fine until I set the machine to go into stand-by mode after a specified period of time and eventually power down the monitor and disk drives after an additional specified time.
I have had successful test runs of 5+ hours without any apparent difficulty but when I changed the settings to the ones mentioned above I get an access violation in MSVCRT.DLL (not my dll). The information message gives me an instruction address in the MSVCRT.DLL and some target address saying that the memory could not be written.
Although the service still indicates that it is running, in the Administrative Tools ==> Services dialog, it is dead in the water, as indicated by an application progress log. Additionally I cannot attach a debugger to it. Not being able to attach a debugger to the process limits the definitive information I can gather about this point of failure.
What I suspect is happening is that the monitor powers down due to prolonged inactivity. I do not think the disks are also powered down at this point because the service is still running or if the service was suspended when the system went into stand-by mode some specified time before the monitor power down is initiated. In either case the disks would power down after the monitor because inactivity would have occured in the monitor some time before the system actually went into stand-by mode or prolonged disk inactivity. From here I am just guessing but it seems, from the name of the dll, like the service is trying to access a buffer associated with the monitor which has been freed when the monitor initiated a power down or power up sequence.
The file version of MSVCRT.DLL is 7.0.2600.2180 (xpsp_sp2_rtm.040803-2158).
My question is if anyone knows of a way to get signeled when the machine makes the transition to/from stand-by or hibernate modes, and when the monitor or disks initiate a power down or power up sequence.
If the process can be signaled then it can be put into a paused state until the machine and\or monitor and disks wake up again and should be able to continue processing.
|
|
|
|
|
i think you are looking for winlogon notification package.
there is very good article over it in CP,search Winlogon at CP
-----------------------------
"I Think It Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|
|
Thank you for the response. It made interesting reading for future reference but Winlogon.exe doesn't expose the events I am looking for.
I need to find a similar program that exposes system transitions to/from stand-by and hibernate, as well as the monitor and disks powering down and up again.
|
|
|
|
|