2010-05-07 29 views
0

我试图使用与C++功能WscRegisterForChanges在Windows 7崩溃使用WscRegisterForChanges

文档位于:

http://msdn.microsoft.com/en-us/library/bb432507(v=VS.85).aspx

我的问题是,即使回调正确执行时,代码在回调执行结束时崩溃。

下面是有问题的代码。这是非常简单的,所以我不知道为什么它的崩溃:

 
#include 
#include 
#include 

void SecurityCenterChangeOccurred(void *param) { 
printf("Change occurred!\n"); 
} 

int main() { 
HRESULT result = S_OK; 
HANDLE callbackRegistration = NULL; 

result = WscRegisterForChanges(
    NULL, 
    &callbackRegistration, 
    (LPTHREAD_START_ROUTINE)SecurityCenterChangeOccurred, 
    NULL); 

while(1) { 
    Sleep(100); 
} 

return 0; 
} 

我的调用堆栈看起来像这样,当发生崩溃:

 
> 00faf6e8() 
    [email protected]() + 0x1293 bytes 
    [email protected]@12() + 0x12 bytes 
    [email protected]() + 0x27 bytes 
    [email protected]() + 0x1b bytes 

如果我添加了ExitThread(0);到SecurityCenterChangeOccurred年底,我得到一个错误,下面的跟踪(所以我不认为我应该使用了ExitThread):

 
Unhandled exception at 0x7799852b (ntdll.dll) in WscRegisterForChangesCrash.exe: 0xC000071C: An invalid thread, handle %p, is specified for this operation. Possibly, a threadpool worker thread was specified. 

    [email protected]() + 0x3ca2f bytes 
    [email protected]() + 0x30 bytes 
> WscRegisterForChangesCrash.exe!SecurityCenterChangeOccurred(void * param=0x00000000) Line 8 + 0xa bytes C++ 
    wscapi.dll!WorkItemWrapper() + 0x19 bytes 
    [email protected]() + 0xdf bytes 
    [email protected]() + 0x1293 bytes 
    [email protected]@12() + 0x12 bytes 
    [email protected]() + 0x27 bytes 
    [email protected]() + 0x1b bytes 

有没有人有,为什么这可能发生的任何想法?

要触发崩溃运行程序并打开或关闭防火墙。

回答

0

它看起来像将WINAPI添加到回调修复此问题。

新的呼叫看起来是这样的:

 
void WINAPI SecurityCenterChangeOccurred(void *param) { 
    printf("Change occurred!\n"); 
} 

有人可以告诉我这是为什么有必要吗?

1

这是由于调用约定。 WinApi32函数|回调的定义必须以宏WINAPI或CALLBACK为前提,它基本上告诉编译器关于调用约定 - 将参数的顺序推送到堆栈,其中应该写入返回值,返回给调用者之后的堆栈恢复。

总结起来,CC定义了来电者和被叫者之间的关系