Page 1 of 1

RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 1:29 am
by wb2k
Hello all,

I want to use the Windows-API-function RegisterServiceCtrlHandlerEx (http://msdn.microsoft.com/en-us/library ... 85%29.aspx) which has the same parameters as RegisterServiceCtrlHandler (http://msdn.microsoft.com/en-us/library ... 85%29.aspx) except the last optional parameter.

However, when using

Code: Select all

hStatus = RegisterServiceCtrlHandlerEx_(#MyService_Name, @Service_CtrlHandler()) 
PureBasic always says: RegisterServiceCtrlHandlerEx_() is not a function, array, macro or linkedlist.
When changing RegisterServiceCtrlHandlerEx_ to RegisterServiceCtrlHandler_ everything is okay.

Any ideas what I am doing wrong here?

Thanks

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 1:54 am
by luis
Hi, not all the API are pre-imported by PB.
That one is missing, you have to use openlibrary() and import the function from Advapi32.dll by yourself.

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 2:26 am
by netmaestro
You have to choose the ascii or unicode version. Sample for ascii:

Code: Select all

Prototype RegisterServiceCtrlHandlerEx( lpservicename.l, lphandlerproc.l, lpcontext.l)

lib = OpenLibrary(#PB_Any, "advapi32.dll")

Global RegisterServiceCtrlHandlerEx_.RegisterServiceCtrlHandlerEx = GetFunction(lib, "RegisterServiceCtrlHandlerExA") ; For ascii version
Use W for the unicode version. With this snippet at the top of your code you can use RegisterServiceCtrlHandlerEx_() seamlessly as though it were already imported.

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 1:51 pm
by wb2k
Thank you very much netmaestro and luis! :D
I wasn't aware that the API-functions have to be pre-imported, so maybe the error-message for unknown functions with underscore at the end should better be "... is not imported or is not a function, array, macro or linkedlist."

The last parameter of RegisterServiceCtrlHandlerEx is optional so I adapted the code a little bit, also extended it with unicode-support:

Code: Select all

Prototype RegisterServiceCtrlHandlerEx( lpservicename.l, lphandlerproc.l, lpcontext.l=0)
lib = OpenLibrary(#PB_Any, "advapi32.dll")
CompilerIf #PB_Compiler_Unicode
  Global RegisterServiceCtrlHandlerEx_.RegisterServiceCtrlHandlerEx = GetFunction(lib, "RegisterServiceCtrlHandlerExW")
CompilerElse
  Global RegisterServiceCtrlHandlerEx_.RegisterServiceCtrlHandlerEx = GetFunction(lib, "RegisterServiceCtrlHandlerExA")
CompilerEndIf

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 2:20 pm
by ts-soft
Here, a bit shorter, with Unicode/ASCII support and x86/x64 support:

Code: Select all

Prototype RegisterServiceCtrlHandlerEx( lpservicename.p-Unicode, lphandlerproc.i, lpcontext.i = 0)
lib = OpenLibrary(#PB_Any, "advapi32.dll")
Global RegisterServiceCtrlHandlerEx_.RegisterServiceCtrlHandlerEx = GetFunction(lib, "RegisterServiceCtrlHandlerExW")

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 7:35 pm
by wb2k
Good idea ts-soft, thanks!

And you are using integer-parameters instead of long-parameters because of 64-bit compiler, right?

Re: RegisterServiceCtrlHandlerEx_ is not a function

Posted: Sun Feb 09, 2014 7:47 pm
by ts-soft
Yes, is right, the parameters a pointers, so integer or pointer is the right type to support 32 and 64-bit.