|
Use GetCursorPos . Convert screen co-ordinates you will get to client co-ordinates.
|
|
|
|
|
I probably should be avoiding this, knowing that a request for a code sample might follow, but have you tried browsing the MSDN for "GetCursorPos", or anything containing the word "cursor"?
If not, please also try to remember from now on, that the "mouse" in most programming languages is referred to as "cursor".
The mouse is the device controlling the cursor. It's position on your desktop is not known by the system. ;) I.e, Unless you tell it you're left handed, it couldn't care less.
Cheers,
Gabriel
|
|
|
|
|
Hello,
I have created a windows service application in windows XP and VC++.Net.
In this windows service application, i am connecting to MySQL database on the same computer. My application is to get the USB plug-n-plays. I am opening the MySQL db not in the ServiceMain() of the service, rather, since i have to every time check for the database connection, so in case of everyrequirement.. i am opening the database and throwing the exception, on db connection failure.
I can see that, everytime the statement :
usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); is throwing the exception.
in the catch() block i reported the error too, but it is not displaying the error report.
After that, i restarted the system and then i see that, when i follow the debugger, the breakpoint at the usbDB.OpenEx(..) is not getting triggered.
myServiceMain(int arg, char *argv)
{
hStatus=RegisterServiceCtrlHandler(szServiceName, handlerFunc);
SetServiceStatus(hStatus, &ServiceStatus);
initService();
....
....
RegisterDeviceNotification(hStatus),&NotificationFiler,DEVICE_NOTIFY_SERVICE_HANDLE);
....
....
while(ServiceStatus.dwCurrentStatus=SERVICE_RUNNING)
{
/*do my task*/
....
....
}
ServiceStatus.dwCurrentStatus=STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
}
InitService()
{
/* intialisation code for my Service*/
}
I think, in the MyServiceMain(..) the code after the RegisterServiceCtrlHandlerEx(...) is not getting executed. So, the breakpoint kept inside the InitService() is not getting called. Even the breakpoint inside the MyServiceMain() is also not getting called. To trigger the breakpoints in the MyServiceMain() callback function, when i am stopping and restarting or restarting the service from the control panel... the service is not getting stopped ..it is only showing that... "stopping".
I have checked the dsn name, user id and password also... but i don know why, it is not getting connected to my windows service application.
Thank you
Anee
Anee
|
|
|
|
|
anumadhu wrote: myServiceMain(int arg, char *argv)
{
hStatus=RegisterServiceCtrlHandler(szServiceName, handlerFunc);
SetServiceStatus(hStatus, &ServiceStatus);
Are you initializing the contents of ServiceStatus somewhere? Also, if connecting to your database (not shown in the code you included) can take a noticeable period of time, you should do it AFTER you report to the SCM that your service is started.
Do you receive notification in your handlerFunc telling your service to stop? Do you handle those notification?
Judy
|
|
|
|
|
yes, i am initalizing the ServiceStatus in the myServiceMain(int arg, char**argv) itself.
i am connecting MySQL to database using this statement:
try
{
usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog);
}
catch(CException *e)
{
WriteIntoLogFile("db open Exception here");// And my log file is showing this stmt as the last stmt in it.
return;
}
myServiceMain is as follows....
void myServiceMain(int argc, char **argv)
{
int result=-1;
usbObj.WriteIntoLogFile("At DataSentinelMain");
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted =SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwWaitHint =0;
ServiceStatus.dwWin32ExitCode = 0;
hStatus=RegisterServiceCtrlHandlerEx((LPCWSTR)SERVICE_NAME_USB,(LPHANDLER_FUNCTION_EX)ControlHandler, 0);
usbObj.WriteIntoLogFile("Aftr calling RegisterServiceCtrlHandler");
if(hStatus==(SERVICE_STATUS_HANDLE)0)
{
usbObj.WriteIntoLogFile("hstatus returned SERVICE_STATUS_HANDLE INVALID");
return;
}
SetServiceStatus(hStatus,&ServiceStatus);
usbObj.WriteIntoLogFile("before RegisterDeviceNotifications");
result=usbObj.RegisterForDeviceNotifications (hStatus);
ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus(hStatus,&ServiceStatus);
if(result!=1)
{
usbObj.WriteIntoLogFile("RegisterDeviceNotification returned invalid value");
return;
}
usbObj.WriteIntoLogFile("After the call of RegisterDeviceNotification");
InitService();//27March 07
usbObj.WriteIntoLogFile("After the call to InitService");
/* Enumerate the USB DEVICES */
result=0;
while(ServiceStatus.dwCurrentState = SERVICE_RUNNING)
{
result++;
usbObj.WriteIntoLogFile("Call to USBServiceFunc from While of ServiceStatus_running");
usbObj.USBService ();
usbObj.WriteIntoLogFile("after and end of USBServiceFunc");
if(result>100)
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
return;
}
Sleep(5000);
}
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
return;
}
usbObj.USBService ()
{
/*this contains a method called usbObj.OpenDevices()..where the db is opened using the above stms..*/
}
i have seen even the MySQL.. service also.. which is getting started before the start of my own service.
but still i am unable to connect to the MySQL db at my local machine.
any idea...pls?
Anee
|
|
|
|
|
Just looking at the code, I don't see anything wrong. You'll have to use the debugger and see where it's hanging up. Put a call to DebugBreak at the start of your ServiceMain - this will force a breakpoint to occur and you can then use VS to debug it.
Judy
|
|
|
|
|
Thanks Judy!
Yeah, i have even debugged this service in following way:
1) first using the exe, i created the service :
sc create MyService binpath= D:\MyService.exe
2) Then started it from the control pannel. After it shows the status as "Started". I go to step 3.
3) Then using the Visual Studio (my development environment) + Debug + Attach Process + MyService.exe+attach.
4) And kept breakpoints in the starting of the MyServiceMain() and at the while() and also at the controlHandler function.
void ControlHandler(DWORD dwServiceCtrlEvent, DWORD dwEventType,
LPVOID lpEventData, LPVOID lpContext)
{
switch(dwServiceCtrlEvent)
{
case SERVICE_CONTROL_DEVICEEVENT:
usbObj.OnMyDeviceEvent(dwEventType,lpEventData);
break;
case SERVICE_CONTROL_STOP:
usbObj.UnRegisterEvents();
ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;//SERVICE_STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
break;
case SERVICE_CONTROL_PAUSE:
break;
case SERVICE_CONTROL_CONTINUE:
break;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
SetServiceStatus(hStatus,&ServiceStatus);
}
When i connect the USB device, then only the breakpoint is getting triggered and that too at the controlHandler()'s SERVICE_CONTROL_DEVICEEVENT. But never in or at the beginning at the MyServiceMain().
That is why, i used a WriteIntoLogFile() to get the required information regarding the order of execution of lines in my application.
Are you sure.. that my code have no errors?? Then i am bit worried..abt the cause of problem.
Thanks in advance..
Anee
|
|
|
|
|
It sounds like the problem is at the beginning. Setting a breakpoint at the beginning of ServiceMain _after_ the service is started won't do you any good since that code has already executed. You need to use the DebugBreak function at the very start of your ServiceMain. It will force a breakpoint and the OS will allow you to select a debugger to attach to the process - in this case your service. You can then debug your startup logic which, by your description, is where your problem is.
I don't know that your code doesn't have any errors. In a visual inspection, I don't see one but I don't know what your problem domain is and your thinking about how your code solves that problem. I don't have your whole program nor do I want you to post it. Your problem is at the beginning - you need to debug that, conparing your intended logic flow with what your code actually does. The only way to debug the beginning of a service is by using the DebugBreak .... or coding an infinite loop and attaching the debugger as you are already doing and then breaking the loop. Use DebugBreak
Judy
|
|
|
|
|
Thanks Judy!
I kept DebugBreak() in the MyServiceMain()... and when i traversed throughout the debug.. the problem was same what i told before.. the
usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog);
is not getting opened and therefore the catch block is getting executed. Whereas i want the database to be connected.
I don know... how to solve..lets hope for the any other right way.
Thanks in Advance
Anee
|
|
|
|
|
That I can't help with - databases are not my thing. At least now you know that it is exclusively a database problem and not a service problem. Make sure you step into the Open and trace it as far into the guts of the provided source code as you can. It might give some help on finding the problem.
One thing to be aware of when running as a service is your permissions. You are not a "normal" program. You are running in the Local System account. You haven't mentioned which OS this is on. XP is usually pretty good about letting services do all the usual things (excepting UI). Vista is much more restrictive.
Good luck!!
Judy
|
|
|
|
|
Great Thanks for helping me out in fixing the exact problem area.
And now moving towards the solution part, i am using the process explorer to get the exact information of the services and processes and their threads,DLLs etc.
After placing DebugBreak()... in the catch block of :
try{
DebugBreak();
usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog);
}
catch(CException *e)
{
WriteIntoLogFile("db open Exception in the User_USB_login_data");
DebugBreak();//Here the breakpoint is getting triggered at last. and the error window is showing like this:
}
Error window displays:
Windows has triggered a breakpoint in MyService.exe.
This may be due to a corruption of the heap, and indicates a bug in MyService.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information
.............................
my development environment is Windows XP and VC++.Net 2005
Any ideas..pls!
Thanks in Advance
Anee
|
|
|
|
|
You should be hitting the DebugBreak in the try section. When the Windows message appears, there should be a "debug" option / button somewhere in the window. If you select that, you should get a window from the Visual Studio Just-In-Time debugger listing the possible debuggers to use. Select Visual Studion 2005 - if you have your service project already open, it should go right to the line with the DebugBreak. Use "set next statement" to skip to the next line of code. Is this not happening? To check the obvious, you are running a debug build of your service, yes? Make sure Just-In-Time debugging is enabled (Tools -> OPtions -> Debugging).
Judy
|
|
|
|
|
|
Hello,
After debugging what i found is that, the CDBException at the following statement raises an exception:
cdb.openEx(szConnectstring, CDatabase::noODBCDialog)
And the message throwed is: "Data source name not found and no default driver specified"
Whereas when i check for this dsn's status..from control panel the test connection is showing success. And also i am using the same dsn and same database from other VC++.Net2005 application ( this is not a windows service application), this application is getting connected to MySQL database using the same dsn and also able to read the records from one of the tables there(which shown in the applications UI window). I am not creating the windows service using the .Net framework. I am creating the my windows service application using the winsvc.h and advapi32.dll.
I guess, since the application from which i am trying to connect to MySQL database is a windows service application, so probably causing some problem (due to some hidden windows internals..issues).. which i am unable to fix...
Can anybody.. help me out by giving the clues..
Thank you..
Anee
|
|
|
|
|
Thanks to all specially to JudyL_FL.. who tried to help me out in fixing my problem.
And there is a good news, the problem got fixed.... .
Actually i am not sure that whether i m right or not in my analysis.
What i did to solve this:
I just created System DSN now and everything was same. And it worked fine, without any problem. Before this i using User DSN.
The cause of problem would be: What i guess is, since the windows service which i am developing is a system service so it is failing to open MySQL db through the User DSN. Am i right? or is there anything else which is causing the problem...
Thanks in advance.
Anee
|
|
|
|
|
in some program i always see "#pragma pack(1)" included in Header. what is the role of #pragma pack(1).
Thanks
Monica
|
|
|
|
|
mo_nica881 wrote: what is the role of #pragma pack(1).
Specifies packing alignment (e.g., 1 byte) for structure and union members.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
It forces structures member alignment to 1 byte. By default, the members of the structure are aligned on 8 bytes boundaries. With this option, you force the members to be aligned on 1 byte boundaries (so it makes your structure more compact in memory but less efficient for read/write operations because some members will need to operations to be updated).
More info here[^]
|
|
|
|
|
mo_nica881 wrote: in some program i always see "#pragma pack(1)" included in Header. what is the role of #pragma pack(1).
It's useful to disable structure member alignment. Value of 1 disables any alignment...
For eg:
struct aligntest
{
char aChar;
int nNum;
};
Normally sizeof( aligntest ) will return 8 but the actual size is 5. Extra 3 bytes is used for padding. This is done for efficiency sake.
This could be a problem if you are using functions like memcpy to copy native type values into structures for splitting them up and you want each part to be named...
For eg:
#pragma pack( push, 1 ) // Set alignment to 1
struct IntParts
{
BYTE byPart1;
BYTE byPart2;
BYTE byPart3;
BYTE byPart4;
};
#pragma pack( pop ) // Restore previous settings
int nSplit = 123213;
IntPart ip = { 0 };
memcpy( &ip, &nSplit, sizeof( ip ));
The above operation will be dangerous with padding since sizeof of int is 4 and the sizeof the structure should also be 4, hence you have to disable padding using #pragma pack( push, 1 ) and when you are done with the setting write another #pragma pack( pop ) which resets the setting.
You should not use 1 alignment always except for above reasons.
|
|
|
|
|
|
sk8er_boy287 wrote: I'm pretty much sure (from experience) that sizeof always returns the size of the type of structure (5 for your code sample). sizeof doesn't care about memory alignment.
Wrong. Test and see.
|
|
|
|
|
sk8er_boy287 wrote: ...(5 for your code sample).
Only if #pragma pack(1) is present. By default, it will have a size of 8.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I've just tested and you're right. I've always trusted sizeof to give me the right size of any type of structure. I find this behavior kind of weird.
However, I've also tested with arrays and the size of a single element of an array includes the padding. So there's no problem there, other than a lot of useless padding for large arrays.
I wonder what this code would do (try to copy 8 bytes into 3 ) :
#pragma pack( push, 1 ) // Set alignment to 1
struct IntParts1
{
BYTE byPart1;
BYTE byPart2;
BYTE byPart3;
};
#pragma pack( pop ) // Restore previous settings
struct IntParts2
{
BYTE byPart1;
BYTE byPart2;
BYTE byPart3;
};
IntParts1 ip1 = {0, 1, 2};
IntParts2 ip2 = {0, 1, 2};
memcpy( &ip1, &ip2, sizeof( ip1 ));
|
|
|
|
|
sk8er_boy287 wrote: I wonder what this code would do (try to copy 8 bytes into 3) :
In your example, both ip1 and ip2 are the same size (3 bytes).
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Can anyone explain that? Shouldn't the second structure be allocated using the default 8-byte alignment?
Here's another example which should copy 8 bytes into 5:
#pragma pack( push, 1 ) // Set alignment to 1
struct IntParts1
{
char byPart1;
int byPart2;
};
#pragma pack( pop ) // Restore previous settings
struct IntParts2
{
char byPart1;
int byPart2;
};
IntParts1 ip1 = {0};
IntParts2 ip2 = {0};
memcpy( &ip1, &ip2, sizeof( ip1 ) );
printf( "%d, %d\n", sizeof( IntParts1 ), sizeof( IntParts2 ) );
And don't tell me they're both 5 bytes in size because i tested this time!
|
|
|
|
|