Solved

Defining C's NULL value in Delphi

Posted on 1998-09-10
27
244 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:EBuffey
  • 8
  • 7
  • 5
  • +3
27 Comments
 
LVL 1

Expert Comment

by:Greedy
ID: 1339289
#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
 
LVL 1

Expert Comment

by:Greedy
ID: 1339290
I reread your question :) ...you need just need to pass nil and you'll be fine
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1339291
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1339292
Sorry Nicklaus Wirth for the "stong typed ...", should of course be "STRONG typed ..."!

/// John
0
 

Author Comment

by:EBuffey
ID: 1339293
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1339294
What is the function? Do you have the .h-file? What does the C-prototype look like?

/// John
0
 

Author Comment

by:EBuffey
ID: 1339295
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1339296
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1339297
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
 

Author Comment

by:EBuffey
ID: 1339298
I had thought that I had got the directive correct as STDCALL but maybe not... I'll have to check again tomorrow...
0
 
LVL 1

Expert Comment

by:lowlevel
ID: 1339299
have you tried the keyword "nil"? Works for me every time..
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1339300
Lowlevel: What do you mean, I don't understand?!

/// John
0
 

Author Comment

by:EBuffey
ID: 1339301
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:Motaz
ID: 1339302
You can use Nil.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1339303
Wow...

That's an idea to give points to the powerranger
0
 

Author Comment

by:EBuffey
ID: 1339304
Sorry, but I'm only giving the points to one of the original responders.
0
 
LVL 1

Expert Comment

by:Greedy
ID: 1339305
You want to answer this one erajoj...your comments were more complete than mine :)

0
 
LVL 4

Expert Comment

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

/// John
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1339307
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
 

Author Comment

by:EBuffey
ID: 1339308
LOL! Ok, if nobody wants these points... How do I withdraw the question? ;-P
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1339309
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
 
LVL 1

Expert Comment

by:Greedy
ID: 1339310
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
 
LVL 1

Expert Comment

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

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1339312
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
 
LVL 3

Accepted Solution

by:
Matvey earned 200 total points
ID: 1339313
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
 

Author Comment

by:EBuffey
ID: 1339314
It's French, Matvey! I live in Olathe, Kansas, USA.

Thanks for everyone's help! :-)
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1339315
It's french, but I'm from Sweden.
(Just studied french for about eight years :-)

/// John
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now