RegisterServiceCtrlHandlerEx_ is not a function

Windows specific forum
wb2k
User
User
Posts: 18
Joined: Sun Feb 09, 2014 1:22 am

RegisterServiceCtrlHandlerEx_ is not a function

Post 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
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post 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.
BERESHEIT
wb2k
User
User
Posts: 18
Joined: Sun Feb 09, 2014 1:22 am

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post 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
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post 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")
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
wb2k
User
User
Posts: 18
Joined: Sun Feb 09, 2014 1:22 am

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post by wb2k »

Good idea ts-soft, thanks!

And you are using integer-parameters instead of long-parameters because of 64-bit compiler, right?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: RegisterServiceCtrlHandlerEx_ is not a function

Post by ts-soft »

Yes, is right, the parameters a pointers, so integer or pointer is the right type to support 32 and 64-bit.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply