Solved

Defining C's NULL value in Delphi

Posted on 1998-09-10
27
248 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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