Link to home
Start Free TrialLog in
Avatar of EBuffey
EBuffey

asked on

Defining C's NULL value in Delphi

I need to pass a NULL value (The definition of NULL used in C) to a function in a DLL.

How do you define NULL in Delphi?

I'm getting all kinds of disastrous results (Delphi going bye-bye, etc.) in all my attempts. I currently have the parameter defined as a Pointer.
Avatar of Greedy
Greedy

#0 is the null chariter '' is also a null...like for the end of a string in C

do you mean nil like for a pointer?...nil is defined in delphi and is a reserved word for a pointer that points to nowere...

what are you doing? is it for a C based DLL
I reread your question :) ...you need just need to pass nil and you'll be fine
Hi,
NULL in 32-bit C/C++ is the 32-bit value $00000000 (0x00000000).
What that means is that it goes for zero values, FALSE values, null-pointers etc...
In Delphi there is no exact equal to it since Delphi uses Object Pascal, which is a stong typed language.
In Delphi you have to adapt to every situation with different types, explicitly. If the actual parameter is Integer/UINT/Cardinal/DWORD etc., you must use 0 (zero, $00000000). If it is a pointer/PChar etc., you will have to use nil. And if the parameter is a BOOL/Boolean, you must use False.
All these are essentially equal, but Delphi doesn't want you to mix apples and pears, just to keep you on the safe side.
You can also type these:

  Integer( False ) => 0
  Boolean( nil )   => False
  Pointer( 0 )     => nil
  Pointer( False ) => nil

Etc...
Hope this helps you.

/// John
Sorry Nicklaus Wirth for the "stong typed ...", should of course be "STRONG typed ..."!

/// John
Avatar of EBuffey

ASKER

Ok, Nil seems to be the concensus...

I successfully call this C function 185 times with Value1 for the first parameter. Then I call it with Value2 for the first parameter. Each of these calls, I'm passing Nil for the fourth parameter. After the final call, I check the return value. When I press F8 on this If statement, Delphi goes into never-never land.

I suppose I need to reinstal Delphi?

I'm using Delphi 2.01, BTW.
What is the function? Do you have the .h-file? What does the C-prototype look like?

/// John
Avatar of EBuffey

ASKER

Ok, here's the stuff from the .H file:

typedef size_t NsSizeT;
typedef Word NetWord;
typedef NetWord SrSts;
typedef NetWord ServiceCode;
SrSts NsInvokeService(ServiceCode       serviceCode,
                      const void        *pServiceParameters,
                      NsSizeT           serviceParameterLen,
                      void             *pResultData,
                      NsSizeT           resultLen);

You can try different directives after the call definition, like:

function NsInvokeService(...):...; cdecl;//pascal;//register;//safecall;//stdcall;

Most of them will give you the emediate visible result of Delphi going kookoo, byebye etc. One or two might work good, but I supose only one will really do. I haven't really got deep into the meaning of each one - maybe someone can give some table with the meaning...

--Matvey
EBuffey: If calling conventions isn't it, please provide more info. Your Delphi prototype, your calling proc, help about the func etc...

Matvey (this is taken from the help file):

Parameters are transferred to procedures and functions via CPU registers and/or the stack, depending on the calling conventions of the procedures and functions. Delphi supports five calling conventions: register, cdecl, pascal, stdcall and safecall.
The register and pascal conventions pass parameters from left to right, that is the leftmost parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last. The cdecl, stdcall and safecall conventions pass parameters from right to left. For all conventions except cdecl, the procedure or function removes parameters from the stack upon returning. With the cdecl convention, the caller must remove parameters from the stack when the call returns. The register convention uses up to three CPU registers to pass parameters, whereas the other conventions always pass all parameters on the stack.

The default calling convention is ALWAYS REGISTER.

The register convention is by far the most efficient, since it often avoids the creation of a stack frame. The pascal and cdecl conventions are mostly useful for calling routines in dynamic-link libraries written in C, C++, or other languages. The stdcall and safecall conventions are used for calling Windows API routines. Interfaces intended to be used between modules, in particular modules written in different programming languages, should declare all methods with stdcall. The safecall convention must be used to implement dual interfaces.

The calling conventions are summarized in the following table.

Convention      Order      Cleanup      Registers
register      Left-to-right      Function      Yes
pascal      Left-to-right      Function      No
cdecl      Right-to-left      Caller      No
stdcall      Right-to-left      Function      No
safecall      Right-to-left      Function      No

/// John
Avatar of EBuffey

ASKER

I had thought that I had got the directive correct as STDCALL but maybe not... I'll have to check again tomorrow...
have you tried the keyword "nil"? Works for me every time..
Lowlevel: What do you mean, I don't understand?!

/// John
Avatar of EBuffey

ASKER

Ok, Nil seems to be the acceptable equivalent for C's NULL.

Also, I had been using STDCALL. CDECL seems to be keep Delphi from going to never-never land. BTW, I don't believe Delphi 2.01 supports SAFECALL. At least it's not in the help files.

Anyway, thanks for all your help! It appears to be working great now! Who gets the 200 points?
You can use Nil.
Wow...

That's an idea to give points to the powerranger
Avatar of EBuffey

ASKER

Sorry, but I'm only giving the points to one of the original responders.
You want to answer this one erajoj...your comments were more complete than mine :)

Interesting concept, to use cdecl as an export from a DLL.
Glad to hear it seems to have worked out.

/// John
You wanted me to answere the question???

Anyway, what does this mean?

"Ed "Le Rêveur" Buffey
Mai tous vos rêves soient doux!"

c u, Matvey
Avatar of EBuffey

ASKER

LOL! Ok, if nobody wants these points... How do I withdraw the question? ;-P
Well, you can'y delete it, because it was already answered once. If it stays IDLE for a week or two it will be deleted also.

If you still want someone to get the points, then say, like: "Motaz, please answere again, I want to give you the points" (or something alike).

c u, Matvey
Screw that Motaz doesn't deserve to lick the hair off a lama's ass.  I'll answer the question tonight unless you just want it to die or something..?

Matvey should take the points so he can move up on the top 15 list! :)

Give it to Matvey and stop whimping around! :)

Matvey, answer the question!
BTW, "mai tous vos rêves soient doux" means "may all your dreams be soft/pleasant", nice huh?

Ed, give good grades! You got good answers.

/// John
ASKER CERTIFIED SOLUTION
Avatar of Matvey
Matvey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EBuffey

ASKER

It's French, Matvey! I live in Olathe, Kansas, USA.

Thanks for everyone's help! :-)
It's french, but I'm from Sweden.
(Just studied french for about eight years :-)

/// John