BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Exception "Bad Application Handle" - why?
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Jens Kretzschmar
Guest





PostPosted: Mon Dec 15, 2003 4:49 pm    Post subject: Exception "Bad Application Handle" - why? Reply with quote



Hello,

I have one EXE and one DLL. From within the EXE I am calling two different
functions of the DLL - with the same code. Well, of course the handed over
parameters differ. But one call works the other does not. It returns a "Bad
Application Handle" Exception while I can call the other function of the
DLL with the _same_ Handle without problems. If I compile the DLL as an EXE
it runs without Problems and Memory Leaks.

Has anybody seen this behaviour? Can someone imagine a cause for this
problem. Sounds more like a problem of the abstract type... :-(

D7 Ent., Win XP

Thanks for those daring to answer! ;-)

Jens
Back to top
J French
Guest





PostPosted: Mon Dec 15, 2003 6:24 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote



On Mon, 15 Dec 2003 17:49:56 +0100, Jens Kretzschmar
<newsposting (AT) 2andfro (DOT) de> wrote:

Quote:
Hello,

I have one EXE and one DLL. From within the EXE I am calling two different
functions of the DLL - with the same code. Well, of course the handed over
parameters differ. But one call works the other does not. It returns a "Bad
Application Handle" Exception while I can call the other function of the
DLL with the _same_ Handle without problems. If I compile the DLL as an EXE
it runs without Problems and Memory Leaks.

Has anybody seen this behaviour? Can someone imagine a cause for this
problem. Sounds more like a problem of the abstract type... :-(

D7 Ent., Win XP

Thanks for those daring to answer! Wink

Sounds like faulty parameters in the 'errant' DLL's calling routine

Back to top
David Reeve
Guest





PostPosted: Tue Dec 16, 2003 1:28 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote



"Jens Kretzschmar" <newsposting (AT) 2andfro (DOT) de> wrote

Quote:
Hello,

I have one EXE and one DLL. From within the EXE I am calling two different
functions of the DLL - with the same code. Well, of course the handed over
parameters differ. But one call works the other does not. It returns a
"Bad
Application Handle" Exception while I can call the other function of the
DLL with the _same_ Handle without problems. If I compile the DLL as an
EXE
it runs without Problems and Memory Leaks.

Has anybody seen this behaviour? Can someone imagine a cause for this
problem. Sounds more like a problem of the abstract type... :-(

D7 Ent., Win XP

Thanks for those daring to answer! ;-)

Jens

Sounds like a problem of confusion between the Application instance of the
exe and the Application instance of the dll. The application handle of the
dll is normally nil, and I assume you are passing the handle of the main
application through for use by the dll. I take it you are
assigning the handle you pass to Application.Handle within the dll ? This
should be without hazard. Can you post more details?

Dave




Back to top
Terry Russell
Guest





PostPosted: Tue Dec 16, 2003 2:01 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

"Jens Kretzschmar" <newsposting (AT) 2andfro (DOT) de> wrote

Quote:
Hello,

I have one EXE and one DLL. From within the EXE I am calling two different
functions of the DLL - with the same code. Well, of course the handed over
parameters differ. But one call works the other does not. It returns a
"Bad
Application Handle" Exception while I can call the other function of the
DLL with the _same_ Handle without problems. If I compile the DLL as an
EXE
it runs without Problems and Memory Leaks.

Has anybody seen this behaviour? Can someone imagine a cause for this
problem. Sounds more like a problem of the abstract type... Sad

Presumably 'same code' means the caller not the dll.
check

function call method/ function declaration model

parameter types, parameters passed, parameters as function sees them
( related to declaration and may be difference between exe and dll)





Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 10:09 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
Sounds like faulty parameters in the 'errant' DLL's calling routine

Hi,

that seems to be it. I built a new unit with only the called routine
(without the functions it calls internally) and it threw the exception. I
took out all parameters and it works. Strange enough as there are only
simple parameters.

When I started programming I learned "Do not pass over strings, use PChar;
do not use Boolean, use WordBool, ..." and so on. Did that change in D7 or
WinXP?

Thanks for your help already!

Jens

Back to top
Maarten Wiltink
Guest





PostPosted: Tue Dec 16, 2003 10:23 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

"Jens Kretzschmar" <newsposting (AT) 2andfro (DOT) de> wrote

[...]
Quote:
When I started programming I learned "Do not pass over strings, use
PChar; do not use Boolean, use WordBool, ..." and so on. Did that
change in D7 or WinXP?

That's for when the DLL is provided by Windows; when the DLL is your
own and written in Delphi, you can (and should) stick to the language's
own types. There's just the heap issue which is sidestepped by using
ShareMem.

The most common mistake remains calling conventions disparities.

Groetjes,
Maarten Wiltink



Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 10:55 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
The most common mistake remains calling conventions disparities.

Yes, this was it. I know it would be something incredibly stupid: In the
declaration of the called routine in the EXE, the "stdcall" was missing. So
parameters were passed Left-to-right ("register") and received
Right-to-left. Crash.

As long as there was only the Application Handle as Parameter everything
was fine, with Application Handle and "WordBool" it still worked but
(strange ?!) with three parameters (Handle, Word, WordBool) it crashed.

This took me three days and I still don't understand why the version with
three parameters crashed (although it is symmetric) and why I got an
exception where String Pointers are freed.... ????

Thanks to all of you, thanks a lot!

Jens

Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 10:58 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
check

function call method/ function declaration model

parameter types, parameters passed, parameters as function sees them
( related to declaration and may be difference between exe and dll)

Yes, that was it. In the declaration of the called routine in the EXE a
"stdcall" was missing. I wrote more in my answer to Maarten Wiltink's
posting.

Thanks a lot for your help!

Jens

Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 11:00 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Thanks for your help, Dave. It was an incredibly stupid error - see my
posting in reply to Maarten Wiltink's posting.

Thanks again,

Jens
Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 11:10 am    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
As long as there was only the Application Handle as Parameter everything
was fine, with Application Handle and "WordBool" it still worked but
(strange ?!) with three parameters (Handle, Word, WordBool) it crashed.

Just to understand it and for learning: Does one also have to count the
return parameter?

(Handle): LongInt - works (value definitions alike)
(Handle, WordBool): LongInt - works (symmetrical)
(Handle, Word, WordBool): LongInt - crashes (asymmetrical)

Back to top
J French
Guest





PostPosted: Tue Dec 16, 2003 12:24 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

On Tue, 16 Dec 2003 11:09:35 +0100, Jens Kretzschmar
<newsposting (AT) 2andfro (DOT) de> wrote:

Quote:
Sounds like faulty parameters in the 'errant' DLL's calling routine

Hi,

that seems to be it. I built a new unit with only the called routine
(without the functions it calls internally) and it threw the exception. I
took out all parameters and it works. Strange enough as there are only
simple parameters.

When I started programming I learned "Do not pass over strings, use PChar;
do not use Boolean, use WordBool, ..." and so on. Did that change in D7 or
WinXP?

Thanks for your help already!

As Maarten has pointed out, you can pass Strings to/from DLLs provided
you use ShareMem
- basically this involves *another* DLL

The problem is that memory manager within the DLL is (normally)
totally different from the memory manager in the calling EXE
- ie: two Memory Managers

Personally I am not that keen on DLLs being 'tied' to the EXEs

From messing around I have found that Delphi Strings *can* be safely
passed to DLLs
- but one should never modify their length
- the best thing is to copy them explicitly (byte by byte)
- and for safety have them declared as Const

Under StdCall a Const String is passed as a pointer to the first byte
of its data eg: @S[1] is put on the stack
The preceding 4 bytes eg: S[-3] to S[0] contain the length of the
String
- simply declaring the parameter as: Const S :String gives you a
Delphi ANSI String

Strings can be returned from DLL Functions (or as Vars) by using
OleVariants that actually contain a String

Although potentially dangerous, it is very convenient having 'clean'
calling parameters for the DLL
- even if it involves a bit more work inside the DLL

Hence one does not need to use the PChar and 'fill a buffer' approach
- ie: the Win API approach
- when writing DLLs that can be called from Delphi or VB

This is useful if one is mixing different versions of Delphi, or
calling Delphi from VB - also calling VB from Delphi

Some might say that it is foolish worrying about not being able to
send #0 in a string to a DLL without sending a buffer length
- I don't agree

- DLLs are supposed to 'wrap' problems and provide a very clean user
interface, a bit more work within the DLL to make it simple to use, is
a small investment.

HTH



Back to top
J French
Guest





PostPosted: Tue Dec 16, 2003 12:32 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

On Tue, 16 Dec 2003 11:55:55 +0100, Jens Kretzschmar
<newsposting (AT) 2andfro (DOT) de> wrote:

Quote:
The most common mistake remains calling conventions disparities.

Yes, this was it. I know it would be something incredibly stupid: In the
declaration of the called routine in the EXE, the "stdcall" was missing. So
parameters were passed Left-to-right ("register") and received
Right-to-left. Crash.

One other thing
Normally I write and compile a *very* simple EXE to act as a testbed
for the DLL I am working on.

Then set that up as the Host Application under Run/Parameters

I then debug the DLL - not the App
It normally shows up any parameter problems pretty fast

Also, I am inclined to use 4 byte Integers instead of Booleans for
returning 'Boolean-ish results'
- there are too many types of 'Boolean' knocking around ...

Back to top
AlanGLLoyd
Guest





PostPosted: Tue Dec 16, 2003 1:13 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

In article <3fdef9da.14213415 (AT) news (DOT) btclick.com>, [email]erewhon (AT) nowhere (DOT) com[/email] (J French)
writes:

Quote:
Also, I am inclined to use 4 byte Integers instead of Booleans for
returning 'Boolean-ish results'
- there are too many types of 'Boolean' knocking around ...


That's a LongBool in Delphi - see Delphi help on "Boolean type".

Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]

Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 1:15 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
ecx, edx. But - what's more important: With 'stdcall', the callee is meant
to clear the stack...and when no stack is used by caller, this will cause
exceptions. What will possibly happen is that the exception occurs at some
later point in the calling routine, as the stack now may be "too short" Wink

That would explain why I get an exception at some point where strings are
freed. Thanks!

Quote:
I'll not spend any money on American Software products
until armed forces are out of Iraq.
..
PS.: Is someone gonna get those guys out of there soon ?
I need to upgrade to Delphi 7 !!!

Why don't you wait for Delphi 8? Delphi 7 is included in that package and
the armed forces will probably be out of Iraq when the first patch is
available.... ;-)

Jens

Back to top
Jens Kretzschmar
Guest





PostPosted: Tue Dec 16, 2003 1:22 pm    Post subject: Re: Exception "Bad Application Handle" - why? Reply with quote

Quote:
One other thing
Normally I write and compile a *very* simple EXE to act as a testbed
for the DLL I am working on.

Then set that up as the Host Application under Run/Parameters

I then debug the DLL - not the App

I discovered that while I was forking through the help file yesterday. How
does that work?

I clicked on "Load" and it simply started the EXE. And when I clicked the
Button which executed the function calling the DLL it simply crashed. I
just tested it with the repaired DLL and it never reaches the breakpoint
set at the beginning of the called function in the DLL. Can I debug only in
the CPU Window?

(So far I just wrote my DLLs as EXE, debugged them and converted them into
DLLs afterwards.).

Thanks teaching me. Smile)

Jens

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.