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.
EBuffeyAsked:
Who is Participating?
 
MatveyConnect With a Mentor Commented:
Cool, I didn't know that :) What language it is anyway?? It's really not in the list of the ones I know - where do you live erajoj and EBuffey? (Sorry, don't remember...). Greedy - you're from the US, right?

Thanks for your help in moving me up in the top 15 list! You're really good friends ;-)

Cheers!
0
 
GreedyCommented:
#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
0
 
GreedyCommented:
I reread your question :) ...you need just need to pass nil and you'll be fine
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
erajojCommented:
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
0
 
erajojCommented:
Sorry Nicklaus Wirth for the "stong typed ...", should of course be "STRONG typed ..."!

/// John
0
 
EBuffeyAuthor Commented:
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.
0
 
erajojCommented:
What is the function? Do you have the .h-file? What does the C-prototype look like?

/// John
0
 
EBuffeyAuthor Commented:
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);

0
 
MatveyCommented:
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
0
 
erajojCommented:
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
0
 
EBuffeyAuthor Commented:
I had thought that I had got the directive correct as STDCALL but maybe not... I'll have to check again tomorrow...
0
 
lowlevelCommented:
have you tried the keyword "nil"? Works for me every time..
0
 
erajojCommented:
Lowlevel: What do you mean, I don't understand?!

/// John
0
 
EBuffeyAuthor Commented:
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?
0
 
MotazCommented:
You can use Nil.
0
 
MatveyCommented:
Wow...

That's an idea to give points to the powerranger
0
 
EBuffeyAuthor Commented:
Sorry, but I'm only giving the points to one of the original responders.
0
 
GreedyCommented:
You want to answer this one erajoj...your comments were more complete than mine :)

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

/// John
0
 
MatveyCommented:
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
0
 
EBuffeyAuthor Commented:
LOL! Ok, if nobody wants these points... How do I withdraw the question? ;-P
0
 
MatveyCommented:
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
0
 
GreedyCommented:
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..?

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

0
 
erajojCommented:
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
0
 
EBuffeyAuthor Commented:
It's French, Matvey! I live in Olathe, Kansas, USA.

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

/// John
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.