May 04, 2024, 11:03:14 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Pointer to a String in API DECLARE

Started by Steven Picard, January 04, 2008, 02:31:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steven Picard

January 04, 2008, 02:31:30 PM Last Edit: January 04, 2008, 02:36:55 PM by Steven Picard
I am having a bit of a problem with the following declare:

The EBasic Windows.inc file declares this API as follows:
DECLARE IMPORT, _OpenSCManager ALIAS OpenSCManagerA(lpMachineName AS STRING,lpDatabaseName AS STRING,dwDesiredAccess AS INT),INT

I then try passing null strings as follows:
HSCMANAGER = _OpenSCManager(Chr$(0), Chr$(0), SC_MANAGER_ALL_ACCESS)
I've tried passing empty strings ("") but that's not the same thing and doesn't work anyways.

When I change the API as follows in the Windows.inc file as follows:
DECLARE IMPORT, _OpenSCManager ALIAS OpenSCManagerA(lpMachineName AS INT,lpDatabaseName AS INT,dwDesiredAccess AS INT),INT

And then call the function as follows:
HSCMANAGER = _OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS)
It works and I get back a handle.

Obviously my lack of knowledge of EBasic is showing.  What is the proper way of passing a NULL pointer to string as an API parameter?  I don't want to edit the windows.inc file since I must be using the API wrong. 

In C++ this declare would be as follows:

SC_HANDLE WINAPI OpenSCManager(
  __in_opt  LPCTSTR lpMachineName,
  __in_opt  LPCTSTR lpDatabaseName,
  __in      DWORD dwDesiredAccess
);


which would be simplified as:

DWORD OpenSCManager(DWORD lpMachineName, DWORD lpDatabaseName, DWORD dwDesiredAccess);
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);


BTW, for this API I pass NULL's for the first two parameters unless I want access the Service Control Manager on a different computer.



Okay, I saw another post on here with a similar problem.  The DECLARES in the windows.inc file are off and should be as follows:

DECLARE IMPORT, CreateService ALIAS CreateServiceA(hSCManager AS UINT,lpServiceName AS POINTER,lpDisplayName AS POINTER,dwDesiredAccess AS INT,dwServiceType AS INT,dwStartType AS INT,dwErrorControl AS INT,lpBinaryPathName AS POINTER,lpLoadOrderGroup AS POINTER,lpdwTagId AS POINTER,lpDependencies AS POINTER,lp AS POINTER,lpPassword AS POINTER),INT
DECLARE IMPORT, OpenSCManager ALIAS OpenSCManagerA(lpMachineName AS POINTER,lpDatabaseName AS POINTER,dwDesiredAccess AS INT),INT


LarryMc

With the proper declares (as you noticed) you can pass a null string with the "".
That's because EB passes string by reference which means it uses the pointer internally.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Ionic Wind Support Team

Steven is correct, the delcares on many of the functions in windows.inc are wrong.  It was converted from the VB4 declares as a convenience, I've been editing them as I find the wrong ones.

You pass a NULL with the word NULL by the way, not an empty string or CHR$(0) which is something completely different.  And empty string "" still has an address.  Windows API function generally require strings to be passed through a pointer as you have found so you can specify NULL for omitted parameters.
Ionic Wind Support Team

Steven Picard

I tried NULL with the lpMachineName AS STRING format and the compiler complained.  When I use NULL for the lpMachineName AS INT the compiler if fine with that.

Ionic Wind Support Team

My point was lpMachineName is a pointer, not a string as the declare is wrong.

When a parameter is declared as a pointer you can pass either a NULL or a string, as it expects.

the "lp" stands for "long pointer". 

Paul.

Ionic Wind Support Team

Steven Picard


LarryMc

Thanks for clearing that up for me also.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library